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:
Step 3: Create File
Execute the following command to create a new file and place some text in it:
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:
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:
Step 5: Commit Changes
Commit all changes into the Git Repository using the “git commit” command with the desired message:
Step 6: Remove Changes
Now, remove the commit using the below-provided command:
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:
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:
Step 2: Create File
Create a new file and place some content in it:
Step 3: Add File into Git Repository
Now, add the file into the Git directory with the help of the “git add” command:
Step 4: Check Git Repository Status
Check the Git repository status:
Step 5: Commit Changes
Commit the changes into the Git repository with any message:
Step 6: Git Push
Execute the “git push” command to push all commit changes into the remote repository:
Step 7: Remove Changes
Remove the all of the pushed commits from the branch:
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:
Step 9: Remove Commit
Remove the commit from a branch in Git using “git reset”:
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.