git tips

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.

# Ignore bundler config
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# Ignore other unneeded files.
doc/
*.swp
*~
.project
.DS_Store
bundler_stubs/

Configure git to use BeyondCompare

diff

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

merge

$ 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”.

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.

git pull --rebase

github

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

$ ssh-keygen -t rsa -C "your_email@youremail.com"

Then copy the key from id_rsa.pub and register it on your github.com.

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

$ git remote add origin git@github.com:/first_app.git
$ git push -u origin master

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

$ git remote rm origin

Rename remote

$ git remote -v
appharbor       https://andrewchaa@appharbor.com/londonubf.git (fetch)
appharbor       https://andrewchaa@appharbor.com/londonubf.git (push)
origin  git@github.com:andrewchaa/LondonUbf.git (fetch)
origin  git@github.com:andrewchaa/LondonUbf.git (push)

$ git remote rename appharbor apphb

$ git remote -v
apphb   https://andrewchaa@appharbor.com/londonubf.git (fetch)
apphb   https://andrewchaa@appharbor.com/londonubf.git (push)
origin  git@github.com:andrewchaa/LondonUbf.git (fetch)
origin  git@github.com:andrewchaa/LondonUbf.git (push)

staging files

$ 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

from http://stackoverflow.com/a/52713/437961

$ 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.

git checkout -b modify-README
git mv README.rdoc README.md
mate README.md

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

git branch NuGetUpdate
git checkout NuGetUpdate

or this is one liner.

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.

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.

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.

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.

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!

$ git checkout ce0320e
$ git checkout -b <new_branch_name>
$ git checkout master
$ git merge new branch
$ git branch -d newbranch
$ git push ...

2 thoughts on “git tips

  1. I read a lot of interesting posts here. Probably
    you spend a lot of time writing, i know how to save you a lot
    of time, there is an online tool that creates readable,
    SEO friendly posts in seconds, just search in google – laranitas free content source

Leave a comment