More Git

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 = someone@somewhere.com

[core]

editor = notepad.exe

[merge]

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"]

[user]

name = Mike Hunt

email = someone@somewhere.com

[core]

editor = notepad.exe

[merge]

tool = p4merge

[mergetool "p4merge"]

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

[diff]

tool = p4merge

[difftool "p4merge"]

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

[/sourcecode]

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]

or

[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

or

[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:

http://stackoverflow.com/questions/255202/how-do-i-view-git-diff-output-with-visual-diff-program

http://gitguru.com/2009/02/22/integrating-git-with-a-visual-merge-tool/

http://stackoverflow.com/questions/866262/p4merge-error

http://www.andymcintosh.com/?p=33

http://www.gitready.com/advanced/2009/02/11/pull-with-rebase.html

Comments are closed