开发者

Staging modified files according to their status with git

I started to version control a directory as follows.

git init
git add . 

Then, I made modifications, adding/deleting/changing files. After I'm done with that, I run

git status

to get the following results.

#   (use "git add/rm ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
# deleted:    bash/_bash_profile
# deleted:    bash/_bashrc
# modified:   functions.sh
# modified:   readme.txt
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
# bash/bash/
# template/

Now, I can do exac开发者_运维技巧tly what 'git status' told me to do.

For deleted file run git rm.

  • git rm bash/bash_profile

For modified/added file run git add

  • git add functions.sh

My question is this : how can I automatize this? I see that git already knows what action to do on the modified files.

  • Is there a git command to do staging every necessary file according to its status? (remove or add)
  • If not, is there any easy way to staging all the files instead of running 'git add' or 'git rm' one by one?


Yep, there's an easy way:

git add -u

This will add all modifications in the current directory (so run it from the top level of your repo if you want everything). It will not, however, add any untracked files. If you want to do that too, you use:

git add .

which will add all files in the current directory. (That means it'll pick up modifications and untracked files, but not deletions like add -u).

Be sure and inspect git status before and after you do this, to make sure you're getting exactly what you want staged. If you prefer commit -a, be even more careful to inspect the status. It's very easy to add something you didn't mean to commit.

Have a look at the man page for more information.


If you pass the -a flag when you're committing, all unstaged changes will automatically be staged for you before committing:

git commit -a

If you don't want to commit yet, you can use the -u flag to git-add instead:

git add -u

This updates the staged file set will all changes to tracked files. It doesn't pick up new untracked files, however - for that, you'd want to just git add . again.


Enter git-add interactive mode:

git add -i

Then type u*q. This will update, stage all changes and quit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜