Skip to main content
Menu
Git | Cheatsheets
Git

Quick reference #

Duplicating a repository #

  1. Open Terminal.

  2. Create a bare clone of the repository.

$ git clone --bare https://github.com/exampleuser/old-repository.git
  1. Mirror-push to the new repository.
$ cd old-repository.git
$ git push --mirror https://github.com/exampleuser/new-repository.git
  1. Remove the temporary local repository you created earlier.
$ cd ..
$ rm -rf old-repository.git

Discarding and Deleting #

Discard uncommitted changes #

git reset --hard

Delete all untracked files and directories #

git clean -fd

Nuke all previous commits #

git checkout --orphan newBranch
git add -A                          # Add all files and commit them
git commit -m "Cleanup"
git branch -D master                # Deletes the master branch
git branch -m master                # Rename the current branch to master
git push -f origin master           # Force push master branch to github
git gc --aggressive --prune=all     # remove the old files

Syncing a fork #

git remote add upstream https://github.com/[Original Owner Username]/[Original Repository].git
git fetch upstream
git checkout master
git merge upstream/master
# If your local branch didn't have any unique commits, Git will instead perform a "fast-forward"

Basic #

List all local branches #

git branch

Commit (permanently record) staged changes #

git commit -m "message"

Upload new branch to origin remote #

git push -u origin <new_branch>

List files to be committed #

git status

Submodules #

Git submodule update #

git submodule update --init --recursive

Diff #

Show unstaged changes #

git diff

Show changes using file diff tool #

git difftool

Show staged changes #

git diff --staged

Commit management #

Create a ‘fixup!’ comit for older commit (to be autosquashed) #

git commit --fixup <sha1>

Create a ‘squash!’ commit for older commit (to be autosquashed) #

git commit --squash <sha1>

Merge #

Merge other branch into current one #

git merge <other_branch>

Rebase #

Perform interactive rebase #

git rebase -i <other_branch>

Perform interactive rebase with autosquash #

git rebase -i --autosquash <other_branch>

Branch #

Rename a branch #

git branch -m <old_name> <new_name>

Delete a branch #

git branch -d <old_name>

Create new branch while staying on current one #

git branch <new_branch_name>
git rev-parse --abbrev-ref HEAD

Create new branch and switch to it #

git checkout -b <new_branch_name>

Other #

rm #

Stage file moving or renaming #

git mv <old_filepath <new_filepath>

Stash #

Stash (temporarily put away) uncommitted changeset
git stash

Restore most recent stashed changeset #

git stash pop

List all stashed changesets #

git stash list

Delete last stashed changeset #

git stash drop

Edit last commit with new staged changes #

git commit --amend

Reset #

Undo (discard) last commit #

git reset HEAD~

Undo (discard) last 3 commits #

git reset HEAD~3

Revert #

Revert previous commit by creating opposite commit #

git revert <sha1>

Tag #

Create new tag #

git tag -a <tag_name>

List tags #

git tag

History #

Show history for current branch #

git log

Show history, one line per commit #

git log --oneline

Show history, with merged branch graph #

git log --graph

Show history for specific file #

git log --follow <file_path>

Show specific commit (contents and metadata) #

git show <sha1>