Git

How to Remove Commit From a Branch in Git

When people work together on the same project as a team, they often encounter many situations where it is required to add, remove, or update data in branches. It might be a hassle for a member to maintain the commit messages during the development. Git makes it easy for users to remove or update commits after and before pushing changes into Git remote directory.

In this study, we will briefly discuss the procedure of removing a commit from a branch in Git.

How to Remove Commit From a Branch in Git?

In Git, you can remove both un-pushed and pushed commits from a branch. Don’t know to do that? The below-given sections will assist you in this regard.

Note: For the demonstration, we will consider the scenario where we have created some files in the Git directory and committed changes to the repository. Later, it came to know that we had committed changes to the wrong directory, and these commits needed to be removed.

To do so, check out the below section.

Method 1: Remove Un-pushed Commit From a Branch in Git Repository

Follow the provided instructions to remove un-pushed changes from a branch of a Git repository.

Step 1: Open Git Bash

Press the “CTRL + Esc” keys to open the “Startup” menu and open the “Git Bash” terminal:

Step 2: Navigate to Git Directory

Next, move to the Git directory from where you want to remove the commit:

$ cd "C:\Users\nazma\Git\mari_khan\my_dir"

Step 3: Create File

Execute the following command to create a new file and place some text in it:

$ echo "file added" > File1.txt

As you can see, we have created a new file named “File1.txt” and added “file added” string in it:

Step 4: Check Status

Now, check the Git directory status:

$ git status

The given output signifies that some changes need to be committed:

Step 5: Add File to Git Directory

Next, run the following command to add the untracked created file in the Git directory:

$ git add File1.txt

Step 5: Commit Changes

Commit all changes into the Git Repository using the “git commit” command with the desired message:

$ git commit -m "1 file added"

Step 6: Remove Changes

Now, remove the commit using the below-provided command:

$ git reset --hard HEAD~1

Here, the “git reset” command will remove all changes, and “–hard HEAD~1” will move the HEAD to the previous commit:

Step 7: Verify Deleted Commit

Lastly, execute the “reflog” command to verify the deleted commit from Git repository:

$ git reflog

Below output indicates that, our commit is deleted successfully from the branch and placed in the log:

Let’s move to the next section to understand the procedure of removing commits from a Branch in Git after pushing.

Method 2: Remove Pushed Commit From a Branch in Git Repository

To remove the already pushed commits from a branch, check out the below provided method.

Step 1: Navigate to Git Directory

First, move to the Git directory from where you need to remove commit:

$ cd "C:\Users\nazma\Git\mari_khan\my_dir3"

Step 2: Create File

Create a new file and place some content in it:

$ echo "new file" > File2.txt

Step 3: Add File into Git Repository

Now, add the file into the Git directory with the help of the “git add” command:

$ git add File2.txt

Step 4: Check Git Repository Status

Check the Git repository status:

$ git status

Step 5: Commit Changes

Commit the changes into the Git repository with any message:

$ git commit -m "added new file"

Step 6: Git Push

Execute the “git push” command to push all commit changes into the remote repository:

$ git push

Step 7: Remove Changes

Remove the all of the pushed commits from the branch:

$ git push origin HEAD --force

The “HEAD –force” will move the HEAD forcefully and remove all changes. In our case, we have already removed commit changes from the branch:

Step 8: Verify Deleted Commit

Write out the “reflog” command to verify the deleted commit from the Git repository:

$ git reflog

Step 9: Remove Commit

Remove the commit from a branch in Git using “git reset”:

$ git reset --soft HEAD^

That’s all! We have demonstrated the procedure of removing commit from a branch in Git.

Conclusion

To remove un-pushed commits from a branch, create and add the file to a directory, commit changes, and run the “$ git reset –hard HEAD~1” command to reset all removed changes. For the next approach, push changes into the remote directory and run the “$ git reset –soft HEAD^” command to remove it from the branch. In this study, we have illustrated the method of removing a commit from a branch in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.