Git - All the commands you need to work on Git
Git has become the most widely used version control system in the world. Following are the premier commands one needs to master to work on Git.
- Setup Git
git config --global user.name <your-name>
git config --global user.email <youremail@xxx.com>
The git config command configures the user name and email for all commits by the current user.
- Clone a repository
git clone <repo>
The git clone command copies an existing central Git repository to your local machine. Take an example
git clone https://github.com/openssl/openssl.git
cd openssl
A copy of openssl Git repository is stored on your local machine within openssl directory.
- Check out branch
git checkout <branch>
The git checkout command checks out a branch. It’s also easy to create a new branch for your work.
git checkout -b <your-new-branch>
You have created a new branch and switched to it. To verify that you are working on that branch, use:
git branch
- Add changes
git add <files>
The git add command adds a change in the working directory to the staging area. To add all the changes in the current directory, use:
git add .
- Save changes
git commit
The git commit command commits the staged changes to the project histroy.
- Inspect
git status
The git status displays the state of the working directory and the staging area.
git log
The git log command displays the committed history.
- Rewrite history
git rebase -i HEAD~i
The git rebase with -i flag begins an interactive rebasing session. It opens an editor where you can enter commands for each commit, whether to merge it into other commits or rewrite its commit message.
- Push to remote
git push origin master
The git push command pushes a branch to the remote centralized server.