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.

38 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.

Crystal X Nasa said...

I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! Crystal X | Crystal X Jogja | Crystal x Nasa | Ciri-ciri Crystal X Asli | Pusat Crystal X Tegal | Pusat Crystal X Jogja

nova nasa said...

This is the kind of article I love find. I'm particularly amazed with how well you actually explored this material. I personally believe this is sound info and I also agree. Information about the health of the area femininity. We introduce crystal x useful for women to overcome the problem of femininity. More intormation here Crystal X Asli | Crystal X Asli Nasa | Jual Crystal X Asli | Jual Crystal X Asli Nasa |Jual Crystal X Asli Mengatasi & Menghilangkan Keputihan | Efek pemakaian crystal x

Dewi Nova said...

Good one. Mengupas Manfaat dan Bahaya Crystal X Very nice work. Jual crystal x Asli You've not exaggerated anything too much, you've kept it simple. It is good to see that. Jual crystal x Asli Nasa I hope your next articles will also be compromising of the same awesome quality as this article Cara Pemakaian Crystal X yang Benar | Crystal x tegal | Crystal x Asli | Bibit Bawor | Entropy Cream | Crystal x tegal

foride88 said...

White papers are extremely regular in the political field, however now these papers are viewed as an imperative apparatus in business. These papers term paper writing services can be posted on an organization's site and can likewise be sent through mail. These papers can likewise be posted on e-bulletins and presentations.

Ken Michale said...

This is a good story to read today! I like it! Friv | Kizi5 | Yepi Games

Sang Pemikir said...

There is lot of information and they are very innovative and informative. I have read the article very well and it seems to me awesome. Agen Bola

Zacky van Hardi said...

This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information. Thanks for this nice article. Agen Sbobet

Clifford Nivarre said...

I don't usually use xargs but I will gonna try this out because based on the comments they had a good time of using it. gain soundcloud comments

Ethan Harry said...

Really important written content. the information that you shown is hard to faith and many superbly i liked the way you afford things here

Accredited GED Online

jhon romeo said...

Title advances have the same highlights as a secured development, beside a single perspective. While secured credits don't outline the kind of protection that will suffice it, title propels especially oblige cars or whatever other vehicle to go about as security. payday loan

JON said...

Two or three affiliations will keep attempting and gather from you regardless of this so I have another suggestion if this happens. In the event that you get distinctive social event hollers toward you have taught them about your dedication mix you have to fax and/or send them an uproot it letter. title loans

JON said...

I got a weapons in my arrange set up together my own prerogative forward solicitation upwards for the delayed admit albeit before uproot analysiss nova scotia, I had gotten my own particular lessening. Quite often there is a halfways pointless also that you are doggy atf gun registration

jhon romeo said...


The certifiable fact in the matter is that being low-pay is not a commonsense figure who needs a development. There are different pay levels that end up strapping people. It isn't the dollar entirety as much as the organization of said pay.
Payday Loans

musa ibrahim said...

This is an imperative issue to consider before taking out attributes that oblige you to set up your auto or truck as protection. Right when increased consistently, the interest rate and costs can incorporate quickly. Really, a couple credit experts truly charge triple-digits in yearly side interest. Cash Advance

Samuel Moreland said...

Individual advances are credits given with no security. You need to delay a bit before you rush to take this supernatural credit. These advances are dubious and they are significantly more lavish than a credit given to its security. car title loans

adrita nirmitishonga said...

The second thing I did is the thing that really helped me which was to get a payday credit joining relationship to handle my commitment at a diminished whole and pay the payday credits back for me for my inspiration. It was one of the best choices I ever gathered and it was an exit plan from the payday credit web. www.approvedcashadvance.com/locations/jackson-MS/

sojib hossain said...

A key for the progress is that the borrower must have an unmistakable title to the credit. The borrower will be obliged to give records showing the commitment concerning auto at the season of sponsorship of advances. Payday Loans

ariyan arifin said...

The borrower may be asked for that hand over the vehicle at a predefined date and time, appropriately issuing him an opportunity to oust his advantages from the auto, that are not a bit of the auto guaranteed. Payday Loans San-diego

Chang bui thi said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write
Signature:
friv4
games2girls 2
Download baixar facebook movel

MUSA77 said...

Since the subprime credit business section is defenseless against unsafe practices utilized by a few development stars, you ought to dependably read the advancement assention intentionally, particularly the true blue revelations piece, to affirm you won't be gotten in a promise trap. Cash Advance

Facebook Descargar said...

I would like more information about this, because it is very nice., Thanks for sharing.
Signature:
download descargar facebook gratis para Android celular and download free descargar facebook apk en español, descargar facebook plus , descarga facebook

Whatsapp Descargar said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
Signature:
download free descargar whatsapp gratis and download baixar whatsapp gratis online and descargar whatsapp , baixar whatsapp

Frozen Juegos said...

I am very happy to read this. Appreciate your sharing.
Signature:
Jugar juegos de frozen en línea gratis, los nuevos de princesa de Disney juegos frozen - la princesa encantadora y linda. Divertirse frozen!

jhon romeo said...

Despite the likelihood that you are not prepared to totally settle the repayment total, it is judicious to pay off however much as could sensibly be normal on the repayment date. This will help to keep the measure of interest you owe to a base. A couple of associations may charge you additional charges for extending your development, you should check if this is the circumstance before you agree shockingly game plan. Cash Advance San-diego