如何只提交一些文件?

发布于 2024-12-02 01:48:17 字数 103 浏览 1 评论 0原文

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

如何仅提交这些文件?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

最舍不得你 2024-12-09 01:48:17

我想您想将更改提交到一个分支,然后使这些更改在另一分支中可见。在 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

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:

git commit [some files]

Or if you are sure that you have a clean staging area you can

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]

If you want to make that commit available on both branches you do

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
坏尐絯 2024-12-09 01:48:17

获取要提交的文件列表

$ 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

Get a list of files you want to commit

$ 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

Add the files to staging

$ git add file1 file2

Check to see what you are committing

$ 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

Commit the files with a commit message

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

If you accidentally commit the wrong files

$ git reset --soft HEAD~1

If you want to unstage the files and start over

$ git reset

Unstaged changes after reset:
M file1
M file2
M file3
M file4
猥琐帝 2024-12-09 01:48:17

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

git commit file1 file2 file5 -m "commit message"

You can commit some updated files, like this:

git commit file1 file2 file5 -m "commit message"
老旧海报 2024-12-09 01:48:17

假设您对多个文件进行了更改,例如:

  • 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

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

Suppose you made changes to multiple files, like:

  • File1
  • File2
  • File3
  • File4
  • File5

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:

git add file1 file3

then, commit

git commit -m "your message"

then push,

git push

2.Direct commit

git commit file1 file3 -m "your message"

then push,

git 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

错々过的事 2024-12-09 01:48:17

其中一些似乎“不完整”,

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

添加1个显示位置路径的特定文件以及

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)

其他答案显示存储等,您有时会想要这样做

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

git add JobManager/Controllers/APIs/ProfileApiController.cs

Commit (remember, commit is local only, it is not affecting any other system)

git commit -m "your message"  

Push to remote repo

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

Other answer(s) show the stash etc. which you sometimes will want to do

以为你会在 2024-12-09 01:48:17

如果您已经暂存文件,只需取消暂存它们即可:

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

然后将它们一点一点添加回来。

If you have already staged files, simply unstage them:

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

Then add them bit by bit back in.

蘸点软妹酱 2024-12-09 01:48:17

我想你也可以使用命令行:

git add -p

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

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

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

至于其他选项(q,a,d,K,g,/,e,?),我不确定它们的作用,但我猜是“?”如果您需要更深入地了解细节,可能会对您有所帮助。

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

希望这会有所帮助,我之前没有看到它说过(如果提到的话,我的错)

I think you may also use the command line :

git add -p

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".

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

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)

来世叙缘 2024-12-09 01:48:17

如果您没有太多代码更改,这是一种简单的方法:

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 只是您在步骤 4 中提交之前在步骤 3 中删除的代码。

因此,步骤 6 是存储你没有的代码[想要]提交,因为您可能真的不想失去这些更改,对吗?因此,现在可以通过在正确的分支上执行 git stash apply 并提交,将步骤 6 中的新存储提交到此分支或任何其他分支。

显然,这假设您在一个流程中执行这些步骤,如果您在这些步骤中的任何其他点进行存储,您将需要记下上述每个步骤的存储引用(而不仅仅是基本存储并应用最新的存储)。

This is a simple approach if you don't have much code changes:

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

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:

5. git stash apply
6. git stash

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).

空心空情空意 2024-12-09 01:48:17

它是: 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

因此为了避免这种情况:

git add app/src/main/java/com/androminor/firstcompose/MainActivity.kt

我检查了所有答案。令我惊讶的是,他们半生不熟的回复给出了错误的输出。

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:

git add app/src/main/java/com/androminor/firstcompose/MainActivity.kt

I checked all of the answers. To my surprise they where half baked reply giving wrong output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文