git cheat sheet
How to show the remote tracking branch?
git branch -vv
- Caution: Double verbose output is porcelain, so don't rely on it in scripts.
- There is no other non-porcelain way, obviously: https://stackoverflow.com/questions/3631706/what-is-this-branch-tracking-if-anything-in-git/16162827#16162827
- For example useful when moving git servers.
How to show the changes of a stash?
git stash show --full-index
--full-index
is agit diff
option- For other output formats see the
git diff
help
How to quickly push a local branch to remote repository?
git push -u origin HEAD
- HEAD --> assumes local branch is checked out currently
- -u origin --> let's the local branch track the newly created remote one
How to write a git commit message?
- Read the excellent article How to Write a Git Commit Message
- In a nutshell the commit message should be a continuation of This commit will... and the first line shall be less than 50 chars. I prefer a limit of 70 chars though.
How to find diffs in the history of a file where a certain string was included?
git log -G'<regex> -- <filepath>'
How to delete a remote branch?
git push origin --delete <remote-branch-name>
How to prune the list of remote branches from deleted ones?
git fetch -p
- From the help: "Before fetching, remove any remote-tracking references that no longer exist on the remote"
How to merge a branch up until a specific commit?
- Say you have a feature branch that is tracked remotely and that you use to create pull requests, e.g. on GitHub. You create commits locally and want to hold back commits when pushing.
- Solution is to have another local branch, that does not track a remote pendant.
- Let's call the local branch that is tracking a remote one (the one you're using for your PRs), the integration branch.
- So how do you merge only certain commits from you local-only branch (i.e. you could call it the staging branch) to your integration branch in order to push it to remote and initiate another PR?
- Do it like this
- Observe the commit hash of the commit you want to be included in the PR
git checkout <integratin-branch-name>
git merge <commit-hash-of-commit-you-wanna-pr>
- The merge will usually resolve in a fast forward
- This way the integration branch (a branch is really just a pointer to a certain commit) is moved to the commit you want to pull
How to make a local-only branch track a remote one?
- This is relevant if you already pushed a local branch to a remote repository, but have forgotten to setup tracking. In this case, when you type
git status
, git won't show you how much commits the remote branch is behind your local one. - To setup up a local branch to track an already existing remote branch do this:
git checkout <branch-name>
, to ensure that you're in the right branchgit branch -u origin/<branch-name>
, setting up an upstream branchgit status
, to see how git is now able to tell if the two branches diverge in any way in terms of commits
How to obtain a list of changed files between two commits?
git diff --name-status <commit> <commit>
- -- name-status creates the list of changed files and prefixed each file with the type of change (A, D, M)
How to set user name and email address for a git repository?
git config user.name "Your Name"
git config user.email "Your Email Address"
- Hint: if you add the
--global
flag the above settings would be the default
How to create a git tag?
git tag -a -m "Tagging first version that is working, although various key objects still included." 0.1 HEAD
- -a --> unsigned tag
- -m --> a readable message
- 0.1 --> the tag name
- HEAD --> the commit the tag should be bound to
How to specify a particular commit in git (e.g. what does HEAD~1 mean)?
- There are a lot of ways to specify commits or ranges of commits
- Some examples:
HEAD~1
HEAD^1
- A good overview can be found at https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection
How to show the contents of a git commit in the command-line?
git show HEAD
How to show a tree-like representation of branches with git cli?
git log --graph --decorate --all
- --graph draws nice lines on the left side
- --decorate shows refs names next to commit hashes
- --all all refs
How to remove a file from the a git repository while keeping it in the working tree?
git rm --cached <file>
- the --cache flag takes care to not remove the file(s) from the working tree
How to amend a git commit with a commit message unchanged?
git commit --amend --no-edit