我有两个项目。一个是“官方”项目,第二个是轻微的修改(添加了一些文件)。我创建了一个新分支,并将新文件放入其中。但在开发过程中,一些两个分支共同的文件被更改。
如何仅提交这些文件?
如何仅提交这些文件?
我想你希望将更改提交到一个分支,然后在另一个分支中使这些更改可见。在 Git 中,当切换分支时,你不应该有任何未提交的修改。
你可以通过以下方式仅提交更改过的文件:
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 commit [some files]
,这意味着使用了 --only
选项)。第一个示例(git add [some files]
后跟git commit
)也会提交任何已暂存的其他更改。 - Lexikos$ 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" -- file1 file2
$ git reset --soft HEAD~1
$ git reset
Unstaged changes after reset:
M file1
M file2
M file3
M file4
git add file1 file2
然后执行了 git commit -m "Fixed files 1 and 2"
,但它提交了所有文件。 - Binar Webgit commit file1 file2 -m "Fixed files 1 and 2"
。 - UncaughtTypeError您可以提交一些更新的文件,例如:
git commit file1 file2 file5 -m "commit message"
假设您已对多个文件进行了更改,如下:
但是您只想提交File1和File3的更改。
有两种方法可以实现:
1.仅将这两个文件暂存,使用以下命令:
git add file1 file3
然后,提交
git commit -m "your message"
git push
2. 直接提交
git commit file1 file3 -m "your message"
然后推送
git push
实际上,第一种方法在我们经常修改文件并将它们推送到暂存区的情况下非常有用——>大型项目,通常是线上项目。
但如果我们修改文件但不进行推送,则可以直接提交——>小型项目。
--only
。然后,它将是与 git commit --only file1 --only file3 -m "my message"
相同的命令,或者使用快捷方式 git commit -o file1 -o file3 -m "my message"
。参考:https://git-scm.com/docs/git-commit - user有些内容看起来“不完整”
人们会不确定是否应该使用引号等等。
添加一个具体的文件,显示位置路径。
git add JobManager/Controllers/APIs/ProfileApiController.cs
提交(请记住,提交只在本地生效,不会影响任何其他系统)
git commit -m "your message"
推送到远程仓库
git push (this is after the commit and this attempts to Merge INTO the remote location you have instructed it to merge into)
其他答案展示了你有时需要执行的隐藏等操作。
如果您已经暂存文件,请简单地取消暂存它们:
git reset HEAD [file-name-A.ext] [file-name-B.ext]
然后逐个把它们再次添加回去。
git add -p
这使您能够逐个查看所有未提交的文件,并选择是否要提交它们。
然后,对于每个修改,您有一些选项可供选择:我使用“y”表示“是,我要添加此文件”,使用“n”表示“不,我稍后会提交此文件”。
Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
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