简体   繁体   中英

delete committed changes in git

I accidently pushed changes to my Dev branch that I now want to get rid of. Not reverting, but completely deleting them. Because of constraints set to the Dev branch, I cannot commit directly to that branch, but via a pull request. These are my steps: I created a new branch based on Dev , called Dev_Reset . Then:

git reset 4db52f3  --hard
HEAD is now at 4db52f3 last_changes_to_keep

git push origin -f

The Dev_Reset branch is then pushed to the remote repo. However, when I create a pull request, there are no differences to incorporate, because it says there are no differences between Dev_Reset and Dev . If I look at the history of the branches in Visual Studio, I see that both HEADS (local Dev and Dev_Reset ) are at commit 4db52f3 , but for some reason I cannot push this to my main Dev branch.

Also, in the history overview I still see all the commits that I wanted to delete using the git reset xxx --hard command, listed above the HEADS . No other branches are pointing to any of the commits that I want to delete.

I am aware of the dangers of git reset --hard , but currently I'm the only one working on this branch. How can I make all these commits go away and reset my main Dev branch to the correct commit?

Thanks

Edit (09 march 2020) As Igal S and LeGEC suggested below, I had to adjust the settings of the Dev branch, and then I could apply my changes here. The concerns regarding fetched branches by other coworkers were noted, my team has been informed. Currently, no one had pulled the respective branch recently. Thanks for the help!

The only way to delete those changes from a remote branch would be to do git reset --hard as you did and then force pushing them to the remote branch.

Assuming that your policy allows that and you have permissions to do that. Which system you are using for your remote git?

Pull requests are only used to add commits from branch to another. I am not familiar with any system that allows using pull requests to remove commits (ie rewrite history)

"erasing" the faulty commits require rewriting the history, you cannot do that through pull requests.


If it is really compulsory to erase those commits : you would have to ask someone with admin rights to temporarily disable the policy on the Dev branch, force push, and re-enable the policy.
This also implies informing other people (eg : a mail to your colleagues) and making sure everyone fixes his local history after the operation.

Otherwise :
you can only revert the modifications with new commits and a new pull request.


Do also note that : since the commits were published, you must assume that someone has fetched them.
If the mistake was to push private keys to the repo, for example, you can clean the history on the central repository ; but git alone will not erase the data from the hard drives of people who have access to the repo, and depending on the perimeter of the sharing (eg: if the central repo is a shared github repo), the actual fix would be to create new keys and invalidate the previous ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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