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

如何只提交这些文件?


当前回答

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

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

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

其他回答

我猜想您希望将更改提交到一个分支,然后使这些更改在另一个分支中可见。在git中,当你改变分支时,你应该在HEAD顶部没有任何变化。

你只提交修改过的文件:

git commit [some files]

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

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]

如果你想让这个提交在两个分支上都可用,你就得这么做

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

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

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

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

获取你想要提交的文件列表

$ 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

将文件添加到登台

$ git add file1 file2

检查一下你所提交的内容

$ 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

使用提交消息提交文件

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

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

$ git reset --soft HEAD~1

如果你想撤销文件重新开始

$ git reset

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

我认为你也可以使用命令行:

git add -p

这允许您逐一查看所有未提交的文件,并选择是否提交它们。

然后,每次修改都会出现一些选项:我用“y”表示“是的,我想添加这个文件”,用“n”表示“不,我稍后会提交这个文件”。

Stage this hunk [y,n,q,a,d,K,g,/,e,?]?

至于其他选项(q,a,d,K,g,/,e,?)我不知道他们是做什么的,但我想如果你需要更深入地了解细节,“?”可能会帮助你。

这样做的好处是,您可以推动您的工作,然后创建一个新的分支,所有未提交的工作将跟随您在那个新分支上。非常有用,如果你已经编写了许多不同的东西,你实际上想要在github上重新组织你的工作。

希望这有帮助,我之前没有看到它被提到(如果它被提到,我的错)

你可以提交一些更新的文件,像这样:

git commit file1 file2 file5 -m "commit message"