Basic workflow
Initialize repository:
git initAdd current directory to stage:
git add .Commit stage with message ‘Message’:
git commit -m 'Message'Add modified file to stage:
git add <file>Show working tree status:
git statusUnstage all files:
git resetDiscard unstaged changes in file:
git checkout <file>Add all modified files to stage:
git add -uAmend current stage to previous commit:
git commit --amendAdd remote repo:
git remote add origin https://example.com/repo1.gitPush current local branch to remote repo:
git push originShow all remotes:
git remotes -vShow both local and remote branches:
git branch -aSee the manual page for a command:
git <command> --helpor:
git help <command>
Repositories
Initialize repository:
git initInitialize bare repository:
git --bare initClone remote repo:
git clone https://example.com/repo1.gitClone remote repo including submodules:
git clone --recursive https://example.com/repo1.gitLocally clean up remote repo:
git fetch origin --pruneor:
git remote prune originAdd multiple remote repos:
git remote set-url origin --add https://example.com/repo1.git git remote set-url origin --push --add https://example.com/repo2.git
Branches
See local branches on current repo:
git branchSee all branches on current repo:
git branch --allCreate a new empty branch of name ‘A’:
git checkout --orphan A && git rm --cached -r .Create a new branch of name ‘A’:
git branch ASwitch to branch ‘A’:
git checkout ACreate new branch ‘A’ and switch to it:
git checkout -b ARename branch ‘A’ to ‘B’:
git branch --move A BMerge branch ‘A’ into main branch:
git checkout main && git merge ADelete merged branch ‘A’:
git branch --delete ADelete unmerged branch ‘B’:
git branch --delete --force Bor:
git branch -D BPush branch ‘A’ to remote repo:
git push origin ARemove branch ‘A’ from remote repo:
git push origin --delete AMerge branch ‘A’ into your active branch:
git merge APush current local branch to a remote repo as main:
git push origin HEAD:mainKeep alternative branch ‘A’ with local-only changes:
git checkout -b A origin/main git config branch.A.rebase true git add Makefile git commit -m 'My local-only Makefile' git checkout main git pull
Commits
Commit stage with message ‘Message’:
git commit --message='Message'Commit all staged changes in current repo:
git commit --allCommit stage and reuse last log message:
git commit -c ORIG_HEADAmend current stage to previous commit:
git commit --amendUndo accidental ammed to commit:
git reset --soft <commit>
Diff
Show changes on all unstaged files:
git diffShow unstaged changes of file:
git diff <file>Show changes on all staged files:
git diff --cachedShow unstaged changes of file:
git diff --cached <file>Show changes between commit ‘aaaaaa’ and commit ‘bbbbbb’:
git diff aaaaaa^..bbbbbbShow changes between current branch and main:
git diff mainShow changes between branch ‘A’ and main:
git diff main..AShow changes between branch ‘A’ and branch ‘B’:
git diff A B
Files
See commited files:
git ls-filesAdd current directory to stage:
git add .Add all modified files to stage:
git add --updateAdd file to stage (accepts wildcards):
git add <file>Interactively add parts of a file to stage:
git add --patch <file>Interactively add parts of all modified files to stage:
git add --patchRemove file from tracked files (pending commit):
git rm <file>Unstage file but keep changes:
git reset HEAD <file>Discard unstaged changes in file:
git checkout -- <file>Interactively select parts of a file to unstage:
git checkout --patch <file>Get file from 4 commits ago:
git show HEAD~4:<file>Get file from a specific commit:
git show <commit>:<file>
Stash
Record changes and go back to a clean state:
git stash saveInteractively record changes and go back to a clean state:
git stash save --patchList all current stashes:
git stash listShow the changes for a stash:
git stash show <stash>Remove a stash from the list and apply it to the working tree:
git stash pop <stash>Apply a stash to the working tree but don’t remove it:
git stash apply <stash>Create a new branch starting from a specific stash:
git stash branch <branch> <stash>Delete a stash:
git stash drop <stash>Remove all stashes:
git stash clear
Logs
View logs of commits:
git logVie log of commits with a graph of changes:
git log --statVie log with pagination:
git log -v
Fetch/Pull/Push
Pull updates from remote repo to local:
git pull origin mainFetch remote repo and rebase to local:
git fetch origin main && git reset --hard origin/HEADPush updates from local repo to remote:
git push origin mainPush local branch ‘B’ to remote repo as branch ‘A’:
git push origin B:ADelete branch ‘A’ from remote repo:
git push origin :AFetch remote branch ‘A’ to local branch ‘B’:
git branch --force B origin/AFetch branch ‘A’ from remote repo w/o merging:
git fetch origin AGenerate pull request with patch for changes:
git request-pull -p <start> <url> <end>
Reset
Unstage all files:
git resetReset back to previous commit:
git reset --soft HEAD^Reset back to previous commit and discard changes:
git reset --hard HEAD^Reset back to specific commit:
git reset --soft <commit>Reset back to specific commit and discard changes:
git reset --hard <commit>
Revert
Revert last commit:
git revert HEAD^Revert specific commit:
git revert <commit>
Forks
Merge an upstream repo directly into your fork:
git pull https://example.com/upstream-repo.git mainAdd upstream repo as remote:
git remote add upstream https://example.com/upstream-repo.gitFetch the branches and commits from upstream repo:
git fetch upstreamMerge changes from upstream into your repo:
git merge upstream/mainWorkflow for adding a feature to a GitHub fork:
git clone [email protected]:me/my_fork.git git remote add upstream https://github.com/user/original.git git checkout -b new-feature [...changes...] git add && git commit -m 'Add new feature' git checkout main && git reset --hard upstream/main git checkout new-feature && git rebase main git push -u origin new-feature
Submodules
Split directory into a new repo:
git filter-branch --prune-empty --subdirectory-filter <directory> mainAdd ‘project.git’ as a submodule inside directory:
git submodule add [email protected]:dir/project.git <directory>Update submodule inside directory recursively to get its submodules too:
git submodule update --init --recursive <directory>Update specific submodule:
git submodule update --remote --merge <submodule>Update all submodules:
git submodule foreach git pull origin mainPull from remote repo with updated sumodules and update locally:
git pull origin main && git submodule update <submodule>Remove submodule in directory:
git submodule deinit <directory>/ git rm <directory>/ rm -rf .git/modules/<directory>
Tagging
Create new tag for specific commit as release 0.1:
git tag 0.1 <commit>Create an annotated tag for HEAD as release 0.2:
git tag --annotate 0.2 --message='my version 0.2'List tags of current branch:
git tagShow information about specific tag:
git show 0.1Push tags to remote repo:
git push origin --tagsPush specific tag 0.1 to remote repo:
git push origin 0.1Delete tag 0.2:
git tag --delete 0.2Delete tag 0.2 from remote repo:
git push origin :refs/tags/0.2Checkout tag 0.2:
git checkout tags/0.2