Git:推送后删除提交的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18357511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: Remove committed file after push
提问by kmaci
Is there a possibility to revert a committed file in Git? I've pushed a commit to GitHub and then I realized that there's a file which I didn't want to be pushed (I haven't finished the changes).
是否有可能在 Git 中恢复已提交的文件?我已经向 GitHub 推送了一个提交,然后我意识到有一个我不想被推送的文件(我还没有完成更改)。
回答by xor
update: added safer method
更新:添加了更安全的方法
preferred method:
首选方法:
check out the previous (unchanged) state of your file; notice the double dash
git checkout HEAD^ -- /path/to/file
commit it:
git commit -am "revert changes on this file, not finished with it yet"
push it, no force needed:
git push
get back to your unfinished work, again do (3 times arrow up):
git checkout HEAD^ -- /path/to/file
检查文件的先前(未更改)状态;注意双破折号
git checkout HEAD^ -- /path/to/file
提交它:
git commit -am "revert changes on this file, not finished with it yet"
推它,不需要用力:
git push
回到你未完成的工作,再做一次(向上箭头 3 次):
git checkout HEAD^ -- /path/to/file
effectively 'uncommitting':
有效地“不承诺”:
To modify the last commit of the repository HEAD, obfuscating your accidentally pushed work, while potentially running into a conflict with your colleague who may have pulled it already, and who will grow grey hair and lose lots of time trying to reconcile his local branch head with the central one:
修改存储库 HEAD 的最后一次提交,混淆您意外推送的工作,同时可能与您的同事发生冲突,他可能已经拉取了它,并且会长出白发并浪费大量时间试图协调他的本地分支负责人与中央:
To remove file change from last commit:
要从上次提交中删除文件更改:
to revert the file to the state before the last commit, do:
git checkout HEAD^ /path/to/file
to update the last commit with the reverted file, do:
git commit --amend
to push the updated commit to the repo, do:
git push -f
要将文件恢复到上次提交之前的状态,请执行以下操作:
git checkout HEAD^ /path/to/file
要使用恢复的文件更新最后一次提交,请执行以下操作:
git commit --amend
要将更新的提交推送到 repo,请执行以下操作:
git push -f
Really, consider using the preferred method mentioned before.
真的,考虑使用前面提到的首选方法。
回答by micoru
If you want to remove the file from the remote repo, first remove it from your project with --cache option and then push it:
如果要从远程存储库中删除文件,请首先使用 --cache 选项将其从项目中删除,然后推送它:
git rm --cache /path/to/file
git commit -am "Remove file"
git push
(This works even if the file was added to the remote repo some commits ago) Remember to add to .gitignore the file extensions that you don't want to push.
(即使文件在某些提交之前已添加到远程存储库,这也有效)请记住将您不想推送的文件扩展名添加到 .gitignore。
回答by René H?hle
You can revert only one file to a specified revision.
您只能将一个文件恢复到指定的修订版。
First you can check on which commits the file was changed.
首先,您可以检查文件已更改的提交。
git log path/to/file.txt
git log path/to/file.txt
Then you can checkout the file with the revision number.
然后,您可以使用修订号签出文件。
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /path/to/file.txt
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /path/to/file.txt
After that you can commit and push it again.
之后,您可以提交并再次推送它。
回答by JB Nizet
Reset the file in a correct state, commit, and push again.
将文件重置为正确的状态,提交并再次推送。
If you're sure nobody else has fetched your changes yet, you can use --amend
when committing, to modify your previous commit (i.e. rewrite history), and then push. I think you'll have to use the -f
option when pushing, to force the push, though.
如果您确定还没有其他人获取您的更改,您可以--amend
在提交时使用,修改您之前的提交(即重写历史记录),然后推送。不过,我认为您必须-f
在推动时使用该选项来强制推动。
回答by krish babu
Get the hash code of last commit.
git log
- Revert the commit
git revert <hash_code_from_git_log>
- Push the changes
git push
获取上次提交的哈希码。
git log
- 还原提交
git revert <hash_code_from_git_log>
- 推动变化
git push
check out in the GHR. you might get what ever you need, hope you this is useful
在 GHR 中查看。你可能会得到你需要的东西,希望你这是有用的