I frequently diff a file, decide it's what I'll stage and then have to run the git add /long/path/to/file
to manually specify the file.
A usual work flow wo开发者_如何转开发uld go along the lines of:
git status
"oooh changes"
git diff /long/path/to/changed/file
"Yup, I remember that - commit time!"
git add /long/path/to/changed/file
Obviously this isn't the hardest thing in the world to do, it just gets a little tedious. I know I could go into interactive mode too, but that's never really fitted my work flow.
So I'm looking for some magic unix or git command where I can say "Hey, that file I just diffed - stage it please!".
Does something like that exist in either Git or Bash? Or is it something I need to build in a bash script?
Use
git add !$
More info: http://www.cyberciti.biz/faq/shell-call-last-argument-of-preceding-command/
You can make use of bash shell shortcuts. For example, type:
git add Alt .
The Alt . shortcut yanks the arguments from the previous command. You can also repeat it to search for arguments further back in your command history.
Bonus material...
If you're up for something more arcane, try the following:
[you@home]$ git diff /some/path/to/file
[you@home]$ ^diff^add
From the definitive guide to bash command line history:
Perhaps the most interesting event designator is the one in form '^string1^string2^' which takes the last command, replaces string1 with string2 and executes it.
Using ^diff^add
, while amusing, can be a pain to type. And it cannot be used in an alias. See my other answer for an alternative.
Add the following to your .bashrc
:
alias addiff='fc -s "git diff=git add"'
You now have a addiff
command which, when used after a git diff
, will covert it to a git add
.
[you@home]$ git diff /some/path/to/file
[you@home]$ addiff
fc (short for Fix Command) is a bash built-in command. When used without options, it allows you to edit your previous command before re-execution. Using the -s
options allows one to specify the pattern without having to resort to a text editor.
For a more generic usage of fc
, add alias r='fc -s'
to your .bashrc
. You can then use it as such:
[you@home]$ r # repeat the previous command
[you@home]$ r git # repeat the last command containing the word "git"
[you@home]$ r foo=bar # repeat the last command, replace "foo" with "bar"
You can try to use:
git add .
This command line adds all new files in the present work directory without specifying file names or file path.
精彩评论