Git Cheatsheet

Wed, Jul 6, 2016
Category: cheatsheet Tags: [git] [linux]

Basic Snapshotting

git-mv - Move or rename a file, a directory, or a symlink

git mv [-v] [-f] [-n] [-k] <source> <destination> rename
git mv [-v] [-f] [-n] [-k] <source> ... <destination directory> move into existing directory

git mv <source> <destination> # rename a directory

Branch Related

git branch [options] <branchname>

git branch # show all branches with current marked with *
git branch -vv # show all local and tracked remote branches
git checkout dev
git branch jesse
# creates branch named "jesse" off from "dev" branch
git checkout -b jesse
# creates and switch to branch "jesse"

Merge

git merge [options] [<commit>...]
git merge --abort may not be able to fully recover

git merge --no-ff
# no fast forwarding creates merge commit for FF
git merge jesse
# merge commits on jesse into current branch
git merge --no-ff 
# no fast forwarding creates merge commit for FF

Working with Remotes

git fetch [<options>] [<repository> [<refspec>]]

git fetch origin # fetch branches and/or tags from "origin" remote
git fetch -a # fetches from all remotes

git remote [-v | --verbose]
git remote add [-f] [--[no-]tags] <name> <url>

git remote # show all remote repositories
git remote -v # show all remote repositories with urls

# add remote repo and name it origin
git remote add origin https://github.com/user/repo.git
# runs git fetch origin immediately after
git remote add -f origin https://github.com/user/repo.git

Merge a Pull Request on Github

Step 1: From your project repository, bring in the changes and test.

git fetch origin
# not necessary if you just pushed local 
git checkout -b lname origin/rname 
git merge dev

Step 2: Merge the changes and update on GitHub.

git checkout dev
git merge --no-ff jesse # no fast fowarding, create a merge commit
git push origin dev

Pull All Remote Branches

# the first line creates local branch with same name tracking remote branch
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

Delete a Remote Branch

git push origin --delete <branchName>
comments powered by Disqus