More Git

by Administrator26. February 2010 12:49

Been using Git for a while now and getting to like it. Found out some more info about some of the commands.

The commands I gave previously for adding p4merge as the merge tool didn't do quite what I was expecting, and didn't actually work :)

After expecting git to use this merge tool whenever merges were needed it doesn't, you have to run it when auto merge fails using this command (discovered from this SO question):

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git mergetool[/sourcecode]

This will then go through each file with conflicts and run p4merge on them. Except that each time it tried to run p4merge it failed, so I guess the command was setup incorrectly.

I took a look at the .gitconfig file in my user folder and noticed that each of the file parameters to the p4merge.exe had got lots of slashes:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"][user]

name = Mike Hunt

email =


editor = notepad.exe


tool = p4merge

[mergetool "p4merge"]

cmd = p4merge.exe \\\"$BASE\\\" \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$MERGED\\\"[/sourcecode]

So I changed it to this:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]


name = Mike Hunt

email =


editor = notepad.exe


tool = p4merge

[mergetool "p4merge"]

cmd = p4merge.exe "$BASE" "$LOCAL" "$REMOTE" "$MERGED"


tool = p4merge

[difftool "p4merge"]

cmd = p4merge.exe "$LOCAL" "$REMOTE"


And now it runs p4merge for the merges and shows the files properly, and also note the entry for the diff tool, so it uses that for diffs as well, using the command:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git difftool[/sourcecode]

Other things that had me scratching my head for a while were:

1) If I delete a folder from the working set I couldn't stage the delete (or commit it), which I discovered was due to me not using:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git add -u .[/sourcecode]


[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git add -A .[/sourcecode]

-u (or --update) stages files based on what is in the index rather than the working set, so it won't add new files, but will update newly updated files that are already staged, and it will also remove files that were staged but have been removed from the working set.

-A (or --all) will do as -u but will also stage new files as well (lesson learned, "use git add -A ." from now on!) More details here

2) "git pull" performs a "git fetch" then a "git merge". The fetch fetches the remote branch to a local tracking branch based on the remote and branch you fetched, e.g. "git fetch unfuddle master" fetches the master branch from the unfuddle remote to a tracking branch locally called 'unfuddle/master'. The merge then merges that tracking branch to the local branch. e.g. if you are in the local branch "git merge unfuddle/master" will merge the changes.

3) If you mess up and want to reset your working files to the last commit:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git reset --hard HEAD[/sourcecode]

4) If you only want to reset individual files:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git checkout -- hello.cs[/sourcecode]

will reset the local working file hello.cs to the staged version


[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git checkout HEAD hello.cs[/sourcecode]

will reset the local working file to the HEAD version (last local commit)

Here are some handy links that I have come across for finding this info:


Git :)

by Administrator16. February 2010 11:47

With the weather being a bit rubbish the past few weekends I have been geeking out (in addition to playing Uncharted 1 & 2) and been learning wpf, mvvm pattern and I decided to start using an open source source code control system. After some deliberation I picked Git (I did look at subversion and mercurial), it seems to have gained popularity and I wanted to see what all the fuss was about and if it was written and used by Linus for the linux kernel then it should be good enough for me :)

After some investigation I found msysgit which is basically git on windows. It effectively installs a bash shell on your windows box with all the git commands. I also decided to use Unfuddle as an online store of my source code. Here are the steps I took to get setup, mainly so that I can find them again if I need to do this later…

Installing msysgit was pretty straight forward, I accepted the defaults and it installed just fine. (This was after discovering the right package to download, as that is not immediately obvious, but it’s here, the git package, rather than the msysgit package)

Setting up git to work with unfuddle and setting up ssh keys etc was the next step, and also setting up the repo to work with Visual Studio, which is basically setting up a .gitignore file to tell git not to include certain files and filetypes in the source code control

Once git is up and running you should then set up some global config, an ssh-key can be created using the following:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]ssh-keygen –C “” –t rsa[/sourcecode]

This will create the key in a folder called .ssh in your user folder. You then use this key to add to unfuddle (or github or wherever) so that you can be authorised against the remote repo.

Then set your username and email:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git config -–global “Your Name”

git config –-global "”[/sourcecode]

In your user folder there should be a .profile file, if there isn’t then you can use

[sourcecode language="bash" gutter="false" 1="tab-size="4"" toolbar="true"]touch .profile [/sourcecode]

which will create a file without a filename and only an extension (you can't do this in explorer) Then add this to it, which starts up the SSH Agent when bash is run, so that you only have to enter the ssh key password once.

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]SSH_ENV=$HOME/.ssh/environment

function start_agent {

echo "Initialising new SSH agent...";

/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"

echo succeeded

chmod 600 "${SSH_ENV}"

. "${SSH_ENV}" > /dev/null



# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then

. ${SSH_ENV} > /dev/null

#ps ${SSH_AGENT_PID} doesn't work under cywgin

ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {







This question on SO tells you how to change git’s default editor, I changed it from vi to notepad. (I use notepad2, but it’s installed to take over from notepad, so opening notepad.exe opens notepad2)

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git config –-global core.editor “notepad.exe”[/sourcecode]

This question on SO tells you how to change the default merge tool to p4merge, which is my merge tool of choice:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git config --global merge.tool p4merge

git config --global mergetool.p4merge.cmd ‘p4merge.exe \”$BASE\” \”$LOCAL\” \”$REMOTE\” \”$MERGED\”’[/sourcecode]

Then that should be the environment set up, after that its just a case of either creating a new repo or cloning one from unfuddle.

This page has the basic info on how to do it:

1) If you are starting a new repo, create it in unfuddle, then create a folder locally for it then:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git init[/sourcecode]

1b) you might also want to create a .gitignore file in the repo folder, so that you can tell git to only commit your source code, mine looks like this (you can tell where I ‘borrowed’ it from):

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]# .gitignore for .NET projects

# Thanks to Derick Bailey


# Additional Thanks to

# - Alexey Abramov

# Standard VS.NET and ReSharper Foo










# Other useful stuff










# Office Temp Files


# If you have a deploy folder



# Exclude ALL DLLs?


2) Associate the repo with the unfuddle remote repo:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git remote add unfuddle

git config remote.unfuddle.push refs/heads/master:refs/heads/master[/sourcecode]

2a) If you already have code in the repo you can clone it

3) Finally, write some code, and when you are happy and want to commit the changes to your local repo:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git add *

git commit[/sourcecode]

4) Then push the changes to the remote unfuddle repo:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git push unfuddle master[/sourcecode]


About the author

Something about the author

Month List

Page List