Personal cheat sheet. Slightly inspired by this Git Cheat Sheet
- Branching
- Committing changes
- Comparing changes
- Finding commits
- Formatting output
- Ignoring files
- Restoring deleted files
- Solving conflicts
- Workflow
- Working directory
- Resources I’ve found especially helpful
git push origin --delete <branchName>
git merge-base feature master
git log --branches
git push origin my_local_branch:feature_branch
git diff --check
- Your messages should start with a single line that’s no more than about 50 characters
- Followed by a blank line
- Followed by a more detailed explanation
- Instead of ‘I added tests for’ or ‘Adding tests for’, write ‘Add tests for’
- Commit per issue, with a useful message per commit
- If some of the changes modify the same file, partially stage files with
git add --patch
git log feature --not master
or
git log master..feature
git log origin/master..HEAD
git log origin/master..
git log master...feature
git log --left-right master...feature
git show master@{yesterday}
And 2 months ago?
git show master@{2.months.ago}
git show HEAD^
git show d921970^2
Note: this is only useful for merge commits, which have move than one parent.
git show e051eff~1:app/assets/javascripts/file.js
Modify ~1
to get previous revisions
git log :/typo
Finds most recent commit that contains word ‘typo’ in the commit message:
47db47b playing with typography
You can use it with other git commands like:
git commit --fixup :/second
Pickaxe finds any commit that introduced or removed the string reflog
:
git log --oneline -S reflog
To search by a regex:
git log --oneline -S re..og --pickaxe-regex
git log --oneline -S"[bash|console]" --pickaxe-regex
Note: use can use extended POSIX regular expression syntax.
How to format reflog
as git log
?
git log -g
echo "some-file.txt" >> .gitignore
echo "some-work-in-progress-file.txt" >> .git/info/exclude
git add -A
You have been working on featureA on 2 different machines. 2 changes were pushed to to origin/featureA before going home:
- C1
- C2
At home you finished the feature adding one small change:
- C3
To make the history simple (e.g in pull requests workflow), you squashed all the 3 commits and pushed the changes.
The history diverged and you can't pull with fast forward on your 1st machine. To force pull you can do:
git checkout featureA
git reset --hard origin/featureA
Your local branch diverged from PR. You reseted it with origin, forgetting about fix you didn’t push.
git reset --hard origin/feature_foo_bar
Doing the same work is a waste of time. Local history is empty.
git reflog feature_coffeelint@{60.minutes.ago}
You can look for the missing commit and cherry-pick it after:
git cherry-pick ff0000
git checkout --conflict=diff3 some-file
or
git checkout --conflict=merge some-file.txt
git checkout --ours PATH/FILE
git checkout --theirs PATH/FILE
--ours
is the branch you're merging in, --theirs
is the branch you're
merging into.
git checkout feature_foo_bar
git rebase master
# some conflict, accept change from `feature_foo_bar`
git checkout --theirs some_file
Rebase replays the current branch's commits one at a time on top of the
master. This makes master
the "base" (ours) branch, and feature_foo_bar
,
theirs.
Prefix branches with initials:
At thoughtbot we prefix our branches with our initials, signaling that those commits may get rewritten and others shouldn’t add commits to the branch. When those commits land into master or a shared branch we never rewrite them again. thoughtbot blog
git add --patch
git reset --patch
git checkout --patch
git stash save --patch
git checkout e051eff~1 app/assets/javascripts/file.js
git stash branch changes-from-stash
git clean -i
Note: use git clean --dry-run
(-n
for short) to see what’s going to be
removed; it doesn’t actually remove anything