git 如何只提交一些文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7239333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:44:06  来源:igfitidea点击:

How do I commit only some files?

gitgit-branchgit-commit

提问by Nips

I have two projects. One is the "official" project and the second is a light modification (some files added). I created new branch and I put new files to them. But in during development some files common to both branches is changed.

我有两个项目。一个是“官方”项目,第二个是轻度修改(添加了一些文件)。我创建了新分支并将新文件放入其中。但是在开发过程中,两个分支共有的一些文件发生了变化。

How do I commit only these files?

如何只提交这些文件?

回答by Alex

I suppose you want to commit the changes to one branch and then make those changes visible in the other branch. In git you should have no changes on top of HEAD when changing branches.

我想您想将更改提交到一个分支,然后使这些更改在另一个分支中可见。在 git 中,更改分支时,HEAD 顶部不应有任何更改。

You commit only the changed files by:

您可以通过以下方式仅提交更改的文件:

git commit [some files]

Or if you are sure that you have a clean staging area you can

或者,如果您确定您有一个干净的停靠区,您可以

git add [some files]       # add [some files] to staging area
git add [some more files]  # add [some more files] to staging area
git commit                 # commit [some files] and [some more files]

If you want to make that commit available on both branches you do

如果您想让该提交在两个分支上都可用,您可以

git stash                     # remove all changes from HEAD and save them somewhere else
git checkout <other-project>  # change branches
git cherry-pick <commit-id>   # pick a commit from ANY branch and apply it to the current
git checkout <first-project>  # change to the other branch
git stash pop                 # restore all changes again

回答by Abram

Get a list of files you want to commit

获取要提交的文件列表

$ git status

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:   file1
modified:   file2
modified:   file3
modified:   file4

Add the files to staging

将文件添加到暂存

$ git add file1 file2

Check to see what you are committing

检查以查看您正在提交的内容

$ git status

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   file1
    modified:   file2

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file3
    modified:   file4

Commit the files with a commit message

使用提交消息提交文件

$ git commit -m "Fixed files 1 and 2"

If you accidentally commit the wrong files

如果你不小心提交了错误的文件

$ git reset --soft HEAD~1

If you want to unstage the files and start over

如果您想取消暂存文件并重新开始

$ git reset

Unstaged changes after reset:
M file1
M file2
M file3
M file4

回答by Adriano Rivolli

You can commit some updated files, like this:

您可以提交一些更新的文件,如下所示:

git commit file1 file2 file5 -m "commit message"

回答by Tom Stickel

Some of this seems "incomplete"

其中一些似乎“不完整”

Groups of people are NOT going to know if they should use quotes etc..

一群人不会知道他们是否应该使用引号等。

Add1 specific file showing the location paths as well

添加1 个显示位置路径的特定文件

git add JobManager/Controllers/APIs/ProfileApiController.cs

Commit(remember, commit is local only, it is not affecting any other system)

提交(记住,提交只是本地的,不会影响任何其他系统)

git commit -m "your message"  

Push to remote repo

推送到远程仓库

git push  (this is after the commit and this attempts to Merge INTO the remote location you have instructed it to merge into)

Other answer(s) show the stashetc. which you sometimes will want to do

其他答案显示了您有时想要做的藏匿

回答by kaiser

If you have already staged files, simply unstage them:

如果您已经暂存文件,只需取消暂存它们:

git reset HEAD [file-name-A.ext] [file-name-B.ext]

Then add them bit by bit back in.

然后一点一点地把它们加回去。

回答by KamalDeep

Suppose you made changes to multiple files, like:

假设您对多个文件进行了更改,例如:

  • File1
  • File2
  • File3
  • File4
  • File5
  • 文件 1
  • 文件 2
  • 文件 3
  • 文件 4
  • 文件5

But you want to commit only changes of File1 and File3.

但是您只想提交 File1 和 File3 的更改。

There are two ways for doing this:

有两种方法可以做到这一点:

  1. Stage only these two files, using:

    git add file1 file2
    

    then, commit

    git commit -m "your message"
    

    then push

    git push
    
  2. Direct commit

    git commit file1 file3 -m "my message"
    

    then push

    git push
    
  1. 仅暂存这两个文件,使用:

    git add file1 file2
    

    然后,提交

    git commit -m "your message"
    

    然后推

    git push
    
  2. 直接提交

    git commit file1 file3 -m "my message"
    

    然后推

    git push
    

Actually first method is useful in case we are modifying files regularly and staging them (Large Projects), generally Live projects.

实际上,如果我们定期修改文件并暂存它们(大型项目),通常是实时项目,则第一种方法很有用。

But if we are modifying files and not staging them then we can do direct commit (Small projects).

但是如果我们正在修改文件而不是暂存它们,那么我们可以直接提交(小型项目)。

回答by James

This is a simple approach if you don't have much code changes:

如果您没有太多代码更改,这是一种简单的方法:

1. git stash
2. git stash apply
3. remove the files/code you don't want to commit
4. commit the remaining files/code you do want


Then if you want the code you removed (bits you didn't commit) in a separate commit or another branch, then while still on this branch do:

然后,如果您希望在单独的提交或另一个分支中删除您删除的代码(您没有提交的位),那么仍然在这个分支上时:

5. git stash apply
6. git stash

With step 5 as you already applied the stash and committed the code you did want in step 4, the diff and untracked in the newly applied stash is just the code you removed in step 3 before you committed in step 4.

在步骤 5 中,因为您已经应用了存储并提交了您在步骤 4 中想要的代码,新应用的存储中的差异和未跟踪只是您在步骤 4 中提交之前在步骤 3 中删除的代码。

As such step 6 is a stash of the code you didn't [want to] commit, as you probably don't really want to lose those changes right? So the new stash from step 6 can now be committed to this or any other branch by doing git stash apply on the correct branch and committing.

因此,第 6 步是您不想 [想要] 提交的代码的藏匿处,因为您可能真的不想丢失这些更改,对吗?因此,现在可以通过在正确的分支上执行 git stash apply 并提交来将步骤 6 中的新存储提交到该分支或任何其他分支。

Obviously this presumes you do the steps in one flow, if you stash at any other point in these steps you'll need to note the stash ref for each step above (rather than just basic stash and apply the most recent stash).

显然,这假定您在一个流程中执行这些步骤,如果您在这些步骤中的任何其他点进行存储,则需要注意上述每个步骤的存储引用(而不仅仅是基本存储并应用最新的存储)。