# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix <sha1-of-merge>
# remove the incorrectly added file
git rm somefile.orig
# commit the amended merge
git commit --amend
# go back to the master branch
git checkout master
# replant the master branch onto the corrected merge
git rebase tmpfix
# delete the temporary branch
git branch -d tmpfix
只是为了把它添加到Charles Bailey的解决方案中,我只是使用了git rebase -i来从之前的提交中删除不需要的文件,它就像一个魅力。
步骤:< / p >
# Pick your commit with 'e'
$ git rebase -i
# Perform as many removes as necessary
$ git rm project/code/file.txt
# amend the commit
$ git commit --amend
# continue with rebase
$ git rebase --continue
然而< em > < / em >,如果,像最初的海报,你已经做了几次提交
要撤消更改的提交,仍然可以使用硬重置
修改它,但这样做也涉及到使用rebase。这里有一些步骤
你可以使用
来修改历史上的提交
# Create a new branch at the commit you want to amend
git checkout -b temp <commit>
# Amend the commit
git rm <file>
git commit --amend --no-edit
# Rebase your previous branch onto this new commit, starting from the old-commit
git rebase --preserve-merges --onto temp <old-commit> master
# Verify your changes
git diff master@{1}
解决方案3:非交互式Rebase
如果你只是想从历史记录中完全删除一个提交,这将是有效的:
# Create a new branch at the parent-commit of the commit that you want to remove
git branch temp <parent-commit>
# Rebase onto the parent-commit, starting from the commit-to-remove
git rebase --preserve-merges --onto temp <commit-to-remove> master
# Or use `-p` insteda of the longer `--preserve-merges`
git rebase -p --onto temp <commit-to-remove> master
# Verify your changes
git diff master@{1}
pick 00ddaac Add symlinks for executables
pick 03fa071 Set `push.default` to `simple`
pick 7668f34 Modify Bash config to use Homebrew recommended PATH
pick 475593a Add global .gitignore file for OS X
pick 1b7f496 Add alias for Dr Java to Bash config (OS X)
edit 00ddaac Add symlinks for executables
pick 03fa071 Set `push.default` to `simple`
接下来,输入git rebase --continue。如果你选择完全删除提交,
然后,这就是你需要做的所有事情(除了验证,请参阅最后一步
这个解决方案)。另一方面,如果您想修改提交,则使用git
将重新应用提交,然后暂停rebase
Stopped at 00ddaacab0a85d9989217dd9fe9e1b317ed069ac... Add symlinks
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
此时,您可以删除文件并修改提交,然后继续
变基:< / p >
git rm <file>
git commit --amend --no-edit
git rebase --continue
< p >就是这样。最后一步,是修改提交还是删除提交
完全正确,验证没有其他意外更改总是一个好主意
是通过改变你的分支在重基之前的状态来实现的:
git diff master@{1}
方案5:过滤分支
最后,如果你想彻底清除所有的痕迹,这个解决方案是最好的
一个文件的存在历史,没有其他的解决方案是相当
这个任务。< / p >
#!/bin/bash
if [[ $1 == "" ]]; then
echo "Usage: $0 FILE_OR_DIR [remote]";
echo "FILE_OR_DIR: the file or directory you want to remove from history"
echo "if 'remote' argument is set, it will also push to remote repository."
exit;
fi
FOLDERNAME_OR_FILENAME=$1;
#The important part starts here: ------------------------
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch $FOLDERNAME_OR_FILENAME" -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
if [[ $2 == "remote" ]]; then
git push --all --force
fi
echo "Done."
You should probably clone your repository first.
Remove your file from all branches history:
git filter-branch --tree-filter 'rm -f filename.orig' -- --all
Remove your file just from the current branch:
git filter-branch --tree-filter 'rm -f filename.orig' -- --HEAD
Lastly you should run to remove empty commits:
git filter-branch -f --prune-empty -- --all