I am a guy who always likes to search online to get the answer. So StackOverflow is my favorite website. But after tens times’s search, you find you always search the same questions sometimes. Here I write down useful Git commands which I use most.
1. configuration
When you finish installing Git, you have to configure it to push your personal information on your GitHub or other git server.
// no "--global" means for one Git repo, with "--global" means for one user, with "--system" means for the whole system(here, you need to use sudo) git config --global user.name "Your Name" git config --global user.email "youremail@domain.com"
If you want to check your configuration status, you can use list. Of course, you can go to ~/.git/config to see your local folder’s configuration and ~/.gitconfig to check global Git configuration.
git config --list
If this is not your first time configuration, you can use amend to change it.
git commit --amend --author='Your Name <you@example.com>'
2. commit
If you want to revert to a special commit based on commitID, you can use reset.
git reset --hard c14809fa
If you want to merge multiple commits to one, use this:
-
git rebase -i <unchanged commitID>
3. fetch
Setting your branch to exactly match the remote branch can be done in two steps:
git fetch origin
git reset --hard origin/master
4.compare
If you want to compare your local Git repo with remote, you can use diff.
git diff <local branch> <remote-tracking branch> // For examplegit diff master origin/master
, orgit diff featureA origin/next
5. branch
If you want to rename a local Git branch,
-
git branch -m <oldname> <newname>
-
If you want to rename the current branch, you can simply do:
git branch -m <newname>