ryrych / git_cheatsheet

QA and workflow examples compiled from different sources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Git Cheat Sheet

Personal cheat sheet. Slightly inspired by this Git Cheat Sheet

TOC

Branching

How to remove remote branch?

git push origin --delete <branchName>

How to figure out the common ancestor of master and feature?

git merge-base feature master

How to display all branches?

git log --branches

How to push a local branch to a different remote?

git push origin my_local_branch:feature_branch

Committing changes

How to make sure that file doesn’t have bad white space?

git diff --check

How your commit message should look like?

  • 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’

How to commit changes?

  • 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

Comparing changes

How to review commits from feature branch that aren’t merged into master?

git log feature --not master

or

git log master..feature

How to review what you’re about to push to a remote branch?

git log origin/master..HEAD
git log origin/master..

How to review all unique commits that master and feature have?

git log master...feature

How to review all unique commits displaying which branch commit is from?

git log --left-right master...feature

How to display where was your master branch yesterday?

git show master@{yesterday}

And 2 months ago?

git show master@{2.months.ago}

How to display the parent of a commit (e.g HEAD)?

git show HEAD^

How to display the second parent of d921970?

git show d921970^2

Note: this is only useful for merge commits, which have move than one parent.

How to preview previous revision of a file?

git show e051eff~1:app/assets/javascripts/file.js

Modify ~1 to get previous revisions

Finding commits

Using keywords

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

Source thoughtbot blog

Using pickaxe

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.

Formatting output

How to format reflog as git log?

git log -g

Ignoring files

How to ignore files (applies to all contributors)?

echo "some-file.txt" >> .gitignore

How to ignore personal files?

echo "some-work-in-progress-file.txt" >> .git/info/exclude

How to add files except those that match pattern defined in .gitignore?

git add -A

Restoring deleted files

Resetting to origin/featureA

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

Using git reflog

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

Solving conflicts

I didn’t solved conflicts properly. Can I re-generate conflict marks?

git checkout --conflict=diff3 some-file

or

git checkout --conflict=merge some-file.txt

How to accept only one side when resolving conflicts?

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.

How not to confuse git checkout --ours / --theirs in rebase?

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.

More info

Workflow

Safe usage of git push --force for feature branches

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

Working directory

How to partially stage file?

git add --patch

How to partially reset files?

git reset --patch

How to partially checkout file?

git checkout --patch

How to partially stash changes?

git stash save --patch

How to check out file in a given revision?

git checkout e051eff~1 app/assets/javascripts/file.js

How to reapply changes that would cause conflicts when applied?

git stash branch changes-from-stash

How to clean working directory and get rid of stashed changes?

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

Resources I've found especially helpful

About

QA and workflow examples compiled from different sources