Skip links

Git commands, making your life easier

August 30, 2011 at 9:24 PM - by Freek Lijten - 0 comments

Tags: ,

At Procurios we've recently migrated from SVN to Git. Since we're new to Git we make mistakes or don't know the best or quickest way to solve a problem. Usually this results in trial and error or a Google session. I'd like to share some of the results in this post so a next Googler might have a more easy job :)

Diff between branches

If you want to see which files have changed between two different branches the following command should work

git diff --name-status master..branchname

This will result in a list of files and their status (As you'd get when you type git status, filepaths and lettercodes). If you want to see the diff output itself simple leave the --name-status part out. The same command can be used to see the difference between two revisions. The following command will give a list of modified and deleted files between the current HEAD and the revision with revisionhash:

git diff --name-status HEAD..revisionhash

In both case you might want to see what happened in a smaller portion of your tree. To do that use the following layout:

git diff --name-status master..branchname -- /path/to/some/folder/or/file.txt

The same works with a diff between revisions of course.

Bisect

Sometimes you need to debug an error which occurs in the current revision but not in a previous revision. Git provides a method for you to do this quite easily. you start by starting the bisect, defining the bad revision and a revision you're certain of it still works.

git bisect start
git bisect good 54cf98....
git bisect bad master

The last command will output something along the likes of the following:

Bisecting: a merge base must be tested
[50055b18f4a4aaa7c65a6a4b3c7ccdeb4c3c333f] "commit message here"

The Git bisect command now checked out the middle commit of the range of commits between good and bad. If you type git status you'll see you're in a newly created branch spefically for bisecting. Don't worry, in the end it will be removed as well.

If this commit isn't working properly as well you enter the command git bisect bad again, and the action will be repeated this time taking the middle of the range between good and the current checked out revision. If the current revision is working you do it the other way around typing git bisect good. Eventually you will find the revision that is broken, notify the opne responsible (or fix your own bug) and issue one final command:

git bisect reset

Your tree is now in the exact same state it was before we started bisecting. 

Change an author for a single commit

Changing a series of commits with the wrong author is fairly easy, I found a nice solution here. Changing the author of a single commit is covered a bit less on the internet, but the solution is equally "easy":

git rebase -i <a commit hash before the wrong commit>

Change the pick in front of the commits you'd like to change into edit.

git commit --amend --author="New Name <new-address@example.com>"
git rebase --continue

You've now changed the commits author for the commits you wanted to edit. Do this before pushing!

Add your own

Usefull suggestions that don't consist of git commit --amend and other well covered (on the internet) commands? Add a comment and I'll add them here :)

Share this post!

Comments

Leave a comment!

Italic and bold

*This is italic*, and _so is this_.
**This is bold**, and __so is this__.

Links

This is a link to [Procurios](http://www.procurios.nl).

Lists

A bulleted list can be made with:
- Minus-signs,
+ Add-signs,
* Or an asterisk.

A numbered list can be made with:
1. List item number 1.
2. List item number 2.

Quote

The text below creates a quote:
> This is the first line.
> This is the second line.

Code

A text block with code can be created. Prefix a line with four spaces and a code-block will be made.