Here are the top 10 Git commands I use at work:
- git status
- git pull
- git checkout
- git add
- git commit
- git push
- git branch
- git merge
- git rebase
- git stash
git status
Show the working status tree with git status
:
git status
Show changes in the working tree with git diff
:
git diff
git pull
Sync the current branch with remote using git pull
:
git pull origin <branch>
If an upstream is set, you can run a shorter command:
git pull
To set an upstream:
git branch --set-upstream-to <branch>
If there are divergent changes, then pull with rebase:
git pull --rebase
git checkout
Create a new branch with git checkout
:
git checkout -b <branch>
Switch to a branch:
git checkout <branch>
Return to the previous branch:
git checkout -
Restore a working tree file:
git checkout -- <file>
Restore all working tree files:
git checkout .
git add
Add a file with git add
:
git add <file>
Add all files:
git add .
Add files in interactive mode:
git add -i
Unstage a file:
git reset <file>
Unstage all files:
git reset
git commit
Record changes with git commit
:
git commit -am "<message>"
Use editor when committing:
git commit -v
Change your editor:
git config --global core.editor "<editor>"
git push
Update the remote branch with git push
:
git push origin <branch>
If an upstream is set, you can run a shorter command:
git push
Update and track the remote branch:
git push -u origin <branch>
Or update your Git config to automatically set up remote tracking:
git config --global push.autoSetupRemote true
Which allows you to do the following:
git push
git branch
List all branches with git branch
:
git branch
Rename your current branch:
git branch -m <branch>
Delete a branch:
git branch -d <branch>
git merge
Join 2 development histories with git merge
:
git merge <branch>
Revert the merge:
git revert -m 1 <commit>
git rebase
Reapply your commits on top of another branch with git rebase
:
git rebase <branch>
Rebase in interactive mode:
git rebase -i <commit>^
git stash
Record the current state and go back to a clean working directory with git stash
:
git stash
List all stashes:
git stash list
Restore and remove the most recent stash:
git stash pop
Restore and keep the nth stash:
git stash apply stash@{<n>}
Remove the most recent stash:
git stash drop