DEV Community

Cover image for 10 most common git errors, and how to fix them
Marcel Scognamiglio
Marcel Scognamiglio

Posted on

10 most common git errors, and how to fix them

Here's an unrequested golden tip: pratice git everyday!

1. I need to undone my pushed commit on local

Solution:

If you have not pushed your commit yet, you can simply undo it by resetting your HEAD to the previous commit:

git reset HEAD~

If you have already pushed your commit, you can undo it by reverting to the previous commit:

git revert HEAD

2. I accidentally committed something to the wrong branch

Solution:

You can either reset your HEAD to the previous commit or Cherry-pick the commit from the wrong branch and apply it to the correct branch.

3. I made a mistake in my last commit

Solution:

You can either reset your HEAD to the previous commit or amend your last commit with the correct changes.

Image description

4. I accidentally deleted a file

Solution:

You can either reset your HEAD to the previous commit or use git checkout to restore the deleted file from the last commit.

5. I accidentally deleted a branch

Solution:

You can use git checkout to create a new branch with the same commits as the deleted branch.

6. I accidentally erase a file from my branch

Solution:

If you have not pushed your branch yet, you can simply restore the file from the last commit.

If you have already pushed your branch, you can either reset your HEAD to the previous commit or use git checkout to restore the file from the last commit.

7. I want to merge two branches, but I don't want to commit all the changes

Solution:

You can use git cherry-pick to pick the commits you want to merge from one branch to another.

Learn more about cherry-pick here

8. I want to rebase my branch, but I don't want to lose my changes

Solution:

You can use git stash to save your changes, rebase your branch, and then apply your changes with git stash pop.

9. I want to rebase my branch, but I don't want to conflict with other branches

Solution:

You can use git rebase -i to interactively rebase your branch and resolve any conflicts before pushing your changes.

10. I want to reset my branch, but I don't want to lose my changes

Solution:

You can use git stash to save your changes, reset your branch, and then apply your changes with git stash pop.

Top comments (0)