我有两个项目。一个是“官方”项目,第二个是轻微的修改(添加了一些文件)。我创建了新的分支,并将新文件放入其中。但是在开发过程中,两个分支共用的一些文件被更改了。
如何只提交这些文件?
我有两个项目。一个是“官方”项目,第二个是轻微的修改(添加了一些文件)。我创建了新的分支,并将新文件放入其中。但是在开发过程中,两个分支共用的一些文件被更改了。
如何只提交这些文件?
当前回答
这是一个简单的方法,如果你没有太多的代码更改:
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
然后,如果你想在一个单独的提交或另一个分支中删除代码(你没有提交的部分),然后在这个分支上执行:
5. git stash apply
6. git stash
在第5步中,您已经应用了存储并提交了在第4步中需要的代码,新应用的存储中的diff和untracked就是在第3步中提交之前删除的代码。
因此,第6步是你不想提交的代码,因为你可能真的不想失去这些更改,对吗?因此,第6步中的新stash现在可以通过在正确的分支上执行git stash apply并提交来提交到这个或任何其他分支。
显然,这假设你在一个流程中执行这些步骤,如果你在这些步骤的任何其他点上进行隐藏,你需要注意上面每个步骤的隐藏引用(而不仅仅是基本的隐藏并应用最近的隐藏)。
其他回答
我猜想您希望将更改提交到一个分支,然后使这些更改在另一个分支中可见。在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]
然后一点一点地把它们放回去。
假设你对多个文件做了修改,比如:
File1 File2 File3 File4 File5
但是您只想提交对File1和File3的更改。
有两种方法:
1.只运行这两个文件,使用:
git add file1 file3
然后,提交
git commit -m "your message"
然后推,
git push
2.直接提交
git commit file1 file3 -m "your message"
然后推,
git push
实际上,第一种方法是有用的情况下,如果我们定期修改文件和分期它们->大型项目,通常是活动项目。 但如果我们正在修改文件,而不是暂存它们,那么我们可以直接提交
获取你想要提交的文件列表
$ 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 commit file1 file2 file5 -m "commit message"