git tips

4 minute read

I have used git for a year now on my personal projects, but it's still mysterious. I feel like I need a good gui like TortoiseHG. Probably Git Extension is the most mature project in guifying git.

Setting up your git environment

Set up can be a times-taking and boring job, but well, you can skip it. It's something you need to to properly, to make the whole experience pleasant!

An augmented .gitignore

This will ignore rails document, vim, emacs swap files, and Mac OS X Finder's DS_Store.

[sourcecode language="ruby"]
# Ignore bundler config

# Ignore the default SQLite database.

# Ignore all logfiles and tempfiles.

# Ignore other unneeded files.

Configure git to use BeyondCompare

[sourcecode language="bash"]
$ git config --global diff.tool bc3
$ git config --global difftool.bc3.path "C:\Program Files (x86)\Beyond Compare 3\bcomp.exe"

[sourcecode language="bash"]
$ git config --global merge.tool bc3
$ git config --global mergetool.bc3.path "C:\Program Files (x86)\Beyond Compare 3\bcomp.exe"

To launch a diff with BC3, use the command "git difftool" or "git difftool foofile.txt"

Line endings

If you use git on windows like me, it's better to configure it to "commit as-is, checkout as-is".

[sourcecode language="bash"]
git config --global core.autocrlf false

Pull behaviour

By default Git will merge remote changes with your local changes when pulling. This can result in an ugly commit graph that makes it difficult to track changes. Rebasing on the other hand results in a single commit line graph. Rebasing long-lived branches is not recommended.

The guidance is that when pulling; when you’re only making changes on the master branch (or any long-lived remote branch) always use rebase. If you’re working on a long-lived branch merge should be used when pulling in changes from the original branch or merging back to that branch.

[sourcecode language="bash"]
git pull --rebase


Generate a new ssh key on your machine. Go for default file name and type in your password.

[sourcecode language="bash"]
$ ssh-keygen -t rsa -C ""

Then copy the key from and register it on your

Create an app first on github. Push your changes to it.

[sourcecode language="bash"]
$ git remote add origin
$ git push -u origin master

Sometimes, you make a mistake while creating a remote origin. Then

[sourcecode language="bash"]
$ git remote rm origin

Rename remote

[sourcecode language="bash"]
$ git remote -v
appharbor (fetch)
appharbor (push)
origin (fetch)
origin (push)

$ git remote rename appharbor apphb

$ git remote -v
apphb (fetch)
apphb (push)
origin (fetch)
origin (push)


staging files

[sourcecode language="bash"]
$ git add . // This will stage all files
$ git add -u // This will stage delete files
you put the period at the end.

Discard unstaged files

[sourcecode language="bash"]
$ git checkout path/to/file/to/revert // for a specific file
$ git checkout -- . //This will delete all unstaged files. Make sure you put the period at the end.

Branch, edit, and merge

To edit readme file, you can checkout in to a new branch. It's kind of a good practice that you protect the main branch by branching your change out. You can delete the temp branch afterwards.

[sourcecode language="bash"]
git checkout -b modify-README
git mv README.rdoc

it commit -a -m 'improve the README file'
git branch -d modify-README

-a means commit all files that you changed.
if you use -D, instead of -d, it will delete the branch even though you haven’t merged in the changes.

Creating a branch
[sourcecode language="bash"]
git branch NuGetUpdate
git checkout NuGetUpdate

or this is one liner.
[sourcecode language="bash"]
git checkout -b NuGetUpdate

Creating a remote branch

The title is a bit misleading. You just create a local branch and push it remotely, then the remote branch is created.

[sourcecode language="bash"]
push <remote-name> <branch-name> // syntax

git branch 1.0-dev
git checkout 1.0-dev
git push origin 1.0-dev

Caching your credential

If you have to type in your username and password to push your change to github, your fingres and wrists will suffer. You can cache your credential for a certain period.

I cached it for 5 hours. If you want an hour, use 3600.

[sourcecode language="bash"]
git config --global credential.helper 'cache --timeout=18000'

ignore ssl certificate error

when you commit to your company githup repository that doesn't have signed certificate, unfortunately, you get an error. You can get around it by turning off the warning.

[sourcecode language="bash"]
git config – –global http.sslVerify false

Git commit against tag with no branch

I honestly panicked. I wasn't aware that the working directory was attached to any branch. I added and committed my changes (which took an hour of toil). Then it wasn't in any branch. I checked out to master. All the changes seemed to be lost!

The saviour was git reflog. You can find your commit SHA1, and checkout with it.

[sourcecode language="bash"]
D:\dev\files>git reflog
WARNING: terminal is not fully functional
ce0320e HEAD@{0}: checkout: moving from tempbranch to NuGetUpdate
ce0320e HEAD@{1}: checkout: moving from NuGetUpdate to tempbranch
ce0320e HEAD@{2}: merge tempbranch: Fast-forward
1b355c7 HEAD@{3}: checkout: moving from tempbranch to NuGetUpdate
ce0320e HEAD@{10}: commit: updated references with the downgraded framework

Then you can check it out, create a new branch, and merge the branch to master or whatever, phew!

[sourcecode language="bash"]
$ git checkout ce0320e
$ git checkout -b <new_branch_name>
$ git checkout master
$ git merge new branch
$ git branch -d newbranch
$ git push ...