Skip to content
David Chandek-Stark edited this page May 19, 2015 · 1 revision

Create new local branch

git checkout -b newbranch

Checkout and track an existing remote branch

# to discover remote branches, if necessary
git fetch 
# then checkout
git checkout -b branch origin/branch

# OR
git checkout -b branch
git branch --set-upstream branch origin/branch

Push a local branch to github (creates new remote branch)

git push origin branchname

Delete a local branch

# fully merged or in HEAD
git branch -d branchname 

# force delete
git branch -D branchname

Delete a remote branch

git push origin --delete branchname

Revert to a previous commit

http://stackoverflow.com/a/1895095

# reset the index to the desired tree
git reset 56e05fced

# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56e05fced"

# Update working copy to reflect the new commit
git reset --hard

Add an exception to a .gitignore rule

# Ignore these
/config/*.yml
# ... but not this one
!/config/predicate_mappings.yml

Get a submodule

git submodule init
git submodule update

Create a tag

# Tag version 0.2.0
git tag -a v0.2.0 -m 'DulHydra 0.2.0'
# Push tag v0.2.0 to github
git push origin v0.2.0

Pull in upstream changes

git fetch upstream
git merge upstream/master # merges upstream into local master branch

Squash commits

I want to squash last three commits on feature-branch into one commit:

git checkout develop
git pull --rebase origin
git checkout feature-branch
git rebase develop
git rebase --interactive HEAD~3
# After rebasing
get push -f origin feature-branch

See http://ndlib.github.io/practices/one-commit-per-pull-request/ for more detail.

Hot fixes (git flow procedure)

See http://nvie.com/posts/a-successful-git-branching-model/.

Example for create hotfix version 1.2.1

Branch from master

$ git checkout master
$ git pull
$ git checkout -b hotfix-1.2.1
[Fix stuff and commit to hotfix-1.2.1]

Merge into master

$ git checkout master
$ git pull
$ git merge --no-ff hotfix-1.2.1
$ git push origin master

Tag

$ git tag -a v1.2.1 -m "DulHydra 1.2.1"
$ git push origin v1.2.1

Merge into develop

$ git checkout develop
$ git pull
$ git merge --no-ff hotfix-1.2.1
$ git push origin develop