Skip to content

Instantly share code, notes, and snippets.

@deqing
Last active October 4, 2018 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deqing/ca565aa91a9d15405edf46a1e2c5f818 to your computer and use it in GitHub Desktop.
Save deqing/ca565aa91a9d15405edf46a1e2c5f818 to your computer and use it in GitHub Desktop.
my git cheatsheet

Table of Contents

Setup

git config --global user.name "John Doe"
git config --global user.email "jdoe@email.com"
# and for fun!
git config --global color.ui true

git config --list

General

Commit changes

git commit -m "Message"

Add and commit in one step

git commit -am "Message"

Update all changes

git add -u

Remove files from Git

git rm index.html

Remove file but do not track anymore

git rm --cached index.html

Undo

Undo changes of one file

git checkout -- index.html

Discard all current changes in staging area

git reset --hard

Undo lastest commit and leave changes in staging area

git reset --soft HEAD~

Undo lastest commit and undo all changes in that commit

git reset --hard HEAD~

Move a file out of staging area, and keep it's changes

git reset HEAD filename

Branch

Create branch

git branch branchname

Create and change to that branch

git checkout branchname

Find out which branch contains a change

git branch --contains 50f3754

List or delete remote-tracking branches (useful after branch merged and deleted on Github)

git branch -r

See which branches are tracked and which are not

git branch -vv

Show

Show oneline-summary of the last three commits (you can define branch name to view changes on that branch)

git log --oneline -3

Show oneline-summary of the last three commits and ref names

git log --oneline -3 --decorate

Show oneline-summary of the last three commits, ref names and changed files summary

git log --oneline -3 --decorate --stat

Show only specific commits

git log --author="Sven"
git log --grep="Message"
git log --until=2016-09-01
git log --since=2016-01-01

List changed files of a commit

git show --name-only b1649a

Show changed content of one file of a specific commit

git show b1649a filename
git difftool --tool=vimdiff --no-prompt b1649a^:filename b1649a:filename

Show file content of a specific commit

git show b1649a:filename

Show changes of each commit for a specific file

git log -p filename

Compare

Compare commits or branches

git diff 6eb715d..537a09f  # Use -b to ignore spaces
git diff branch1 branch2

Compare one file changes between two commits

git diff 6eb715d..537a09f index.html

Show summary between two commits

git diff --stat --summary 4f032cd..1affbd8

Show changes of one file in staging area

git diff --cached filename

Sync

Get latest code from remote (same branch)

git pull --rebase  # or git pull

Get latest code from another branch

git rebase main  # so new changes in main branch applied to your current branch

Save

Save your current work

git stash        # save work and undo all checkouts, like git reset --hard
git stash pop    # restore your work
git stash show

Save one file only

1. git add filenames        # all files that you'd like to save and reset
2. git stash --keep-index

Restore one file only

1. git stash list                       # find out which stash you'd like to restore with
2. git checkout stash@{0} configure.ac  # restore configure.ac only

Commits

Update latest commit instead of checkin a new one

git commit --amend

Checkout a specific commit

git checkout b1649ac

Cherry-pick one file only from a commit

git cherry-pick -n SHA  # -n means don't commit
                        # remove other files
git commit

Reorder your commits

git rebase -i $parent_of_flawed_commit
git rebase -i HEAD~4   # Change pick to e if you need to change commit or commit log
                       # then it will stop at your 'e' commit
git commit --amend     # Do this when you are done.
git rebase --continue  # then continue to apply other commit

Merge latest 5 commits into one

git rebase -i HEAD~5

Remote

Add a new remote

git remote add local-name https://github.com/user/repo.git

List remote repositories

git remote -v
git remote -vv

Delete remote branch

git push origin :to-be-deleted

Working with Upstream

Before Setup

  1. Fork the repository of the project on github.com, then you will have your own repository
  2. Click "Clone or download" button in your own repository
  3. Change to ssh, and copy the link
  4. Clone it and setup by following steps

Setup Steps

git clone git@github.com:your-repo-link
cd your-repo-dir
git remote add upstream https://github.com/upstream-repo-link

Submit a PR to upstream

git fetch upstream                  # Get latest code from upstream
git rebase upstream/master master   # Update your local `master` branch with latest upstream code

git rebase upstream/master          # when you are already staying at master branch

# or just
git pull --rebase

git push origin master  # Push to origin repo, master branch
# Now your repo should have a new button letting you to submit a PR to upstream, or just click "Create pull request" to submit.

Merge

git mergetool --tool=vimdiff

Solve conflicts

e.g. when doing `git rebase up/master master` and seeing conflicts:
LOCAL is change from upstream
REMOTE is your local change!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment