Git: How to undo local commits and add a file?

I mistakenly added files to masterand did this too. Otherwise, I had to do it in a branchfeature_x

Now I know that there are commands like git reset. My problem is that if I reset things, will it cancel my code too or just cancel the files and commit to master?

How to avoid resetting without losing code?

Update

The code has not yet been pressed on the remote control. When I tried, it gave an error:

error: src refspec feature_x does not match any.

+4
source share
5 answers

You are currently on the main branch with a random commit of X, Y, and Z as follows:

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

,

git branch feature-x

feature-x , , reset :

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

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

feature-x:

git checkout feature-x
+8

:

git reset --soft HEAD^

, .

.

0

, , git , discard. sourcetree 3- , , , , .

0

, master, master, . master , feature_x. , , feature_x, master, master, , master .

, git . ( , ), , ( ) , , .

,

feature_x

  • ( ), ,

    git stash
    
  • master

    git checkout master
    
  • feature_x, , master,

    git branch feature_x
    

    ( git checkout -b feature_x, master, feature_x)

  • , ,
    • master -
  • master last, master.

    master, bitbucket origin ( git remote -v), :

    • , :

      git fetch
      
    • ( master) , master:

      git reset --hard origin/master
      

    , , , master, master , master, , master. , , 5 master , ( ),

    git reset --hard master~5
    

    , (, , , ), (sha1 hash) master , `master

    git reset --hard <commit id>
    

    , a1b2c3d4

    git reset --hard a1b2c3d4
    

feature_x ...

... master

, , 1 2 (.. , master), feature_x master

git checkout feature_x
git merge --ff-only master

3 ( feature_x).

... , master

feature_x master, .

- master feature_x, 1 2 (.. , master):

git checkout feature_x
git merge master  # You might want to modify the suggested commit message,
                  # as 'merge master into feature_x' will be confusing when
                  # those commits won't be ancestors of 'master' later on.

, 2, 3.

feature_x, feature_x ( , feature_x, ) feature_x master ( , master feature_x ). , , git. , , , ( , ), (, ). ( ), , , , - .

0

, .

It should work.

-one
source

All Articles