我如何只提交一些文件?

Nip*_*ips 234 git git-commit git-branch

我有两个项目.一个是"官方"项目,第二个是轻微修改(添加了一些文件).我创建了新分支,并将新文件添加到它们中.但在开发过程中,两个分支共有的一些文件会发生变化.

我如何只提交这些文件?

Ale*_*lex 240

我想你想将更改提交到一个分支,然后在另一个分支中显示这些更改.在git中,更改分支时,HEAD顶部不应该有任何更改.

您只提交已更改的文件:

git commit [some files]
Run Code Online (Sandbox Code Playgroud)

或者,如果你确定你有一个干净的临时区域,你可以

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]
Run Code Online (Sandbox Code Playgroud)

如果您想在两个分支上提供该提交,则可以

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
Run Code Online (Sandbox Code Playgroud)

  • 要字面上只提交**那些文件,即使已经进行了其他更改,也应该使用第二个例子(`git commit [some files]`,这意味着`--only`开关).第一个例子(`git add [some files]`后跟`git commit`)也将提交已经上演的任何其他更改. (22认同)
  • 我将有助于提供git commit [some files]的示例.比如你应该如何替换[some files],逗号,空格? (4认同)
  • @claudiu通常shell的东西是空格分隔的.如果你潜水得足够深,你可以改变它 (2认同)
  • 如果您可以添加一些示例,这个答案将会很棒。 (2认同)

Abr*_*ram 136

获取要提交的文件列表

$ 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
Run Code Online (Sandbox Code Playgroud)

将文件添加到暂存

$ git add file1 file2
Run Code Online (Sandbox Code Playgroud)

检查一下您的承诺

$ 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
Run Code Online (Sandbox Code Playgroud)

使用提交消息提交文件

$ git commit -m "Fixed files 1 and 2"
Run Code Online (Sandbox Code Playgroud)

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

$ git reset --soft HEAD~1
Run Code Online (Sandbox Code Playgroud)

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

$ git reset

Unstaged changes after reset:
M file1
M file2
M file3
M file4
Run Code Online (Sandbox Code Playgroud)


Adr*_*lli 75

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

git commit file1 file2 file5 -m "commit message"
Run Code Online (Sandbox Code Playgroud)


Tom*_*kel 21

其中一些似乎"不完整"

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

添加 1个特定文件,同时显示位置路径

git add JobManager/Controllers/APIs/ProfileApiController.cs
Run Code Online (Sandbox Code Playgroud)

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

git commit -m "your message"  
Run Code Online (Sandbox Code Playgroud)

推送到远程仓库

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

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


kai*_*ser 10

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

git reset HEAD [file-name-A.ext] [file-name-B.ext]
Run Code Online (Sandbox Code Playgroud)

然后一点一点地添加它们.


小智 8

假设您对多个文件进行了更改,例如:File1 File2 File3 File4 File5

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

有两种方法可以执行此操作:1>仅使用以下两个文件进行分段:git add file1 file2

然后,提交git commit -m"你的消息"

然后推git push

2>直接提交

git commit file1 file3 -m"我的消息"

然后推,git推

实际上,如果我们定期修改文件并暂存它们,那么第一种方法很有用 - >大型项目,通常是Live项目.但是如果我们正在修改文件而不是暂存它们,那么我们可以直接提交 - >小项目