DEV Community

Cover image for Remove latest pushed Git-commits from remote repo
boolfalse
boolfalse

Posted on

Remove latest pushed Git-commits from remote repo

We’ll do an experiment on GitHub, but practically you can do this on other similar platforms as well. Our goal is to delete recently pushed commits from the remote repository branch.

Let’s assume we have a remote Git repository, and there we already have some commits pushed. Two developers maintaining the project:

  • Owen — the repository owner
  • Cole — the repository collaborator

In some moment of time project’s comment history on the master branch looks like this:

Fake page from GitHub commits history

As we can see, the first five (starting from the bottom) commits were pushed by the owner, and the next four commits were pushed by the collaborator.

Now the question:

Is there a way to remove the latest commits from the master branch?

Criterias:

  • master branch is the default branch of the project (it could be any branch)
  • no one can delete master branch (we can’t delete the branch and recreate it anyway)
  • last commits done by a collaborator of the project (it could be one commit or more by one or more collaborators)

Our goal:

Our goal is to clean up all the latest commits (in our case, from “Commit 6” to ”Commit 9") from the master branch.

Solution steps:

Let’s make sure, that we are on the master branch, and pull the remote state to the local machine:

git checkout master
git pull origin master
Enter fullscreen mode Exit fullscreen mode

Let’s remove all the latest commits on the local machine, which we want to delete. We’ll point the hash-code of the commit, that would be the HEAD commit after all the deletion (the hash of “Commit 5” in our case):

commits that will be removed are in red rectangle

git reset --soft 9d24c70
git reset
git checkout .
Enter fullscreen mode Exit fullscreen mode

Now we’ll create a new temporary branch and switch to it with current HEAD. We’ll remember to delete that right after the commits removal.

git checkout -b temp
Enter fullscreen mode Exit fullscreen mode

Let’s make some small changes (something, anything safe) to that temporary branch, commit them, and push to remote:

git add .
git commit -m "small changes"
git push origin temp
Enter fullscreen mode Exit fullscreen mode

Now, here’s the most important part of this article:
Switch back to the master branch, rebase the temporary branch on it, and push that to remote:

git checkout master
git rebase temp
git push origin master
Enter fullscreen mode Exit fullscreen mode

At this moment we already have master branch cleaned from the latest unnecessary commits.

master branch now is clean

We can be sure about having the actual result like this:

git log --graph --oneline --decorate --all
Enter fullscreen mode Exit fullscreen mode

Additionally, don’t forget to remove the temporary branch from the remote and from the local as well:

git push origin temp --delete
git branch -d temp
Enter fullscreen mode Exit fullscreen mode

That’s all.

Here’s the link of the appropriate GitHub Gist that you can check out as well.


If you liked this article, feel free to follow me here. 😇

To explore projects working with various modern technologies, you can follow me on GitHub, where I actively publicize much of my work.

For more info, you can visit my website boolfalse.com

Thank you !!!

Top comments (4)

Collapse
 
alexdevfx profile image
Alexey

Nice tip!

Collapse
 
boolfalse profile image
boolfalse

I appreciate your feedback. Feel free to recommend writing something interesting as an article for later.

Collapse
 
vandoeurnpin profile image
Vandoeurn Pin

Helpful!

Collapse
 
boolfalse profile image
boolfalse

I appreciate your feedback. Feel free to recommend writing something interesting as an article for later.