Skip to content

Instantly share code, notes, and snippets.

@mikenairn
Last active September 26, 2015 01:07
Show Gist options
  • Save mikenairn/85df4f51d96b4f68ba33 to your computer and use it in GitHub Desktop.
Save mikenairn/85df4f51d96b4f68ba33 to your computer and use it in GitHub Desktop.
Git Commands

Common git commands

View local commits not in origin/master

git log origin/master..HEAD

Reset local repo back to remote repo head

git reset --hard origin/master

Create a new remote branch called "design"

git push origin design

Delete a local branch called "design"

git branch -D design

Delete a remote branch called "design"

git push origin :design

Delete a remote branch called design using refs/head. You may have to do this if you have a tag with the same name as the branch you are trying to delete. i.e. error: dst refspec fix-1-improve-binary-download matches more than one.

git push origin :refs/heads/design

Create a new local branch and switch to it

git checkout -b new-branch

Revert remote repo to local, may be dangerous if others have pulled changes

git push staging master:master --force

Show origin details, shows tracking status of branches

git remote show origin

Set the remote tracking branch for a local

git branch --set-upstream foo upstream/foo

Create a new local branch that tracks an existing remote

git checkout --track -b ${branch_name} origin/${branch_name}

or

git checkout ${branch_name}

Ruby method to get current branch name

def git_branch
    `git name-rev --name-only HEAD`
end

Merge a lot of commits into 1, based on diff between 2 branches

git fetch --all
git checkout alpha
git pull
git diff origin/master..HEAD >> alpha.diff
git checkout master
git checkout -b alpha_updated
git apply alpha.diff
<commit changes>
git push origin alpha_updated:alpha -f

###Housekeeping

Cleanup files, optimise repo

git gc

Delete all stale tracking branches under "origin"

git remote prune origin

#Do a dry run first
git remote prune -n origin

http://help.github.com/git-cheat-sheets/

###GitHub

https://help.github.com/articles/fork-a-repo

Add original repo as upstream so you can pull updates

git remote add upstream [email protected]:feedhenry/Wufoo-Template.git
git fetch upstream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment