简体   繁体   中英

Git - How to delete commits on Remote master branch

I am new to GIT and I am trying to wrap my head around its concepts. As a result I have some basic questions. Please pardon my naivety. I searched the internet but ended up getting more confused. Here are my questions:-

$ git log -n 10 --oneline
170daa2 (HEAD -> master, origin/master, origin/HEAD) Revert "Undo Pull request from feature branch test"
e3f0714 Revert pull request test This reverts commit c6b3b1a
c6b3b1a Revert commit
b72e92e Undo Pull request from feature branch test
3077d10 (origin/feature/test1, feature/test1) First commit
1ba77e8 Updated test1
8fceee2 Revert Commit 1
ecddd63 Test for remote status
6c5a094 Cherry Pick 1
3b66732 Fixed conflict

$ git log -n 6 --oneline feature/test1
3077d10 (origin/feature/test1, feature/test1) First commit
1ba77e8 Updated test1
8fceee2 Revert Commit 1
ecddd63 Test for remote status
6c5a094 Cherry Pick 1
3b66732 Fixed conflict
  • As seen from above, when I do git log while being on the master branch (first output above) why does it also show commits from my feature branch ( feature/test1 ) along with the ones in my master? When I explicitly specify the branch in the git log it then shows the commits pertaining to that branch only, right? Is it because git will show all commits (irrespective of any branch) combined together in chronological order? Can anyone please explain.

  • How to delete commits on a remote branch? -
    Suppose I have made 2 commits on feature branch and merged them to remote master via a pull request.
    Now, I realize those are bad commits and I want to cleanup and rollback to the state where my remote master (and the local master) branches were before the pull request was approved. I know I could use: git reset --hard HEAD~2
    to get rid of these 2 commits on the local master branch, but how do I get rid of them from the remote master branch since they were already merged to master? Is it that reset is used for local commits while revert is used for remote commits? What is the difference between these 2 commands? and how do I solve my question -

    • Do, I need to first reset master locally and then "force push" this state to remote master by running: git push origin +master ? Is this a correct approach? OR
    • Do I use git revert -m 1 <commit-hash> to undo commits from remote branch with the caveat that the revert will rollback and make a new commit. Is there any way to rollback on both remote and local master branch without making a new commit? Please suggest the best approach.

Since I am just getting started with git it may be possible that I have asked a wrong question. Either way please excuse my naivety. Any help is appreciated.

git revert -m 1 <commit-hash> 
git push -u origin master

the -m 1 is needed because your commit is actually lives on 2 diffrent branches (master and feature/test1) once you merged it. So you need to tell git what directions to choose when reverting the commit.

ok to answer your question briefly and simply:

If you want to correct an error that has been made on the Remote branch, then simply do the following:

1) checkout locally to the branch you want to push to master, basically it's master branch, checkout there using:

git checkout master

2) git log --oneline

to check all your commits (I know you know that)

3) inspect and find the last commit you want your master branch to point at

4) after finding the hash commit, do the following:

`git reset --hard

5) Now you need to force push to the master branch

Make sure you're still checked out on master and:

git push --force

and Voila, you're done, However, just make sure that if some other fellows will re-pull again from master, then they have to git pull --rebase or otherwise their git pull will fail if they were already on the old repository.

if you need to delete the commit (bad code or whatever ), is simply go back to the commit just before then copy the hash (sha-commit):

1) git reset --hard sha-commit

2) git reset --soft HEAD@{1}

3) this commit message is an example feel free to change it:

git commit -m "Reverting to the state of the project at sha-commit"

4) git push origin master

now your last commit in the master branch is on the commit you choose

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