Skip to content

Instantly share code, notes, and snippets.

@bdlangton
Last active May 17, 2021 08:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdlangton/b47be9dc85992b99d5fa1d83e93a94f8 to your computer and use it in GitHub Desktop.
Save bdlangton/b47be9dc85992b99d5fa1d83e93a94f8 to your computer and use it in GitHub Desktop.
Git commands

Commands

Miscellaneous Description
-C <path> Run as if git was started in <path> instead of the current directory
git commit Description
-a, -all Automatically stage modified and deleted files and then commit
-p, --patch Interactively select which modified and deleted files to stage before committing
--squash=<commit> Squash this commit with another one
--author=<author> Override the author used for this commit
-m <msg>, --message=<msg> Specify the commit message
-n, --no-verify Bypass the pre-commit and commit-msg hooks
--amend Amend the previous commit with staged changes added to it
--amend --no-edit Same as --amend but don't prompt to change the commit message, just use what was already there
git add Description
-n, --dry-run Don't actually add the files, just show if they exist and/or will be ignored
-f, --force Allow adding otherwise ignored files
-p Interactively choose hunks of code to stage
git reset Description
HEAD <file> Unstage changes in the file already staged. Can also run this against directories for everything in the directory.
--hard HEAD~<#>
--hard <commit-hash>
Reset the current branch back to a specific commit and point the branch name back to that commit
git checkout Description
-b <new_branch> <starting_point> Creates a new branch starting with commit pointed to by starting_point. If no starting point, then it starts at where HEAD currently points. If the starting point is a branch, then it will set that branch as the remote tracking branch (to disable this, add --no-track).
-f, --force Proceed even if the working tree or index differs from HEAD. This will throw away local changes.
--detach Checkout for inspection and discardable experiments, rather than to work on that branch
-p Interactively select hunks to apply to the branch that you are checking out
<commit_hash/branch/tag> path/to/file.txt Checkout file.txt as it exists in the provided hash.
git diff Description
--name-only Show only names of changed files
--name-status Show only names and status of changed files
-w Ignore whitespaces when comparing lines
SHA1..SHA2, SHA1 SHA2 Show diff between the head of SHA1 vs head of SHA2 (will show diff as if you are merging SHA2 to SHA1)
SHA1...SHA2 Show diff between common ancestor commit of both branches vs the head of SHA2 (will show diff as if you are merging SHA2 into common anceestor)
git push Description
<remote> <branch> Normal push to a remote branch
<remote> <branch>:<remote_branch> Push local branch to a differently named (not upstream) remote branch
<remote> :<remote_branch> Deletes the remote branch on the remote repo
--force-with-lease Push force, but not if someone else has pushed up changes since you've pulled them down
-f, --force Forces a push even if commits are different. Data can be lost here and is not suggested for pushing to shared repos
-u, --set-upstream Set the remote branch as the default upstream to push to
--tags All refs under refs/tags are pushed
git pull Description
git pull <remote> <branch> Same as doing git fetch and then git merge FETCH_HEAD
-r, --rebase Rebase the current branch on top of the upstream branch after fetching
git fetch Description
<remote> Fetch from a remote repo
--all Fetch from all remote repos
--tags Fetch all tags from the remote in addition to branches fetched
-p Prunes any remote tracking branches that no longer exist on the remote repo
git bisect Description
git bisect start
git bisect bad <commit>
git bisect good <commit>
Steps to starting a bisect
git rebase Description
-i Rebase interactively, meaning you can choose commits to modify, remove, squash, reorder, etc.
<branch1> <branch2> Rebases branch2 (or current branch if not specified) onto branch1
-i HEAD~3 Rebases (interactively) the current branch onto the commit at HEAD~3
git clone Description
<remote_repo> Clones the remote repo down
-b <name>, --branch <name> When cloning, point the local HEAD to the specified branch.
git init Description
git init Initializes an empty git repo in the current directory.
You can run git init on an existing repo. It will just pick up any newly added templates/hooks in the ~/.git-templates directory. It won't overwrite existing templates/hooks even if they differ, it will just add new ones.
--template=<template-directory> Choose the directory from which to use templates (defaults to ~/.git-templates)
git merge Description
<remote>/<branch> Merges remote branch into current branch
--no-commit --no-ff <branch> Merges branch into your branch without making a commit, so you can view diff between them
git tag Description
<tagname> Set a tag on the current commit
<tagname> sha Set a tag on the specific sha
-l (or nothing) List tags
-d <tagname> Delete tag
git push origin --delete <tagname> Delete a remote tag
-f Replace an existing tag with this new one
-a Create an annotated tag
-m "Message" Pass a message to the annotated tag
git show Description
git show <object> For commits it shows the log message and textual diff.
For tags, it shows the tag message and the referenced objects.
For plain blobs, it shows the plain contents.
--name-only Show only names of changed files
--name-status Show only names and status of changed files
git remote Description
add <name> <url> Adds a remote repo
rename <old> <new> Rename a remote repo
remove <name> Remove a remote repo
set-url <name> <newurl> Set a new URL for the remote repo
show <name> Shows info on the remote repo. Includes remote branches and which branches track with which remote branches.
git branch Description
git branch <branch-name> -u <remote>/<branch> Set tracking remote branch
<branch-name> Creates a new branch at HEAD
-d <branch>, -D <branch> Deletes a branch name, -D forces delete.
-m <oldbranch> <newbranch>, -M <oldbranch> <newbranch> Renames a branch, -M forces the rename even if newbranch already exists.
-r, --remotes List remote branches
-a, --all List local and remote branches
--list List local branches
git clean Description
-f Force cleaning files even if clean.requireForce is set to true (default)
-d Remove untracked directories also
-i Interactive mode, lets you choose what files to clean
-n Dry run
git log Description
--full-history -- <file> Show full history of the file when it existed, not just commits that it was changed (even if it is currently deleted)
--no-merges Exclude merge commits
SHA1..SHA2, SHA2 ^SHA1 Show lot of commits that exist in SHA2 but not SHA1
SHA1...SHA2 Show log of commits that are only in SHA1 OR only in SHA2 (no commits that are in both branches)
git shortlog Description
-s,--summary Provide a commit count summary instead of each commit description
-n,--numbered Sort the output by the number of commits per author
-c,--committer Show committer identities instead of authors
-w<num> Limit number of characters wide to print
git cherry Description
git cherry -v origin/master Find commits yet to be applied to upstream (in this case origin/master)
-v Show the commit subjects next to the SHA
git ls-tree Description
List the contents of a tree object
master List the contents of the tree object in the master branch
git cat-file Description
Provide content or type and size information for repository objects
-p Pretty-print the contents of based on its type. Can use this on any object: commit, file, etc.
Hub Command Description
browse Open a github page in your browser
compare Open a compare page on github
compare [START..]END Compare specific hashes/branches
issue List or create issues
pr list List pull requests
pull-request Create a pull request on github (will open vim to enter PR title)
sync Fetch git objects from upstream and update branches

Show number of commits per author.

git shortlog -sn --all

Use --no-merges if you want to exclude merge commits Use -e if you want to see the users' email addresses

Get list of contributors for the repo.

git log --format='%aN' | sort -u

Display addition/deletion stats for an author.

git log --shortstat --author="Barrett Langton" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "Files changed: ", files, "Lines inserted: ", inserted, "Lines Deleted: ", deleted }'

Git-stats is a custom app that will display graphical info: npm i -g git-stats

git-stats --authors git-stats -g

Handle git merge conflicts by opening them in vim with vim-conflicted

git conflicted

Provide a tar.gz file of the latest release

git archive master --prefix='project/' | gzip > git describe master.tar.gz

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