如何在合并分支时添加 .gitignore 或在合并时忽略文件?
How to add .gitignore while merging branches or ignore files on merging?
我即将在 gitlab 上将我的开发分支合并到我的主分支。我错误地提交了很多我不想在我的主人身上拥有的文件(它们大约有 150 个文件)。我已经创建了一个合并请求,但还没有合并分支。有没有办法不合并所有这 150 个文件,只合并我想要的文件?合并时是否有任何方法可以添加 .gitignore 文件?我是 git 的新手,无法真正找到解决这个问题的方法。
在此先感谢您的帮助!
据我了解,您在 developer
分支上提交了一些不需要的文件。我假设你在上次提交时这样做了。您可以使用 git --soft
命令简单地 return 到上一个提交:
# we are on branch `developer`
git reset --soft HEAD~1
现在您回到了之前的提交,但仍然在暂存区中进行了所有更改(感谢 --soft
标志)。但是,您的 150 个不需要的文件还在暂存区中(使用 git status
来监控)。因此,您没有丢失任何真正重要的更改,但我们需要从临时区域中删除 150 个不需要的文件。
现在您有多种选择。您可以像这样取消暂存 150 个文件中的每一个:
git reset -- path/to/unwanted/file001
虽然这不是一件愉快的工作,特别是如果你必须重复这个 150 次。您可以简单地取消暂存所有文件:
git reset
并暂存要提交到 developer
分支的文件:
git add path/to/wanted/file01
第三个选项是使用 git reset
取消暂存所有文件,编辑您的 .gitignore
文件,最后将更改添加到暂存区域:
git add --all
您的 .gitignore
应该可以防止将 150 个不需要的文件添加到暂存区。
所以,总结一下:
git reset --soft HEAD~1 # or HEAD~N if you created the unwanted files N commits ago
git reset
/* edit your .gitignore file */
git add --all
/* commit the results */
我建议先创建一个新分支来尝试编辑,这样您就不会错误地从您一直在处理的分支中永久删除某些内容。你可以使用,
git checkout -b <new-branch>
任何未提交的更改都将随之而来,因此您可能希望在这样做之前确保您是最新的。然后在合并到 master 之前尝试以下操作。
查看 this answer,您可以删除单个文件,
To untrack a single file that has already been added/initialized to
your repository, i.e., stop tracking the file but not delete it from
your system use: git rm --cached filename
或者删除更新后的 gitignore 中的所有文件,
To untrack every file that is now in your .gitignore:
First commit any outstanding code changes, and then, run this command:
git rm -r --cached .
This removes any changed files from the
index(staging area), then just run:
git add .
Commit it:
git commit -m ".gitignore is now working"
我即将在 gitlab 上将我的开发分支合并到我的主分支。我错误地提交了很多我不想在我的主人身上拥有的文件(它们大约有 150 个文件)。我已经创建了一个合并请求,但还没有合并分支。有没有办法不合并所有这 150 个文件,只合并我想要的文件?合并时是否有任何方法可以添加 .gitignore 文件?我是 git 的新手,无法真正找到解决这个问题的方法。
在此先感谢您的帮助!
据我了解,您在 developer
分支上提交了一些不需要的文件。我假设你在上次提交时这样做了。您可以使用 git --soft
命令简单地 return 到上一个提交:
# we are on branch `developer`
git reset --soft HEAD~1
现在您回到了之前的提交,但仍然在暂存区中进行了所有更改(感谢 --soft
标志)。但是,您的 150 个不需要的文件还在暂存区中(使用 git status
来监控)。因此,您没有丢失任何真正重要的更改,但我们需要从临时区域中删除 150 个不需要的文件。
现在您有多种选择。您可以像这样取消暂存 150 个文件中的每一个:
git reset -- path/to/unwanted/file001
虽然这不是一件愉快的工作,特别是如果你必须重复这个 150 次。您可以简单地取消暂存所有文件:
git reset
并暂存要提交到 developer
分支的文件:
git add path/to/wanted/file01
第三个选项是使用 git reset
取消暂存所有文件,编辑您的 .gitignore
文件,最后将更改添加到暂存区域:
git add --all
您的 .gitignore
应该可以防止将 150 个不需要的文件添加到暂存区。
所以,总结一下:
git reset --soft HEAD~1 # or HEAD~N if you created the unwanted files N commits ago
git reset
/* edit your .gitignore file */
git add --all
/* commit the results */
我建议先创建一个新分支来尝试编辑,这样您就不会错误地从您一直在处理的分支中永久删除某些内容。你可以使用,
git checkout -b <new-branch>
任何未提交的更改都将随之而来,因此您可能希望在这样做之前确保您是最新的。然后在合并到 master 之前尝试以下操作。
查看 this answer,您可以删除单个文件,
To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use:
git rm --cached filename
或者删除更新后的 gitignore 中的所有文件,
To untrack every file that is now in your .gitignore:
First commit any outstanding code changes, and then, run this command:
git rm -r --cached .
This removes any changed files from the index(staging area), then just run:
git add .
Commit it:
git commit -m ".gitignore is now working"