DEV Community

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

Posted on

3 2

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.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more