Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to undo local commits and addition of file?

Tags:

git

bitbucket

I mistakenly added files to master and committed too. Otherwise I had to do it in a branch feature_x

Now I know there are commands like git reset. My issue is, if I reset things, will it undo my code too or just undo files and commit in master?

How do I avoid resetting things without losing code?

Update

Code has not been pushed on remote yet. When I tried it gave error:

error: src refspec feature_x does not match any.

like image 233
Volatil3 Avatar asked Mar 15 '23 22:03

Volatil3


1 Answers

You are currently on the master branch with the accidental commits X, Y and Z like this:

A - B - C - X - Y - Z  <- master

Now you can create a new branch where you currently are using

git branch feature-x

The feature-x branch will point to the same commit as the master branch, making it safe to reset the master branch to the earlier version:

git reset --hard HEAD~3    # Move master branch 3 commits back

You now have this

          X - Y - Z  <- feature-x
         /
A - B - C  <- master

Now you can checkout the feature-x branch again:

git checkout feature-x
like image 96
Sven Marnach Avatar answered Mar 18 '23 06:03

Sven Marnach