Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Friday, March 7, 2014

Cleaning up whitespace additions in git

I wrote a previous post on cleaning up end of line whitespace using git... But I've now run into a similar issue, the IDE used by several of the devlopers on my project
likes to change surrounding whitespace indentation of lines. This means if you change just one line, the indentation changes will swamp the real changes, making the diff unhappy to look at.


You can view a diff without showing whitespace changes in git using --ignore-space-change or --ignore--all-space. However unforunately the command

git rebase -f master --ignore-space-change

doesn't do what one might hope.


However all is not lost. You can get the same kind of behaviour by plumbing together git format-patch and git am.


git branch fixed 40caad7
git checkout fixed
git format-patch --stdout --ignore-all-space fixed..original | git am --ignore-whitespace

This takes the the changes from 40caad7 to original and applies them to the new fixed branch, but removes/fixes whitespace changes.


however this is not nice to use - so you can wrap it up into a little git alias like this: (newlines inserted for clarity - you'll need to remove them if you use it)


[alias]
 cleanwhite = "!f() { 
⏎  orig=$(git rev-parse HEAD) ; 
⏎  mergebase=$(git merge-base HEAD $1) ; 
⏎  git reset --hard $1 ; 
⏎  git format-patch --stdout --ignore-all-space $mergebase..$orig | git am --ignore-whitespace ; 
⏎ } ; f"

Now cleaning up your current base is as simple as

$ git cleanwhite master

Which will clean up all commits whitespace from your current commit back to where you diverged from master.

Thursday, April 19, 2012

Faking watch on OS X

Sometimes watching something change over time in your terminal is kinda helpful. Linux has a great utility for this called "watch". OS X doesn't some with this. Of course you can build it yourself if you want .. but that can mean a chunk of pain too... luckily bash comes with what you need. "while" and some ANSII escape sequences are enough to get you going.

Here was my first try:

The first problem with this for me was that in iterm2 clear just keeps adding to your terminal scrollback in some weird way. Luckily replacing this with some ANSII control sequences fixes that up nicely.

Now it doesn't add to the scrollback but it does blink every 4 second. I fixed this by storing the current and previous results in temp files and only clearing if they've changed.

Thursday, September 9, 2010

Fixing whitespace issues with git rebase

We're using a rebase and merge style approach for getting topic branches into our master branch.

Typical usage looks like this:
# Get the topic branch
git checkout topicbranch
# Sit it ontop of master
git rebase master
# Clean up all the commits so its nice and tidy
git rebase -i master
git checkout master
git merge topicbranch
Now if you add a --whitespace=fix to the first rebase stage, git will clean up all the odd whitespace at end of line stuff that git complains about and that editors can leave in there accidentally.

However this only works if your branch is not already branched off master. If your branch already contains the master head you'll just get the message:
Current branch topicbranch is up to date.

It turns out its easy to make the rebase happen:
git rebase -f master --whitespace=fix

Thursday, December 10, 2009

Merging Git Repositories

There's a bunch of ways of merging git repositories. Here's the one that I find easiest. Say I've developed some test features in directory/repository A that I want to start to use in directory/repository B


First I package everything into a temporary directory inside A in preparation for moving

cd /path/to/A     
mkdir A_merge     
git mv * A_merge     
git mv .gitignore A_merge/.gitignore
git commit -m"Preparation for merging A into B"


Then I create a branch in B for merging into
cd /path/to/B
git checkout -b merge_A_into_B
Next get the stuff from A's master branch and merge with the current branch.
git fetch file:///path/to/A 'refs/heads/*:refs/remotes/A/*'
git merge A/master
All the stuff that you need should now be in /path/to/B/A_merge.
Move this directory and its contents to whereever you want and commit.