This short Snipp shows you how to remove the last commit from the git repository.
1. Check the logs
First of all, check your local commit with messages before removing the last commit. Run the following command to check the logs in one line.
git log --oneline
2. Remove the last commit from the local branch
Now, Run the following command to remove the last commit and discard the changes from the local branch.
git reset --hard HEAD~1
Checkout the different ways to Undo commit before push in Git.
3. Update remote repository
At last, we will update the files and again need to push with force. It will delete the previous commit and keep a new one on the remote git repository.
git push origin <name_of_branch> -f
Now you can check the logs to verify the commit in the git repository.
A commit is like a snapshot of the Git repository at one point in time. Adding commits keep track of our progress and changes as we work. Git considers each commit change point or "save point". It is a point in the project you can go back.
You should make new commits often, based around logical units of change.
When we commit, we should always include a message.
By adding clear messages to each commit, it is easy for yourself (and others) to see what has changed and when.
The main workflow for a developer will follow these steps
- Create a branch for the changes to be made and give it a name according to the naming convention.
- Commit changes to the branch. There are often multiple commits for a bug fix or feature.
- Push the branch to the remote repository.
- Create a pull request so other team members can review the changes. To incorporate feedback, more commits might be needed and changes to be pushed.
- Complete the pull request and resolve any merge conflicts from changes other team members made after creating the branch.
Comments