简体   繁体   English

git push -f在bitbucket中留下不需要的日志

[英]git push -f leave unwanted log in bitbucket

I did a commit, pushed it, but later realised I made some mistake on the code, I do git reset HEAD~ , and recommit and push -f , I found there's an extra log in my PR. 我做了一个提交,把它推送了,但是后来意识到我在代码上犯了一些错误,我做了git reset HEAD~ ,然后重新提交并push -f ,我发现我的PR中还有一个额外的日志。 How do I get rid of that? 我该如何摆脱呢?

Let's say we have a remote origin with branch master that currently points to commit dd61ab32 . 比方说,我们有一个远程origin与分支master ,目前指向提交dd61ab32 We want to remove the top commit. 我们要删除top提交。 Translated to git terminology, we want to force the master branch of the origin remote repository to the parent of dd61ab32 : 转换为git术语,我们希望将原始远程存储库的master分支强制为dd61ab32parent dd61ab32

git push origin +dd61ab32^:master

Where git interprets x^ as the parent of x and + as a forced non-fastforward push. git将x^解释为x+的父项,并将其解释为强制的非快进推送。

If it is the last commit that you wanted to delete locally then: Two steps needed: 如果这是您要在本地删除的最后一次提交,则:​​需要两个步骤:

git reset HEAD^ --hard
git push origin -f

if its not the last commit you could try: 如果不是最后一次提交,则可以尝试:

git rebase -i commit_hash^

Rewriting history or rebasing if the branch has already been pushed is usually a bad idea and you may also prefer to use: 如果分支已经被推送,则重写历史记录或重新编制基准通常是个坏主意,您可能还希望使用:

git revert commit_hash

Source 资源

I would assume here that the problem is that you actually made a new commit on top of your branch, instead of amending the commit you wanted to rework. 我在这里假设问题是您实际上在分支的顶部进行了一次新的提交,而不是修改您要重做的提交。 So here is what you probably should have done: 所以这可能是您应该做的:

# make changes
git commit --amend
git push --force origin master

This should still leave you adding one new commit to the remote. 这仍然应该使您向远程添加一个新的提交。 However, in general force pushing a branch is bad news in Git, especially when other users might be sharing your branch, and might have already pulled it at some point. 但是,一般来说,在Git中强行推送分支是个坏消息,尤其是当其他用户可能正在共享您的分支,并且可能已经在某个时候将其拉出时。 In this case, a much safer approach is to just make a new commit and push that. 在这种情况下,一种更安全的方法是仅提交一个新的提交并将其推送。 Or, you could use git revert to undo a commit, if you wanted to do that. 或者,如果您想这样做,可以使用git revert撤消提交。

You can untrack the file with: 您可以使用以下方法取消跟踪文件:

git rm --cached <file_name>

Then you need to put it in the .gitignore file so you will not add it again. 然后,您需要将其放入.gitignore文件中,以便不再添加它。 If the file is created automatically and the name changes from build to build, you can use a wild card like *.log or target/* , for example. 如果文件是自动创建的,并且名称在build到build之间变化,则可以使用*.logtarget/*类的通配符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM