Skip to content

Instantly share code, notes, and snippets.

@TomyJaya
Last active June 4, 2021 13:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save TomyJaya/7136b7794d5d5e4a9100 to your computer and use it in GitHub Desktop.
Save TomyJaya/7136b7794d5d5e4a9100 to your computer and use it in GitHub Desktop.
git-cheatsheet

Cherry Pick

Cherry pick based on a commit by ignoring spaces

HINT: To avoid conflicts, cherry pick with the same order of the original commits (i.e. use Unix tac command to reverse sort the git log)

git cherry-pick -X ignore-all-space <commit_hash>

e.g.

git cherry-pick -X ignore-all-space 123f2fs

Patch

Create a patch file based on a commit

git format-patch -1

e.g.

git format-path -1 ccc4752

Create a patch from a feature branch

git format-patch master --stdout > <patch_file_name>

e.g.

# Get Latest Changes
git pull 
# Create and switch to new bug fix branch
git checkout -b bug-fix-branch 
# Perform bug fixes and commit them
# Spit out the patch file
git format-patch master --stdout > bug-fix-1.patch

Apply a patch from a feature branch

git apply --stat <patch_file_name>

git apply --check <patch_file_name>

git am < <patch_file_name>

e.g.

# Review the changes
git apply --stat bug-fix-1.patch
# Simulate if git can apply the change
git apply --check bug-fix-1.patch
# Apply the patch
git am < bug-fix-1.patch
# Patch is applied and committed. Ready to push!

Clone

Cloning a remote repository (SSH)

git clone <user@server:path/to/repo.git>

e.g.

git clone git@github.com:TomyJaya/git-real.git

Cloning a remote repository (https)

git clone

e.g.

git clone https://github.com/TomyJaya/git-real.git

Status

Checking the Status of Your Files

git status 

Staging & Committing

Add a file to staging/ index/ changeset

git add <file_name>

e.g.

git add README.md

Add all files to staging/ index/ changeset

git add .
git add -A

See: http://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add

To get the list of file changes in a commit

git diff-tree --no-commit-id --name-status -r <commit_id>

e.g.

git show --pretty="format:" --name-status 53571fc
git --no-pager show --pretty="format:" --name-status 53571fc

To see what you've changed but not yet staged

git diff
If you want to see what youíve staged that will go into your next commit:
git diff --cached
git diff --staged 

Hint: cached and staged are synonymous.

Get the difference of the same file between two different commits on the same branch

git diff <start_commit>..<end_commit> -- <path/to/file>

e.g.

git diff abcsdef..HEAD --ivysettings.xml # between now and abcdef commit
git diff HEAD~2..HEAD --ivysettings.xml # between now and two commmits back

Commit your changes

git commit -m ""

e.g.

git commit -m "Initial Commit"

Commit all modified files, by-pass add to index or staging

git commit -am ''

e.g.

git commit -am 'added new benchmarks'

Remove files

git rm

e.g.

git rm PROJECTS.md

Keep the file in your working tree but remove it from your staging area

git rm --cached

e.g.

git rm --cached README

Renaming and Moving Files

git mv <file_from> <file_to>

e.g.

git mv README.md README

Logs

Retrieve logs (view commit history)

git log 
git --no-pager log -n 20 --pretty=oneline --abbrev-commit
git log --abbrev-commit --pretty=oneline ###  Get abbreviated commit
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph
git log --graph --oneline
git log --since=2.weeks
# Another really helpful filter is the -S option which takes a string and only shows the commits that introduced a change to the code that added or removed that string.
git log -Sfunction_name 

Undo (Reset, Checkout, Revert)

Undoing Things (Changing the last Commit)

git commit -m 'initial commit'
git add forgotten_file.txt
git commit --amend 
# You end up with a single commit & the second commit replaces the results of the first.

WARNING: Don't amend your last commit if you've already pushed it!

Unstage staged files

git reset HEAD <file_name>

e.g.

git reset HEAD CONTRIBUTING.md

⭐ Reverting the whole repo

git reset --hard HEAD~1 

Note: Undo commit/ local commit and nuke last commit and never see it again.

Undo commit/ local commit but keep your changes

git reset HEAD~1

Undo commit/ local but leave your files and your index:

git reset --soft HEAD~1

Revert changes (Unmodifying all modified files)

Hint: Be careful of using this command as you can't get the modification back

git reset --hard

Remove all untracked files and directories

Hint: Be careful of using this command as you can't get the untracked files back

git clean -fd 

⭐ Revert changes (Unmodifying a Modified File) - Discarding Changes

git checkout --

git checkout HEAD

e.g.

git checkout -- CONTRIBUTING.md
git checkout HEAD CONTRIBUTING.md

Note: http://stackoverflow.com/questions/13321458/meaning-of-git-checkout-double-dashes

WARNING: It's important to understand that git checkout -- [file] is a dangerous command. Any changes you made to that file are gone & you just copied another file over it. Don't ever use this command unless you absolutely know that you don't want the file.

DANGEROUS COMMAND:

# The below will discard all unstaged changes!!!
git checkout -- . 

Revert mistakenly checked in file

Note: if you accidentally made a wrong change to a file and committed it, you can get back the last but one checked in file:

git checkout HEAD~1 <file>

Remotes

Show all remotes

git remote
git remote -v ###  shows URL

Note: remote repositories are versions of your project that are hosted on the Internet or network somewhere.

Adding remote repository

git remote add

e.g.

git remote add pb https://github.com/paulboone/ticgit

IMPORTANT: http://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

  • If you clone a repository, the command automatically adds that remote repository under the name ìoriginî. So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It's important to note that the git fetch command pulls the data to your local repository & it doesn't automatically merge it with any of your work or modify what youíre currently working on. You have to merge it manually into your work when youíre ready.
  • If you have a branch set up to track a remote branch, you can use the git pull command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the git clone command automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you're currently working on.

Fetching and Pulling from Your Remotes

git fetch

e.g.

git fetch origin

Pushing to Your Remotes

git push origin master

Inspecting a remote

git remote show origin

Removing and Renaming Remotes

git remote rename pb paul
git remote rm paul

Tagging

Listing Your Tags

git tag
git tag -l 'v1.8.5*' ###  search with a particular pattern

Tags (Lightweight and annotated)

Annotated Tags

git tag -a <version_name> -m '<tag_message>'

e.g.

git tag -a v1.0 -m 'my version 1.0'

Show Tag information

git show v1.0

Lightweight tag

git tag v1.4-lw

Tag a previous commit

git tag -a <version_name> <abbreviated_index_checksum>

e.g.

git tag -a v1.2 9fceb02

Sharing Tags

git push origin

e.g.

git push origin v1.5
git push origin --tags ###  transfer all of your tags to the remote server that are not already there

Checking out Tags

Note: You can't really do that, instead, put a version of the repository in your working directory that looks like a specific tag.

###  Create a new branch version2 which has contents as of tag v2.0.0
git checkout -b version2 v2.0.0 

Config

Set up Aliases:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
###  Can do git unstage file instead of git reset HEAD file
git config --global alias.unstage 'reset HEAD --' 
###  see the last commit easily
git config --global alias.last 'log -1 HEAD' 

Branching

List all branches

git branch
git branch -v  ###  with last commit on each branch
git branch --merged ###  only merged branch
git branch --nomerged ###  only unmerged branch

Create a new Branch

git branch <new_branch_name>

Switch to branch

git checkout <branch_name>

e.g.

git checkout branch123 or git checkout master

Shorthand to checkout and create a branch

git checkout -b <branch_name>

Delete branch

git branch -d <branch_name>

Force Delete branch

Note: will delete even if there's unmerged changes

git branch -D <branch_name>

Git fetch and merge a branch

e.g. serverfix

git fetch origin
git merge origin/serverfix

Note: If you want your own serverfix branch that you can work on, you can base it off your remote branch

git checkout -b serverfix origin/serverfix

Tracking Branches

Note: Checking out a local branch from a remote branch automatically creates what is called a "tracking branch" (or sometimes an "upstream branch"). Tracking branches are local branches that have a direct relationship to a remote branch. If youíre on a tracking branch and type git pull, Git automatically knows which server to fetch from and branch to merge into.

git checkout --track origin/serverfix

Add a tracking to an existing local branch

git branch -u origin/serverfix

List tracking branches

git branch -vv

Deleting Remote Branches

git push origin --delete serverfix

Rebase

Rebasing

Note: However, there is another way: you can take the patch of the change that was introduced in C4 and reapply it on top of C3. In Git, this is called rebasing. With the rebase command, you can take all the changes that were committed on one branch and replay them on another one.

git checkout experiment
git rebase master
git checkout master
git merge experiment

# Check out the client branch, figure out the patches from the common ancestor of the client and server branches, and then replay them onto master
git rebase --onto master server client 
#  fast-forward your master branch
git checkout master; git merge client

###  You can rebase the server branch onto the master branch without having to check it out first by running git rebase [basebranch] [topicbranch] which checks out the topic branch (in this case, server) for you and replays it onto the base branch (master):
git rebase master server 
###  fast-forward the base branch (master):
git checkout master; git merge server 

IMPORTANT: Do not rebase commits that exist outside your repository.


Stashing

Saving changes before switching branch without commit (Stashing)

git stash
git stash save 
git stash -u 
###  Git will also stash any untracked files you have created
git stash --include-untracked 

See list of stash

git stash list

Apply latest stash

git stash apply

apply a specific stash

git stash apply stash@{2}

delete stash

git stash drop stash@{0}

pop stash

git stash pop

Cleaning working directory

git clean
git clean -d -n ###  -n option, which means ìdo a dry run and tell me what you would have removedî.

Create a branch from a stash

git stash branch testchanges

remove everything but save it in a stash.

git stash --all

Miscelleneous

Check for whitespace error

git diff --check

Get history of HEAD

git reflog

Get what was done in a branch yesterday

git show master@{yesterday}

Get all commits reachable by experiment that aren't reachable by master

git log master..experiment

See what you're about to push to a remote:

git log origin/master..HEAD

See all commits that are reachable from refA or refB but not from refC

git log refA refB ^refC
git log refA refB --not refC

Triple Dot: all the commits that are reachable by either of two references but not by both of them.

git log --left-right master...experiment

Git Grep (Search for occurences of string of chars)

git grep -n <phrase> ### will show line number
git grep --count <phrase> ###  will show count
git grep -p <phrase> ###  will find method
git grep --break --heading -n <phrase>  ###  more readable format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment