- Notifications
You must be signed in to change notification settings - Fork1
💭 A collection of git tipshttps://gitpitch.com/ericat/git-tips/master?grs=github&t=white#
ericat/git-tips
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 ongit 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:
GoodAdd validation error msg
Badadding validation error msg
You can start your commit with either a lower case or an uppercase letter, but be consistent.
Lowercase works well withlabels, for example:
docs(changelog): update change log to beta.5
This is a handy table for labelsAngular'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 abody to explain what the commit does. A good idea is to reference the JIRA / Github issue, for example:
fix: validation on text inputThis 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 instructionshere.
- Prefix with
feature
orfix
labels; - Use lowercase;
- Include JIRA issue;
- Separate words with either slash, underscore or hyphen (avoid mix and match)
Goodfeat/SEWA-104/react-barcodes
Badfeat_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 thecore.excludesfile
setting in your.gitconfig
to it:
[user]name = Kittyemail = kitty@ticketmaster.co.uk[core]excludesfile = /Users/kitty/.gitignore_global
The.gitignore_global
:
npm-debug.lognode_modulescoverage.DS_Store
git config --global help.autocorrect 1
This will correct your typos:
git checkotuWARNING: 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, soit'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 samecommit message:
git add filenamegit commit --amend --no-edit
Useful when something needs a quick fix, but you are not finishedwith what you were doing:
git add -agit stashgit stash apply
Some cleanup:
git stash listgit 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 havegit diff origin/develop <filename> // can also pass a file namegit diff HEAD // compare with staged changesgit diff -w // see changes without indent changesgit diff --cached // see diffs for files already in the staging area
git log -3 // show the last 3 commitsgit log --after="2014-7-1"git log --after="yesterday" // and also "1 week ago" or --before
A list of examples:
git log —onelinegit log -3git log --author="ericat"git log -p -S"Math" // code changes that include "Math", the -p also includes the code changesgit log -p -G <regex>git log --no-mergesgit 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 branchgit log master..wip-star-items // see all changes in wip that are not in master.git shortlog // see history without hashes
Whengit 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 intomaster
:
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
andfix/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 shortloggit shortlog -s -n -egit 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 -lgit config --list
git show --pretty=raw <hash>
git log -- package.jsongit blame package.jsongit blame -L150 package.jsongit 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 branchnamegit cherry -v develop // finds the diff between your branch and develop
same as:
git log --oneline feat/JIRA-687-react-input ^developgit 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
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 anotherconsole.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'
About
💭 A collection of git tipshttps://gitpitch.com/ericat/git-tips/master?grs=github&t=white#