Skip to content

Instantly share code, notes, and snippets.

@arunma
Forked from TomyJaya/git-cheatsheet.md
Last active April 21, 2018 09:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save arunma/fb9bd1a51a7080377339 to your computer and use it in GitHub Desktop.
Save arunma/fb9bd1a51a7080377339 to your computer and use it in GitHub Desktop.
git-cheatsheet
# 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 <URL>
e.g. git clone https://github.com/TomyJaya/git-real.git
# Checking the Status of Your Files
git status
# 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 .
# 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. (cached and staged are synonymous)
git diff --cached
git diff --staged
# 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 "<message>"
e.g. git commit -m "Initial Commit"
# Commit all modified files, by pass add to index or staging
git commit -a -m '<message>'
e.g. git commit -a -m 'added new benchmarks'
# Remove files
git rm <file>
e.g. git rm PROJECTS.md
# keep the file in your working tree but remove it from your staging area
git rm --cached <file>
e.g. git rm --cached README
# Renaming and Moving Files
git mv <file_from> <file_to>
e.g. git mv README.md README
# 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
git log -Sfunction_name # 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.
# Undoing Things (Changing the last Commit)
git commit -m 'initial commit'
git add forgotten_file
git commit --amend # You end up with a single commit ñ the second commit replaces the results of the first.
### 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
# Undo commit/ local commit -- nuke last commit and never see it again.
git reset --hard HEAD~1
# 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 a Modified File)
"git checkout -- <file>..." to discard changes in working directory
e.g. git checkout -- CONTRIBUTING.md
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.
# Show all remotes ( remote repositories: versions of your project that are hosted on the Internet or network somewhere. )
git remote
git remote -v # shows URL
# Adding remote repository
git remote add [shortname] [url]
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 [remote-name]
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
# 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 [tagname]
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 (can't really do that, instead, put a version of the repository in your working directory that looks like a specific tag):
git checkout -b version2 v2.0.0 # Create a new branch version2 which has contents as of tag v2.0.0
#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
git config --global alias.unstage 'reset HEAD --' # Can do git unstage fileA instead of git reset HEAD fileA
git config --global alias.last 'log -1 HEAD' # see the last commit easily
# 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 (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
# 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
# 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
# Rebasing
# 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
git rebase --onto master server client # 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 checkout master; git merge client # fast-forward your master branch
git rebase master server # 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 checkout master; git merge server # fast-forward the base branch (master):
### Do not rebase commits that exist outside your repository.
# 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
# to 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
# Saving changes before switching branch without commit (Stashing)
git stash
git stash save
git stash -u
git stash --include-untracked # Git will also stash any untracked files you have created
# 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
# 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