是否有一种方法可以将提交从源分支重放到目标分支,但同时考虑到目标分支中.gitignore中所做的更改?
下面是一个场景:
假设我从主服务器中分离出来,开始提交文件,包括本应在.gitignore中但没有的.gitignore文件。有什么方法可以让我去主人那里,将"*.bin“提交给.gitignore,然后在重定位它时修复我的主题分支(或者其他一些自动操作)?通过修复,我的意思是删除任何.bin文件的更改集,这些更改现在被忽略了。这意味着1)如果提交在a.txt和foo.bin上发生了更改,那么它应该只提交a.txt;2)如果提交仅更改了foo.bin,则应该完全删除
目标是很容易地清除只有在拉请求时捕获的多个错误。
常规的git rebase不起作用。错误提交的文件仍然是提交的,即使在回购的(新的)线性历史中,gitignore模式在错误提交之前就已经存在了。
发布于 2021-10-12 19:36:24
是否有一种方法可以将提交从源分支重放到目标分支,但要考虑到目标分支中
.gitignore中所做的更改?
是的,这是可能的,然而,它需要一些脚本工作。(下面循环中的示例脚本甚至可能适用于您。)基本上,您将模拟重基,但在每个提交之间有几个步骤。算法如下所示:
.gitignore文件现在应该就位了。)列表中每个提交的--no-commit标志的提交,这样您就不会完成提交,然后reset将更改解除,然后再对其进行add返回,这将关注.gitignore 下面是一个工作示例bash脚本(将其保存为文件并在一个空目录中运行以进行测试):
#!/bin/bash -v
git init
git branch -m main # rename to main in case that's not your default branch name
echo asdf > asdf.txt && git add . && git commit -m "Add asdf.txt"
git branch test-branch
echo "*.log" > .gitignore && git add . && git commit -m "Add .gitignore"
git switch test-branch
echo abc > abc.txt
echo def > def.log
git add . && git commit -m "Add abc.txt and def.log"
echo ghi > ghi.txt
echo hij > hij.log
git add . && git commit -m "Add ghi.txt and hij.log"
git log --all --graph --name-only
# Get all the commit IDs that would be rebased if we rebased test-branch onto main,
# and store them into an array in reverse order
declare -a commitIDs=(`git log main..test-branch --pretty=format:"%h" --reverse`)
# reset test-branch to main
git reset --hard main
# loop through the commitIDs and cherry-pick each one without committing
for i in "${commitIDs[@]}"
do
echo "About to cherry-pick commit $i"
git cherry-pick $i --no-commit # this leaves the commit staged
git reset # unstage so we can commit with the .gitignore
git add . # this doesn't add ignored files
git commit --reuse-message=$i
done
git log --all --graph --name-only完成后,查看两个git log语句的输出。第一个有2个提交,每个提交中包含一个.txt和.log文件。第二个文件使用.gitignore文件从提交的文件中删除所有.log文件。
注意,我在重写时使用了以前的提交消息,因为这通常是您想要的。但是,我有意识地将提交命名为这样一种方式,以突出显示您不希望这样做的场景,例如,当您忽略的文件名在提交消息中指定时。
发布于 2021-10-12 18:15:44
不,您需要使用git rm删除您已经在跟踪的文件(或者如果您想清除它的所有跟踪git-filter-branch)。下面是gitignore手册页面中的相关部分(注意最后一句):
一个gitignore文件指定了Git应该忽略的故意未跟踪的文件。Git已经跟踪的文件不受
影响
https://stackoverflow.com/questions/69545149
复制相似问题