Git

Setting up email

https://help.github.com/articles/setting-your-email-in-git

git config --global user.email "<em>me@here.com</em>"
# Set a new email
git config --global user.email
# Verify the setting
# me@here.com

Fetch (checkout) remote branch

http://stackoverflow.com/questions/9537392/git-fetch-remote-branch

git checkout --track origin/daves_branch

Undo git merge with fast forward

http://blog.tplus1.com/blog/2011/09/23/undo-a-fast-forward-git-merge/

$ git reflog show master
$ git reset --keep master@{1}

Remove tag and delete from remote

http://nathanhoad.net/how-to-delete-a-remote-git-tag

git tag -d 12345
git push origin :refs/tags/12345

Merge commits from Github fork

http://stackoverflow.com/questions/4581740/pull-in-changes-from-a-github-fork

Pulling in a single commit would be a cherry-pick and would rewrite the commit ID (and mark you as the committer while retaining the author). The process is pretty straightforward, though:

git fetch git://github.com/user/project.git
git cherry-pick <SHA-COMMIT-ID>

Open current file in vim on Github

Assuming the remote is not master, but github:

  Gbrowse @github

Use Chromium as a default browser in git

https://github.com/tpope/vim-fugitive/issues/75

  $ git config --global web.browser chromium-browser

Hash of a commit before some hash

  git rev-parse hash^

http://stackoverflow.com/questions/949314/how-to-retrieve-the-hash-for-the-current-commit-in-git

Not to fast-forward merge with master

[branch "master"]
    remote = origin 
    merge = refs/heads/master
        mergeoptions = --no-commit --no-ff

What files changed between two revisions

http://stackoverflow.com/questions/1552340/git-show-all-changed-files-between-two-commits

git diff --name-only SHA1 SHA2

where you only need to include enough of the SHA to identify the commits. You can also do, for example

git diff --name-only HEAD~10 HEAD~5

to see the differences between the tenth latest commit and the fifth latest (or so).

Remove submodule

http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached path_to_submodule (no trailing slash).
  4. Commit and delete the now untracked submodule files.

Highlighting git branch

In bash.rc (via https://gist.github.com/938573)

### PROMPT ####################

function parse_git_dirty {
  $(git status 2> /dev/null && echo "*"
}

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* /[\1$(parse_git_dirty)]/"
}

COLOR_GRAY="\"
COLOR_WHITE="\"
COLOR_GREEN="\"
COLOR_RED="\"
COLOR_YELLOW="\"
COLOR_BLUE="\"

if /Users/$USER != $HOME ; then
    PROMPT_USERNAME="$COLOR_RED$USER$COLOR_WHITE "
else 
    PROMPT_USERNAME=""
fi

export PS1=$PROMPT_USERNAME'$(__git_ps1 "%s$(parse_git_dirty) ")\w$ '

Pull and merge

http://stackoverflow.com/questions/292357/whats-the-difference-between-git-pull-and-git-fetch

In the simplest terms, "git pull" does a "git fetch" followed by a "git merge".

You can do a "git fetch" at any time to update your local copy of a remote branch. This operation never changes any of your own branches and is safe to do without changing your working copy. I have even heard of people running "git fetch" periodically in a cron job in the background (although I wouldn't recommend doing this).

A "git pull" is what you would do to bring your repository up to date with a remote repository.

Revert the last commit

http://christoph.ruegg.name/blog/2010/5/5/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html

This is the easiest case. Let’s say we have a remote mathnet with branch master that currently points to commit dd61ab32. We want to remove the top commit. Translated to git terminology, we want to force the master branch of the mathnet remote repository to the parent of dd61ab32:

$ git push mathnet +dd61ab32^:master

Where git interprets x^ as the parent of x and + as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.

$ git reset HEAD^ --hard
$ git push mathnet -f

Set up gh-pages branch in a separate directory

https://gist.github.com/825950

Clone your "grandmaster" repository into the "gh-pages" folder (this will clone in the "master" branch), checkout the "gh-pages" branch, list the files (should have "index.html" and ".git") and then remove the "master" branch to avoid any confusion. Last step is to check that "master" branch was removed and only "gh-pages" branch is listed.

ichris:gh-pages $ git clone git@github.com:chrisjacob/grandmaster.git .
ichris:gh-pages $ git checkout origin/gh-pages -b gh-pages
ichris:gh-pages $ ls -la
ichris:gh-pages $ git branch -d master
ichris:gh-pages $ git branch

Move last commit from a wrong branch

http://www.williambharding.com/blog/git/git-move-commit-to-another-branch/

when I switch branches and forget to switch back before making my next commit

  git cherry-pick [commit ID]

Undo last commit in git

http://stackoverflow.com/questions/927358/git-undo-last-commit

Add/remove files to get things the way you want:

git rm classdir
git add sourcedir

Then amend the commit:

git commit --amend

The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place :)

Adding bitbucket git repo as a remote

  1. add public key to bitbucket: https://bitbucket.org/account/#ssh-keys
  2. use git not https link to add another remote, code below adds bitbucket with name safe and pushes there
git remote add safe git@bitbucket.org:user/repo
git push safe master

via http://news.ycombinator.com/item?id=3067795 http://confluence.atlassian.com/display/BITBUCKET/Importing+Code+into+your+Bitbucket+Repository

Make sure to use git@bitbucket.org:{username}/{reponame} instead of any https links bitbucket might give you. I was able to upload an existing repo I had by: 1) Uploading my SSH Public Key at https://bitbucket.org/account/ (i.e. the contents of ~/.ssh/id_rsa.pub) 2) Create a blank repo at Bitbucket 3) Adding a remote (git remote add bitbucket git@bitbucket.org:{username}/{reponame}.git) 4) Pushing to bitbucket (git push bitbucket master) The most important parts were adding my SSH Public Key, and making sure to use git@bitbucket.org:{username}/{reponame}.git instead of any https links you might see on bitbucket.

Adding remote git repository

http://caiustheory.com/adding-a-remote-to-existing-git-repo

cd existing_git_repo
git remote add origin git@github.com:caius/foo.git
git push origin master

Stop tracking files in git

http://source.kohlerville.com/2009/02/untrack-files-in-git/

git rm --cached filename

Revert (checkout) a single file

http://norbauer.com/notebooks/code/notes/git-revert-reset-a-single-file

This one is hard to find out there so here it is. If you have an uncommitted change (its only in your working copy) that you wish to revert (in SVN terms) to the copy in your latest commit, do the following:

git checkout filename

This will checkout the file from HEAD, overwriting your change. This command is also used to checkout branches, and you could happen to have a file with the same name as a branch. All is not lost, you will simply need to type:

git checkout -- filename

Branching

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

Basic Branching and Mergin - a chapter from Pro Git.

Sending tags to remote

Pushing tags to a remote repository You can create tags locally, but you probably want to send them to the remot repository as well:

# create a local tag "tagname" with the given message.
git tag -a "tagname" -m "message"
# send your tags to the remote repository "origin"
git push origin --tags
Last edited by Artem Dudarev, 2013-10-13 02:01:37. Edit