简体   繁体   中英

How to go back to previous uncomitted state after git pull

I am working on a feature-branch alongside with my colleague. It's been a few days that I that have committed my changes.

Yesterday he has committed his changes and that changed the structure of the project we were working on but didn't harm my changes.

I have pulled those changes and made a commit with mine.Upon testing later I realized its not working the way I want it to be, so I want to go back to the state before that pull.

I cannot go back to my previous commit since by doing that my latest changes will be lost.

I tried git reflog which show me this output :

08aed28 HEAD@{0}: commit: Added support for database
4cf0fe8 HEAD@{1}: pull: Fast-forward
d5e2930 HEAD@{2}: commit: Added logs

I did git reset --hard HEAD@{1} but that will not take me to previous state.

What should I do ?

To continue working with your changes on a older commit I would suggest you the following chain of commands - I'll assume you are using the command line interface.

  1. Find the commit you want to "start" work from (let's say the commit hash is 123abc )
  2. Create a branch which points to that commit (let's call it test ) - git branch test 123abc
  3. Check the new branch out - git checkout test
    (You can combine this two steps by using git checkout -b test 123abc )
  4. Use git cherry-pick to regain your new changes

cherry-pick ( documentation )

You can use cherry-pick to (quote documentation) " Apply the changes introduced by some existing commits ".

In your case you could now simply use a command similar to this one: git cherry-pick 456def (where 456def is the hash of the commit whose changes you want to apply).

It's also possible to cherry-pick a range of commits, if you for example want to pick the latest 3 commits from the master branch your command could look like this git cherry-pick master~3.. .

For further information on take a look at the gitrevisions documentation or alternativly the Revision Selection chapter of the progit book .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM