A collection of git-related tips, tricks and best practices
Copycat @Octocats
- Git Best Practices
- Standards
- Setup
- Git by Task
- Git Troubleshooting
- Git By Example
- Switch to previous branch
- Use grep
- Abort a merge
- Show file in other git branch
- Pick a file from another branch/commit
- Pick a file from another branch/commit (that does not exist in the current branch)
- Pick a file from another branch but rename it
- Find branches who are not yet merged to develop
- Find out which branch contains a specific commit
- See which branch a commit belongs to
- List all dev working on a project
- See only meaningful changes without whitespace in diffs
- See changed words when editing prose
- View all global settings
- Check parent of a merge and file changes
- Checking history of a file
- Find out which remote branch a local branch is tracking
- Update your remote
- Find the commit where the branch was started
- Add everything but whitespace changes
- Find a commit that touches a particular snippet of code
- Need to remove some files from a previous commit
- Checkout a new branch from a hash
- Check if a rebase is in progress
- Checkout only part of a file
- Some JIRA Assistance
- Commit early and often
- Keep branches short-lived
- Keep branches up-to-date
- Rebase when possible
- Enforce naming standards
More on git best practices.
- Use consistent casing in the subject line
- Do not end the subject line with a period
- Use imperative mood in commit messages
- Limit the subject line to 50 characters
- Use a body
The commit should look like this:
<title>
<BLANK LINE>
<body>
Git uses the imperative mood when merging or reverting:
Good Add validation error msg
Bad adding validation error msg
You can start your commit with either a lower case or an uppercase letter, but be consistent.
Lowercase works well with labels, for example:
docs(changelog): update change log to beta.5
This is a handy table for labels Angular's commit standards and guidelines:
Label | Description |
---|---|
feat | A new feature |
fix | A bug fix |
style | Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) |
refactor | A code change that neither fixes a bug nor adds a feature |
perf | A code change that improves performance |
test | Adding missing or correcting existing tests |
chore | Changes to the build process or auxiliary tools and libraries such as documentation generation |
doc | Documentation only changes |
You should also use a body to explain what the commit does. A good idea is to reference the JIRA / Github issue, for example:
fix: validation on text input
This fixes the validation on the text input, broken in commit a05bcd3.
Closes SEWA-104.
You can set up your editor to open a standard template on commit. Find instructions here.
- Prefix with
feature
orfix
labels; - Use lowercase;
- Include JIRA issue;
- Separate words with either slash, underscore or hyphen (avoid mix and match)
Good feat/SEWA-104/react-barcodes
Bad feat_SEWA_104_React-barcodes_Step-2
git config --global core.editor "code --wait"
git config --global core.editor "atom --wait"
git config --global core.editor "subl -n -w"
git config --global core.editor "vim"
Create a file called ~/.gitmessage.txt
in your home directory with the following content:
Subject line
<blank line>
Body.
[JIRA-XXX]
Tell git to use it:
git config --global commit.template ~/.gitmessage.txt
You can ignore files locally or globally by adding them to:
- your repo's
.git/info/excludesfile
- a local
.gitignore
- your
.gitignore_global
in your home directory
Then point the core.excludesfile
setting in your .gitconfig
to it:
[user]
name = Kitty
email = [email protected]
[core]
excludesfile = /Users/kitty/.gitignore_global
The .gitignore_global
:
npm-debug.log
node_modules
coverage
.DS_Store
git config --global help.autocorrect 1
This will correct your typos:
git checkotu
WARNING: You called a Git command named 'heckout', which does not exist.
Continuing under the assumption that you meant 'checkout'
in 0.1 seconds automatically...
README.md
The first commit of a repository cannot be rebased like regular commits, so it's good practice to create an empty commit as your repository root:
git commit -m "root" --allow-empty
Amend the latest message, if you haven't pushed:
git commit --amend -m "New commit message"
If you forgot to add a file to the latest commit, you can still reuse the same commit message:
git add filename
git commit --amend --no-edit
Useful when something needs a quick fix, but you are not finished with what you were doing:
git add -a
git stash
git stash apply
Some cleanup:
git stash list
git stash clear
This will apply and delete from the stack:
git stash pop
Diffing code:
git diff origin/develop // see changes that develop does not have
git diff origin/develop <filename> // can also pass a file name
git diff HEAD // compare with staged changes
git diff -w // see changes without indent changes
git diff --cached // see diffs for files already in the staging area
git log -3 // show the last 3 commits
git log --after="2014-7-1"
git log --after="yesterday" // and also "1 week ago" or --before
A list of examples:
git log —oneline
git log -3
git log --author="ericat"
git log -p -S"Math" // code changes that include "Math", the -p also includes the code changes
git log -p -G <regex>
git log --no-merges
git log --grep="PD-6300"
git log —all --grep="PD-6300"
git log --all --oneline --decorate --author="Erica" --since="1.week"
git log --author=“Ericat|Rebecca"
git log — webpack.config.babel.js // show the history of changes for a particular file. Can omit — if there is no risk of mixing it up with a branch
git log master..wip-star-items // see all changes in wip that are not in master.
git shortlog // see history without hashes
When git log
shows a :
at the bottom, it means paginated results. You can navigate with:
B (back) F (forwards)
or simply j k.
You can also search within the pagination with /searchterm
List all branches that have already been merged into master
:
git branch --merged master
Delete them:
git branch --merged develop | grep -v 'master$' | xargs git branch -d
Delete local branches that have been deleted remotely:
git prune
Fetch and purge old data, making sure everything is up to date:
git fetch -p
Simply chekout the current branch. If you are on a detached HEAD from develop, do:
git checkout develop
This may happen if you create a branch with a similar name to another, but with different casing.
For example:
fix/SEWA-776/ism-tool-tip
and fix/sewa-776/seatmap-tooltip
The error you may get:
fatal: fix/sewa-776/seatmap-tooltip cannot be resolved to branch.
fatal: The remote end hung up unexpectedly
To fix, rename .git/refs/head/SEWA-776
to .git/refs/head/sewa-776
.
git checkout -
Find a list of files containing a CSS variable with grep
Bonus: open them in vim
git grep —name-only \$tint-white | xargs vi
git reset --hard HEAD
Checkout a specific version of a file:
git checkout <hash> -- <file_path>
git show fe-tests:test/acceptance/sell.js
git checkout <hash> -- <path_to_file>
git checkout <other_branch> — <path_to_file>
git show <branch>:<path_to_file> > <new_path_to_file>
git branch --no-merge develop
git branch -a —contains <hash>
git log --all --source --oneline
git shortlog
git shortlog -s -n -e
git shortlog -sn // list devs with n of commits
git diff -w
This is useful if you suspect Git has been converting line endings.
git diff --word-diff
git config --global -l
git config --list
git show --pretty=raw <hash>
git log -- package.json
git blame package.json
git blame -L150 package.json
git blame -L150,+10 package.json
git branch -vv
git remote set-url origin <url>
Visually, through the command line:
git log --graph --oneline --all --decorate
Through a few other commands:
git reflog --date=local | grep branchname
git cherry -v develop // finds the diff between your branch and develop
same as:
git log --oneline feat/JIRA-687-react-input ^develop
git log develop..master // show all commits that your branch have that are not yet in master
git diff --ignore-all-space | git apply --cached
git grep class="ticket-price-variation"
The above will output a list of files that contain a particular snippet.
git reset —soft HEAD^
If you want to checkout and old version of your code, you can do it in another branch:
git checkout -b test-branch 56a4e5c08
You can check whether a rebase is in process by looking for the directory .git/rebase-merge/
.
git checkout -p (<filename>, optional)
When you've committed yet another console.log
(you could also use a linter :P)...
Find out what has changed in the past two weeks (sprint goals?):
git log --since='2 weeks ago' --oneline
What have you done last week? #timesheets
git log --all --oneline --author='Erica' --since='1.week'
Grep for a ticket name:
git log --grep='PD-6300'
git log —all --grep='PD-6300'