Skip to content

Git

Basic workflow

  • Initialize repository:

      git init
  • Add 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 status
  • Unstage all files:

      git reset
  • Discard unstaged changes in file:

      git checkout <file>
  • Add all modified files to stage:

      git add -u
  • Amend current stage to previous commit:

      git commit --amend
  • Add remote repo:

      git remote add origin https://example.com/repo1.git
  • Push current local branch to remote repo:

      git push origin
  • Show all remotes:

      git remotes -v
  • Show both local and remote branches:

      git branch -a
  • See the manual page for a command:

      git <command> --help

    or:

      git help <command>

Repositories

  • Initialize repository:

      git init
  • Initialize bare repository:

      git --bare init
  • Clone remote repo:

      git clone https://example.com/repo1.git
  • Clone remote repo including submodules:

      git clone --recursive https://example.com/repo1.git
  • Locally clean up remote repo:

      git fetch origin --prune

    or:

      git remote prune origin
  • Add 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 branch
  • See all branches on current repo:

      git branch --all
  • Create a new empty branch of name ‘A’:

      git checkout --orphan A && git rm --cached -r .
  • Create a new branch of name ‘A’:

      git branch A
  • Switch to branch ‘A’:

      git checkout A
  • Create new branch ‘A’ and switch to it:

      git checkout -b A
  • Rename branch ‘A’ to ‘B’:

      git branch --move A B
  • Merge branch ‘A’ into main branch:

      git checkout main && git merge A
  • Delete merged branch ‘A’:

      git branch --delete A
  • Delete unmerged branch ‘B’:

      git branch --delete --force B

    or:

      git branch -D B
  • Push branch ‘A’ to remote repo:

      git push origin A
  • Remove branch ‘A’ from remote repo:

      git push origin --delete A
  • Merge branch ‘A’ into your active branch:

      git merge A
  • Push current local branch to a remote repo as main:

      git push origin HEAD:main
  • Keep 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 --all
  • Commit stage and reuse last log message:

      git commit -c ORIG_HEAD
  • Amend current stage to previous commit:

      git commit --amend
  • Undo accidental ammed to commit:

      git reset --soft <commit>

Diff

  • Show changes on all unstaged files:

      git diff
  • Show unstaged changes of file:

      git diff <file>
  • Show changes on all staged files:

      git diff --cached
  • Show unstaged changes of file:

      git diff --cached <file>
  • Show changes between commit ‘aaaaaa’ and commit ‘bbbbbb’:

      git diff aaaaaa^..bbbbbb
  • Show changes between current branch and main:

      git diff main
  • Show changes between branch ‘A’ and main:

      git diff main..A
  • Show changes between branch ‘A’ and branch ‘B’:

      git diff A B

Files

  • See commited files:

      git ls-files
  • Add current directory to stage:

      git add .
  • Add all modified files to stage:

      git add --update
  • Add 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 --patch
  • Remove 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 save
  • Interactively record changes and go back to a clean state:

      git stash save --patch
  • List all current stashes:

      git stash list
  • Show 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 log
  • Vie log of commits with a graph of changes:

      git log --stat
  • Vie log with pagination:

      git log -v

Fetch/Pull/Push

  • Pull updates from remote repo to local:

      git pull origin main
  • Fetch remote repo and rebase to local:

      git fetch origin main && git reset --hard origin/HEAD
  • Push updates from local repo to remote:

      git push origin main
  • Push local branch ‘B’ to remote repo as branch ‘A’:

      git push origin B:A
  • Delete branch ‘A’ from remote repo:

      git push origin :A
  • Fetch remote branch ‘A’ to local branch ‘B’:

      git branch --force B origin/A
  • Fetch branch ‘A’ from remote repo w/o merging:

      git fetch origin A
  • Generate pull request with patch for changes:

      git request-pull -p <start> <url> <end>

Reset

  • Unstage all files:

      git reset
  • Reset 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 main
  • Add upstream repo as remote:

      git remote add upstream https://example.com/upstream-repo.git
  • Fetch the branches and commits from upstream repo:

      git fetch upstream
  • Merge changes from upstream into your repo:

      git merge upstream/main
  • Workflow 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> main
  • Add ‘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 main
  • Pull 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 tag
  • Show information about specific tag:

      git show 0.1
  • Push tags to remote repo:

      git push origin --tags
  • Push specific tag 0.1 to remote repo:

      git push origin 0.1
  • Delete tag 0.2:

      git tag --delete 0.2
  • Delete tag 0.2 from remote repo:

      git push origin :refs/tags/0.2
  • Checkout tag 0.2:

      git checkout tags/0.2