Task | Command |
---|---|
Fetch for later merging | git fetch [remote name] |
Pull and merge | git pull [remote name] |
Pull and rebase local changes on top (instead of merging) | git pull --rebase [remote name] |
Clone only a subset of files | git init repo cd repo git remote add origin url git config core.sparsecheckout true echo "path/*" >> .git/info/sparse-checkout git pull --depth=1 origin master |
Task | Command |
---|---|
Push commits to the remote repo | git push <remote> [branch] |
Push a new local branch to the remote repo and track it | git push -u <remote> <branch> |
Push a specific tag to the remote repo | git push <remote> <tagname> |
Push all tags to the remote repo | git push --tags <remote> |
Task | Command |
---|---|
Create a branch | git branch <new branch name> |
Create a branch for a starting point e.g. commit | git branch <new branch name> [starting point] |
Create a branch and checkout at the same time | git checkout -b <new branch name> [starting point (commit)] |
View all local branches | git branch |
View all remote branches | git branch -r |
View all branches | git branch -a |
View all branches that are merged into the current branch | git branch --merged |
View all branches that are not merged into the current branch | git branch --no-merged |
View all branches that contain a commit | git branch --contains <commit id> |
Delete a branch (foo) that has been merged into the current branch | git branch -d foo |
Delete a branch (foo) that has not been merged into the current branch | git branch -D foo |
Rename a branch | git branch -m <old> <new> |
Task | Command |
---|---|
Merge from branch foo into the current branch | git merge foo |
Merge from foo but don't commit | git merge --no-commit foo |
Force creation of a merge commit (allows easier reverting of the merge) | git merge --no-ff foo |
Add a one line log messae from each merged commit | git merge --log foo |
Specify a custom log message | git merge -m 'msg' foo |
Merging with conflicts | git merge foo ; resolve; git add ; git commit |
Merge a single commit from another branch into the local one | git cherry-pick <commit id> |
Merge latest commit from a branch | git cherry-pick <branch name> |
Merge latest commit from a tag | git cherry-pick <tag name> |
Cherry pick but edit the commit message | git cherry-pick -e <commit id> |
Cherry pick but don't commit | git cherry-pick --no-commit <commit id> |
Add a "Signed-off-by" line to the commit message | git cherry-pick --signoff <commit id> |
Task | Command |
---|---|
Rebase current branch against master | git rebase master |
Rebase current branch against a commit | git rebase <commit id> |
Continue a rebase after a conflict | git rebase --continue |
Skip a commit that is causing a conflict | git rebase --skip |
Abort a rebase | git rebase --abort |
Undo a rebase after it completes | git reset --hard ORIG_HEAD |
Interactive rebase | git rebase -i |
Move branch | git rebase --onto <onto branch> <from branch> <branch to move> |
Task | Command |
---|---|
List all tags | git tag |
Tag the latest commit as v1.0 in the current branch | git tag v1.0 |
Create a tag called beta1 from the next to last commit | git tag beta HEAD^ |
Task | Command |
---|---|
Stash changes in the working tree | git stash |
Apply a stash to the current working tree | git stash apply |
Apply a stash and remove from the stash stack | git stash pop |
List the available stashes | git stash list |
Create a stash in patch mode | git stash -p |
Delete stashes | git stash drop <stash name> |
Remove all stashed changes | git stash clear |
Create a branch from an existing stash | git stash branch <branch name> [<stash name>] |
Task | Command |
---|---|
View a list of all commits | git log |
View the log with each commit on a single line | git log --oneline |
View the last N commits | git log -N |
Show the changes made in the last commit | git log -l -p HEAD |
List all commits that changed a specific file | git log --follow -- filename |
Limit the log output to a single file or directory | git log -- some/path/ or git log -- some_file |
View the commits in the last week | git log --since="1 week" or git log --after="7 days" |
View the commits prior to last week | git log --before="1 week" or git log --until="7 days" |
View the log entries by a single committer | git log --author="Stephen" |
View the log entries matching a regular expression | git log --grep="some [Rr]eg[Ee]x" |
View the log entries matching a regular expression and ignoring case | git log --grep="some [Rr]eg[Ee]x" -i |
View which files were changed in each each commit | git log --stat |
View cumulative stats only for each commit | git log --shortstat |
View the commits between two branches | git log --left-right --cherry-pick --oneline branch1...branch2 |
View the changes within the files between each commit | git log --patch |
Graphically view the commits | git log --graph --all --decorate --oneline |
Task | Command |
---|---|
View the differences beween the current working area and the staging area | git diff |
View the differences ignoring whitespace | git diff -w |
View the differences between the staging area and the repo | git diff --staged |
View the differences between the working tree and a commit in the repo | git diff HEAD or git diff <commit id> |
View the differences between two commits | git diff first second |
View the differences between a local and a remote branch | git diff <local_branch> <remote>/<remote_branch> |
Limit the diff output to a specific path | git diff -- path/ |
Show the files that have changed | git diff --stat |
Show the files that have changed between the working tree and a branch | git diff --stat -- branch_name |
Show change stats between the last 10 commits | git diff --stat HEAD~10 |
Show statistics between 2 commits | git diff --stat first second |
Show summary stats for the last 10 commits | git diff --shortstat HEAD~10 |
Show stats in a parseable format for the last 10 commits | git diff --numstat HEAD~10 |
Show the patch in addition to the statistics information | stats in a parseable format for the last 10 commits |
View the commits between two branches | git log --left-right --cherry-pick --oneline branch1...branch2 |
Task | Command |
---|---|
Check the stats of a patch to be applied | git apply --stats <patch> |
Check for errors | git apply --check <patch> |
Apply the patch but don't commit it | git apply <patch> |
Apply and commit the patch | git am <patch> |
Apply and commit the patch with signoff | git am --signoff <patch> |
Task | Command |
---|---|
Display file with entire line-by-line commit info | git blame some/file |
Start the output of blame at line 10 | git blame -L 10 some/file |
Limit the output of blame to lines 10 through 20 | git blame -L 10,20 some/file or git blame -L 10,+11 file |
Show 10 lines of blame starting at regex | git blame -L "/def to_s/",+10 some/file |
Task | Command |
---|---|
Amend the previous commit | Stage a new change and git commit --amend |
Amend the previous commit and keep the same log message | git commit --amend -C HEAD |
Fix the previous commit by removing it | git reset --hard HEAD |
Use interactive rebase to edit a commit other than the last one | git rebase -i HEAD~3 then git commit --amend then git rebase --continue |
Revert a commit | git revert <commit id> |
Revert a commit using the default message | git revert --no-edit <commit id> |
Revert a commit, but don't commit the change | git revert --no-commit <commit id> |
Reset staged changes but don't erase any changes | git reset HEAD |
Reset staged changes but don't erase any changes | git reset HEAD |
Reset specific files | git reset HEAD <file1> <file2> ... |
Completely undo last commit | git checkout HEAD <file or path to reset> |
Completely remove the last 3 commits | git reset --hard HEAD^^^ |
Reset last commit and stage the changes | git reset --soft HEAD^ |
Undo last change to head | git reset ORIG_HEAD |
Erase commits with interactive rebase | git rebase -i <commit to erase |
Narrow down a buggy commit | git bisect start , git bisect bad , git bisect good <commit id> , git bisect reset |
Task | Command |
---|---|
Add a new remote repo | git remote add <name> <repo URL> |
Remove a remote | git remote rm <name> |