Skip to content

Instantly share code, notes, and snippets.

@matshou
Last active January 25, 2022 12:19
Show Gist options
  • Save matshou/913a23562063ed1925d8064cb9ba35b3 to your computer and use it in GitHub Desktop.
Save matshou/913a23562063ed1925d8064cb9ba35b3 to your computer and use it in GitHub Desktop.
Git command references.

Reference sheet covering basic and commonly used Git commands.

Stash

Restore all files in the current directory to their stashed version §

$ git checkout stash -- .

Diff

Show a list of commits between two tags §

$ git log --pretty=oneline <tagA>...<tagB>

Show changes between commits, commit and working tree, etc §

$ git diff <commit1> <commit2>

Options

--name-only ~ show only names of changed files (§)
--diff-filter ~ select only filtered files (§)

Examples

Show branch diff (only modified files) §

$ git diff --diff-filter=M <branch1> <branch2>

Print diff in output file §

$ git diff <commit1> <commit2> > <output>

Branch

Shows a list of branches in local repository §

$ git branch

Create a new branch in local repository §

$ git checkout -b <branch>

Delete a branch in local repository §

$ git branch -d <branch>

Delete a branch in remote repository §

$ git push <remote> --delete <branch>

Rename a branch in local repository §

$ git branch -m <old_name> <new_name>
$ git push origin :<old_name> 
$ git push origin <new_name>:refs/heads/<new_name>

Combines remote branch into current local branch §

$ git push <remote> <branch>

Resets the current branch head to desired commit §

$ git reset <mode> <commit>

Creates a new orphan branch §

$ git checkout --orphan <branch>

Reset HEAD to last commit §

$ git reset --hard ORIG_HEAD

Commit

Create a new commit §

$ git commit

Using this command with no options will open a new text file where you can specify commit message and other fancy stuff. This command is not of too much use in most cases if we're using git GUI.

Set the date of the last commit to the current date §

$ GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

Print commit date §

$ git log -1 --format=%ci <commit_sha>

List all commits that changed a file §

$ git log --follow -- <filename>

Options

--amend ~ replace the tip with a new commit (§)
--no-edit ~ use with --amend to use existing commit message without launching an editor (§)
--allow-empty ~ create new empty commit (§)

Examples

Create new commit with a message §

$ git commit -m "<msg>"

Replace last commit while keeping the same message §

$ git commit --amend --no-edit

Show date and time a commit was created §

$ git show -s --format=%ci <commit>

Display last commit §

$ git show --name-status

Tags

Create a tag on your current branch §

$ git tag <tag>

List tags in local repository §

$ git tag -l

Create a tag for an older commit §

$ git tag -a <tag> <commit> -m <message>

Rename a tag in local repository §

$ git tag <new tag> <old_tag>
$ git tag -d <old_tag>
$ git push origin :refs/tags/<old_tag>
$ git push --tags

Edit annotated tag message §

$ git tag <tag_name> <tag_name>^{} -f -m "<new_message>"

Edit tag message in local repository §

$ git tag <tag> <tag>^{} -f -m <new message>

Push single tag to remote repository §

$ git push origin <tag>

Push all local tags to remote repository §

$ git push origin --tags

Remove tag from remote repository

$ git push origin :<tag>

Remove tag from local repository

$ git tag --delete <tag>

Remove all tags from local repository §

$ git tag | xargs git tag -d

Remove all tags from remote repository §

$ git tag -l | xargs -n 1 git push --delete origin

Print dates when tags were created §

$ git tag --format "%(refname:short) %(creatordate)"

Change past tag time and date (backtrack) §

$ GIT_COMMITTER_DATE="<time_and_date>" git tag -a <tag> -m <message>

Remote

Deletes stale references on remote §

$ git remote prune <remote_name>

Misc

Remove recovery/backup files §

$ git reflog expire --expire=now --all && git gc --prune=now --aggressive

Stop tracking file in repo §

$ git rm --cached <file>
$ git update-index --assume-unchanged <file>

List configured remote servers §

$ git remote

Remove all files from working tree §

$ git rm -rf .

Rename directory §

$ git mv <old_name> <new_name>

Add to index in executable mode §

$ git add --chmod=+x -- <file>

Update file to executable mode §

$ git update-index --chmod=+x <file>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment