使用git根据其状态分段修改的文件 [英] Staging modified files according to their status with git

查看:108
本文介绍了使用git根据其状态分段修改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  git init 
git add。

然后,我进行了修改,添加/删除/更改文件。完成之后,我运行

  git status 

以获得以下结果。

 
#(使用git add / rm ...来更新将要提交的内容)
#(使用git checkout - ...放弃工作目录中的更改)

#deleted:bash / _bash_profile
#deleted:bash / _bashrc
#modified:functions.sh
#modified:readme.txt

#未记录文件:
#(使用git add ...来包含将要提交的内容)

#bash / bash /
#template /

现在,我可以做'git status'



对于已删除的文件,运行git rm。


  • git rm bash / bash_profile



修改/添加文件运行git add




  • git add functions.sh



我的问题是:我该如何自动化?我发现 git已经知道修改后的文件需要执行什么操作


  • 是否有git命令根据其状态来暂存每个必需的文件? (删除或添加)

  • 如果没有,是否有任何简单的方法来暂存所有文件,而不是一个接一个地运行'git add'或'git rm'? b $ b

解决方案

是的,有一个简单的方法:

  git add -u 

这会添加当前的所有修改目录(所以如果你想要所有的东西,可以从你的回购的顶层运行它)。但是,它不会添加任何未跟踪的文件。如果你也想这样做,你可以使用:

  git add。 

这将添加当前目录中的所有文件。 (这意味着它会提取修改和未跟踪的文件,但不会删除像 add -u )。



请确保在执行此操作之前和之后检查 git status ,以确保您获得了您想要执行的操作。如果您更喜欢 commit -a ,请仔细检查状态。添加你并不想要提交的内容非常简单。



查看手册页以获取更多信息。


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

这篇关于使用git根据其状态分段修改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆