如何只提交一些文件?
我有两个项目。一个是“官方”项目,第二个是轻微修改(添加了一些文件)。我创建了新分支并将新文件放入其中。但在开发过程中,两个分支共有的一些文件发生了变化。
如何仅提交这些文件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我想您想将更改提交到一个分支,然后使这些更改在另一分支中可见。在 git 中,更改分支时,HEAD 上不应有任何更改。
您可以通过以下方式仅提交更改的文件:
或者,如果您确定您有一个干净的暂存区域,则可以
如果您想让该提交在两个分支上都可用,您可以这样做
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.
You commit only the changed files by:
Or if you are sure that you have a clean staging area you can
If you want to make that commit available on both branches you do
获取要提交的文件列表
将文件添加到暂存
检查您要提交的内容
使用提交消息
如果您不小心提交了错误的文件
如果您想取消暂存文件并重新开始
Get a list of files you want to commit
Add the files to staging
Check to see what you are committing
Commit the files with a commit message
If you accidentally commit the wrong files
If you want to unstage the files and start over
您可以提交一些更新的文件,如下所示:
You can commit some updated files, like this:
假设您对多个文件进行了更改,例如:
但您只想提交 File1 和 File3 的更改。
有两种方法可以做到这一点:
1.仅暂存这两个文件,使用:
然后,提交
然后推送,
2.直接提交
然后推送,
实际上第一种方法很有用,如果我们定期修改文件并暂存它们 -->大型项目,通常是实时项目。
但是如果我们正在修改文件而不是暂存它们,那么我们可以直接提交 -->小项目
Suppose you made changes to multiple files, like:
But you want to commit only changes of File1 and File3.
There are two ways for doing this:
1.Stage only these two files, using:
then, commit
then push,
2.Direct commit
then push,
Actually first method is useful in case if 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
其中一些似乎“不完整”,
一群人不会知道他们是否应该使用引号等。
添加1个显示位置路径的特定文件以及
提交(记住,提交仅在本地,它不会影响任何其他系统)
推送到远程存储库
其他答案显示存储等,您有时会想要这样做
Some of this seems "incomplete"
Groups of people are NOT going to know if they should use quotes etc..
Add 1 specific file showing the location paths as well
Commit (remember, commit is local only, it is not affecting any other system)
Push to remote repo
Other answer(s) show the stash etc. which you sometimes will want to do
如果您已经暂存文件,只需取消暂存它们即可:
然后将它们一点一点添加回来。
If you have already staged files, simply unstage them:
Then add them bit by bit back in.
我想你也可以使用命令行:
这允许你逐一查看所有未提交的文件,并选择是否要提交它们。
然后,每次修改都会出现一些选项:我使用“y”表示“是的,我想添加此文件”,使用“n”表示“否,我稍后会提交此文件”。
至于其他选项(q,a,d,K,g,/,e,?),我不确定它们的作用,但我猜是“?”如果您需要更深入地了解细节,可能会对您有所帮助。
这样做的好处是,您可以推送您的工作,然后创建一个新分支,所有未提交的工作将在该新分支上跟随您。如果您已经编码了许多不同的东西并且您实际上想在推送之前在 github 上重新组织您的工作,那么非常有用。
希望这会有所帮助,我之前没有看到它说过(如果提到的话,我的错)
I think you may also use the command line :
This allows you to review all your uncommited files, one by one and choose if you want to commit them or not.
Then you have some options that will come up for each modification: I use the "y" for "yes I want to add this file" and the "n" for "no, I will commit this one later".
As for the other options which are ( q,a,d,K,g,/,e,? ), I'm not sure what they do, but I guess the "?" might help you out if you need to go deeper into details.
The great thing about this is that you can then push your work, and create a new branch after and all the uncommited work will follow you on that new branch. Very useful if you have coded many different things and that you actually want to reorganise your work on github before pushing it.
Hope this helps, I have not seen it said previously (if it was mentionned, my bad)
如果您没有太多代码更改,这是一种简单的方法:
然后,如果您希望在单独的提交或另一个分支中删除您删除的代码(未提交的位),那么仍然在此分支上执行以下操作:
使用步骤 5由于您已经应用了存储并在步骤 4 中提交了您确实想要的代码,因此新应用的存储中的 diff 和 untracked 只是您在步骤 4 中提交之前在步骤 3 中删除的代码。
因此,步骤 6 是存储你没有的代码[想要]提交,因为您可能真的不想失去这些更改,对吗?因此,现在可以通过在正确的分支上执行 git stash apply 并提交,将步骤 6 中的新存储提交到此分支或任何其他分支。
显然,这假设您在一个流程中执行这些步骤,如果您在这些步骤中的任何其他点进行存储,您将需要记下上述每个步骤的存储引用(而不仅仅是基本存储并应用最新的存储)。
This is a simple approach if you don't have much code changes:
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:
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.
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.
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).
它是:
git add full path of your file with file name
假设我想添加一个文件 MainActivity.kt 那么如果我这样做
git add MainActivity.kt
它将抛出错误,提示fatal: pathspec 'MainActivity.kt did not match any files
因此为了避免这种情况:
我检查了所有答案。令我惊讶的是,他们半生不熟的回复给出了错误的输出。
it is:
git add full path of your file with file name
suppose I want to add a file MainActivity.kt then If i do
git add MainActivity.kt
it will throw error sayingfatal: pathspec 'MainActivity.kt did not match any files
so to avoid that:
I checked all of the answers. To my surprise they where half baked reply giving wrong output.