How to git remove commit PROPERLY [Practical Examples]

Getting started with git remove commit

There are many ways to achieve git remove commit. You can pick the best solution depending on the challenge you are facing. For instance, do you want to remove

  1. the last commit?

Reset the HEAD

Advertisement
git reset --hard HEAD^
  1. a group of commits on a branch?

Interactively rebase the branch

git rebase -i HEAD~N

where N is the number of commits from the head

or

git squash the commits

git merge --squash <branch>
  1. all commits?

Navigate to the folder hosting the repo and delete .git subdirectory

rm -rf .git
  1. or remove the only (one) commit you have.
git update-ref -d HEAD

The challenging part about understanding git remove commits lies in having inadequate knowledge of git workflow, the reset command and branching. Here is what you should know about the three things.

Advertisement

 

Three things to understand before applying git remove commit

Git remove commit often involves the working tree, reset command and branching.

 

1. The working tree

Basic git workflow entails the working directory, the index and commit history. You only interact with workflow when tracking file changes. Otherwise, your project's files and folders reside in the untracked parent directory.

Running

git init

creates a .git sub-directory in the parent directory. The .git folder holds commit information.

Running the command git

add .

to the parent folder takes a snapshot of the files to the index, also called the staging area.

From there, you can permanently store the changes in the git database using unique hashes called commit or history. The latest commit in the history or the one you view intending to modify is called the HEAD.

You can undo git changes at the three levels:

Advertisement
  • from head to working directory,
  • from the commit history to the staging area, or
  • from the staging area to the working directory.

Since we start interacting with commit hashes past the staging area, most git remove commit actions will touch the HEAD or commit hash. The most typical commands to use then are git reset and git revert.

 

2. The reset command

Reset is the most familiar command to git remove commit. It occurs in three states: hard, soft and mixed. Git reset soft alters the HEAD commit, while git reset mixed unstages a file. Git reset hard entirely removes a commit from the history and deletes the associated files in the working directory.

Since deleting a commit history may cause conflict between local (not pushed) and remote (pushed) repos, it would be best to use the revert command instead of git reset to undo changes you plan to push.

 

3. Git branching

Other times you want to git remove commits in a different branch. A branch is a single development line. The initial branch is often referred to as the master or main.

Several developers, working on various features of the same project, can create branches, handle tasks in the branches and merge the changes back to the main branch.

While modifying the branches, the developers do commits, most of which you (as the main branch manager) are not interested in.

So, you want the commits removed at the branch level. Git rebase or merge can help restructure the history, as you will see in the practical part of this tutorial.

 

Lab setup to practice git remove commit

I am creating a repo called git_remove_commit on Github.

Advertisement

new repo to practice git remove commit

Copy the URL, clone it on the command line and cd into the repo.

How to git remove commit PROPERLY [Practical Examples]

cd git_remove_commit

We have one commit created from Github by adding the README.md file.

git log

repo state before git remove commit

Add two commits in the history.

touch second.txt

git add second.txt

git commit -m "second commit in main branch"

touch third.txt

git add third.txt

git commit -m "third commit in main branch"

Rechecking the history

git log

shows we have three commits to practice git remove commit from branch, last commit, or a bunch of commits.

How to git remove commit PROPERLY [Practical Examples]

 

Git remove the last commit by resetting the HEAD

It is simple to remove the last commit in history. You can reset HEAD by running the command

git reset --hard HEAD^

The caret ^ after HEAD implies the last commit referencing the HEAD.

The reset command removed the last commit with the id b3fcfc8eacf4b35ce9cc2034d6bcf2e41411243c. We remain with two commits, the HEAD being at commit id 2c1d719a986c3e3018ca2115b0d1971909a09d15

How to git remove commit PROPERLY [Practical Examples]

 

Git remove commit from branch

before push

We can git remove commit through interactive rebase or by squashing it.

Let us create and checkout a branch called rebased

git switch -c rebased

Create three commits.

touch feature1.txt

git add feature1.txt

git commit -m "first commit in rebased branch"

touch feature2.txt

git add feature2.txt

git commit -m "second commit in rebased branch"

touch feature3.txt

git add feature3.txt

git commit -m "third commit in rebased branch"

We have three new commits. Let us check out the main branch.

git checkout main

git log

We lack the rebased branch's commits

Let us update the changes in the main branch by rebasing the rebased branch.

git rebase rebased

We can remove the last three commits by interactively rebasing them.

git rebase -i HEAD~3

Our default text editor opens up.

How to git remove commit PROPERLY [Practical Examples]

Change the first two (from the bottom) pick with squash options.

How to git remove commit PROPERLY [Practical Examples]

Then close the text editor.

The text editor reopens, asking for a commit message.

I am commenting out the available messages and supplying it the message, "from rebased branch"

How to git remove commit PROPERLY [Practical Examples]

Rechecking the history

git log

shows our git remove commits mission on the three commits was successful, and we now have a new commit.

How to git remove commit PROPERLY [Practical Examples]

 

after push

You are free to delete the last commit from git remove from a branch before push section or proceed with it.

Let us git remove commit from branch after push by switching to rebased branch, then push the branch's three commits to create a branch in the remote.

We will then push changes before merging the branch on the command line.

git checkout rebased

git push --set-upstream origin rebased

Check remote and compare & pull request the changes until the commits are reflected below.

How to git remove commit PROPERLY [Practical Examples]

How to git remove commit PROPERLY [Practical Examples]

Check out the main branch and merge the rebased branch.

git checkout main

git merge --squash rebased

Edit the commit message, then push the changes with the -f flag

git commit -m "from rebased branch"

git push -f

Recheck the remote. The last three commits named first.. second.. third commit in rebased branch disappeared.

How to git remove commit PROPERLY [Practical Examples]

You can read more about squashing commits here.

 

Git remove commit from branch by clearing the history

Lastly, you may want to remove the only commit you have or git remove the first commit in the history. Either way, the following commands will serve you.

The command

git update-ref -d HEAD

clears all the commits and takes the changes to the staging area.

How to git remove commit PROPERLY [Practical Examples]

Let us commit the changes before clearing the repo.

git commit -m "git remove commit"

How to git remove commit PROPERLY [Practical Examples]

Move to the parent directory, and git remove the commits by deleting the .git sub-directory.

cd ..

rm -rf .git

Checking the status

git status

How to git remove commit PROPERLY [Practical Examples]

confirms we removed the repo with all the commits!

 

Summary

You can git remove commit by resetting the head, squashing the commits on a branch, or clearing the commit history. However, you should understand the working tree, reset, and branching before that.

Use the following commands to remove target commit(s) respectively:

Target CommitCommand ACommand B
lastgit reset --hard HEAD^
on branchgit rebase -i HEAD~Ngit merge --squash
allrm -rf .gitgit update-ref -d HEAD

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X