Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Tuesday, August 20, 2013

Installing meld or p4merge as diff / merge tool for Git on Windows

Meld

The following PowerShell uses Chocolatey to install meld and configures it as the diff and merge tool for Git. Note that meld is an officially supported diff tool in Git for Windows.
cinst meld

git config --global diff.tool meld
git config --global merge.tool meld

git config --global mergetool.meld.keeptemporaries false
git config --global mergetool.meld.trustExitCode false
git config --global mergetool.meld.keepbackup false

P4Merge (Perforce Merge)

The following PowerShell uses Chocolatey to install p4merge and configures it as the diff and merge tool for Git. Note that p4merge is an officially supported diff tool in Git for Windows.
cinst p4merge

git config --global diff.tool p4merge
git config --global merge.tool p4merge

git config --global mergetool.p4merge.keeptemporaries false
git config --global mergetool.p4merge.trustExitCode false
git config --global mergetool.p4merge.keepbackup false

Semantic Merge

If you're coding in C#, it may be worth using Semantic Merge (also installable via Chocolatey). Note that this set of commands sets up the PlasticSCM merge tool as the fall-back when not merging C#.
cinst SemanticMerge

git config --global diff.tool semantic
git config --global merge.tool semantic

$semanticMergeUserPath = Resolve-Path ~\AppData\local\PlasticSCM4\semanticmerge\semanticmergetool.exe
git config --global difftool.semantic.cmd ('''' + $semanticMergeUserPath + ''' -d=\"$LOCAL\" -s=\"$REMOTE\" -edt=\"mergetool.exe -d=\"\"@sourcefile\"\" -dn=\"\"@sourcesymbolic\"\" -s=\"\"@destinationfile\"\" -sn=\"\"@destinationsymbolic\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\"')
git config --global mergetool.semantic.cmd ('''' + $semanticMergeUserPath + ''' -b=\"$BASE\" -d=\"$LOCAL\" -s=\"$REMOTE\" -r=\"$MERGED\" -l=csharp -emt=\"mergetool.exe -b=\"\"@basefile\"\" -bn=\"\"@basesymbolic\"\" -s=\"\"@sourcefile\"\" -sn=\"\"@sourcesymbolic\"\" -d=\"\"@destinationfile\"\" -dn=\"\"@destinationsymbolic\"\" -r=\"\"@output\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\" -edt=\"mergetool.exe  -s=\"\"@sourcefile\"\" -sn=\"\"@sourcesymbolic\"\" -d=\"\"@destinationfile\"\" -dn=\"\"@destinationsymbolic\"\" -t=\"\"@filetype\"\" -i=\"\"@comparationmethod\"\" -e=\"\"@fileencoding\"\"\"')

git config --global mergetool.semantic.keeptemporaries false
git config --global mergetool.semantic.trustExitCode false
git config --global mergetool.semantic.keepbackup false

Other merge tools

You can see which diff tools are supported natively by git by running the following command:

git difftool --tool-help

You'll see something like the following result:

'git difftool --tool-<tool>' may be set to one of the following:
  gvimdiff
  gvimdiff2
  vimdiff
  vimdiff2

The following tools are valid, but not currently available:
  araxis
  bc3
  codecompare
  defaults
  deltawalker
  diffuse
  ecmerge
  emerge
  kdiff3
  kompare
  meld
  opendiff
  p4merge
  tkdiff
  vim
  xxdiff

Some of the tools listed above only work in a windowed
environment. If run in a terminal-only session, they will fail.

Wednesday, December 12, 2012

Git client configuration

This is the Git configuration I use for my local repositories.
[core]
 autocrlf = false
[push]
 default = simple
[rerere]
 enabled = true
[diff]
 tool = p4merge
[difftool "p4merge"]
 cmd = p4merge \"$LOCAL\" \"$REMOTE\"
[merge]
 defaultToUpstream = true
 tool = p4merge
[mergetool "p4merge"]
 cmd = p4merge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
 keepTemporaries = false
 trustExitCode = false
 keepBackup = false
[gc]
 auto = 0

Friday, October 19, 2012

After recently importing a Subversion repository into Git, along with the associated .gitignore file, I noticed that git status was performing poorly. The solution was to split the monolithic .gitignore file in the root folder into multiple .gitignore files spread throughout the repository.

I wrote the following PowerShell script to do so:
$content = type .gitignore | % { $_.Trim() } | ? { $_.StartsWith('/') } | % { $_.SubString(1) }
$content | % {
    $folder = split-path -Parent $_    
    if($folder.length -gt 0) {
        if(-not (test-path $folder)) {
            md $folder | Out-Null
        }
        $pattern = split-path -Leaf $_
        $newPattern = '/' + $pattern
        $ignoreFile = "$folder\.gitignore"
        $exists = $false
        if((test-path $ignoreFile ) -and (type $ignore) -notcontains $newPattern) {
            $exists = $true
        }
        if(!$exists) {
            Add-Content -Force -Path $ignoreFile $newPattern
        }
    }
}

Monday, October 8, 2012

Git Cookbook

This page lists some useful commands that I use with Git:
  • Show all branches that contain a commit. Useful to see which branches a topic branch has been merged into.
    • git branch --all --contains commit
  • Show which branches have not been merged into the current or specified branch.
    • git branch --all --no-merge [branch]
  • Pretty-print history tree
    • git log --oneline --graph --decorate --all
  • List all changes that will be sent with the next push
    • git log --branches --not --remotes
  • Update from remote
    • git fetch
    • git rebase -p
  • Get name of remote tracking branch for the current branch
    • git branch -vv | ? { $_ -match '^\*' } | % { $_ -match '\[([^:]+)' | Out-Null; $Matches[1] }
  • Get the most recently modified topic branch
    •  git for-each-ref --sort=-committerdate --format='%(refname)' refs/remotes/origin/topic | select -first 1

Wednesday, June 20, 2012

Get status of all Git repositories using PowerShell

The following PowerShell one-liner will get the status of all Git repositories:
dir -r -i .git -fo | % { pushd $_.fullname; cd ..; write-host -fore yellow (get-location).Path; git status; popd }
Note that this will not work for bare repositories.