Thursday, April 1, 2010

Using xargs with git

Sometimes, when I'm working on a project, I'll create a bunch of new files and realize that I have a ton of untracked stuff that I need to add to my git repository. Since generally only use git on the command line, it would be painful to copy and paste all of the untracked file names from the output of git status into separate git add commands.

The two commands I have found handy for dealing with this situation are git ls-files and xargs.

If you run the command:
git ls-files -o
It will show you all of the untracked files in your working directory, one file per line. A problem that you will run into here is that it also shows files in your .gitignore. To get around this issue, you just need another argument to specify your .gitignore:
git ls-files -o --exclude-per-directory=.gitignore
Now that you have all of the files you want to add, you just need to run git add on all of them. This is where xargs comes in handy. It will read from standard in, break it up on line endings and then feed each line as an argument into another command. Putting it all together, you get:
git ls-files -o --exclude-per-directory=.gitignore | xargs git add
That last command will add any untracked files to your git repository. The beautiful thing here is that we can also leverage some UNIX-y goodness if we want to as well. Let's say we're working on a project and we only want to commit some XSLT we have been working on. You can do this by throwing grep into the command chain:
git ls-files -o --exclude-per-directory=.gitignore | grep xslt | xargs git add
This will only add files that contain "xslt" in their names. This same approach comes in handy when you remove files from your working copy but forget to run git rm.

14 comments:

Michael A. Macasek said...

You can add something like this to the .gitconfig in your home dir as an alias:

gadd = !sh -c 'git ls-files -o --exclude-per-directory=.gitignore | grep "$0" | xargs git add'

so you can do:

git gadd xslt

葉雅慧 said...

出遊不拘名勝,有景就是好的 ..................................................

宗弘 said...

I do like ur article~!!!...................................................

鈺禎 said...

感謝分享--每天都要快樂喔................................

PorshaCoghlan梁子珠 said...

很用心的blog,推推哦 ........................................

RafaelHartin1 said...

謝謝您的分享~感恩唷!!..................................................

RooseveltHouchi嬌琪 said...

要像鐵鎚和釘子一樣,永遠向著定點努力。..................................................

Glennie9654 said...

nice to know you ~........................................

Londyn Cora said...

The persistence behind a bill is ratify is up to date regarding the pinched out and easier said than done wait horizontal under a understanding that provide a stanch path to citizenship term papers writing!!!

Harding Jamie said...

Magnificent! To tell the certainty this is a kind post concerning this topic. I got an unpolluted solution from here. Also I can refer to you all hwsolvers.com/programming_project here to get educational helps. However will wait for more posts from the blogger. Thanks a lot…….

yakenzu toby said...

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. ibcbet, judi bola, sbobet, agen bola, ibcbet, sbobet

shahbaz said...

thanks for this web it is very wonderful and very use full for me thanks for this........... Wholesale Towel Supplier

Legal Herbal Online said...

The perseverance behind a new bill can be ratify can be updated regarding the pinched away and also easier said than done wait around side to side below a new knowing that offer a stanch path to citizenship.Healthybodyinc 75%

jhon philips said...

I merely have a glimpse here and appear nice to appear out this world wide world wide journal. flush content writing hand and very cooperative internet location. i might like most of we've got got got a bent to tend to tend to tend to tend to organization neck of the woods unit to undertake to travel seeking out these diversity of things, here we've got got got a bent to tend to tend to tend to unit able to observe everything payday loans. I’m with the content standing associate degreed do deference him as associate honest provider. Thanks for your work and you too.