Revert to the last stable commit without losing new commits

I am new to git, so I ask before I make some fatal error ... :-)
In the last days I have made some changes to my project to update to new versions of some of the libraries I am using, but I failed ( also due to lack of libs docs ...: - ().

I just decided to abandon the update, so now I need to go back to the last stable commit.
I also clicked on remote access, but that shouldn't be a problem since I'm currently the sole developer on this project.

This is my current git log:

$ git log --oneline
22c0713 Upgrading to Firebase 1.1
6d5f9f4 Porting customers to angularfire 0.8
fd9db42 Porting to Angularfire 0.8
d728b82 Working out authenticating on authenticated session problems
d511245 Testing authWithOAuthRedirect
abd9849 Porting to firebase 1.1.2
8884b88 Testing loadRemote() with relative path on public repositories
7830eea Testing loadRemote() with relative path on public repositories
f36c2f5 Finished working on I18N
...

      

The f36c2f5 commit (the last one I show above) is the last stable one I would like to revert to.

I would also like to avoid losing new (bad) commits for a possible future reference.

What is the most appropriate strategy for this task?

UPDATE . I'm almost there thanks to your responses (going to take over in a minute ...). Another little problem: I also have a "gh-pages" branch to push my dist subfolder to the github gh-pages site.
Now after

git checkout -b my_branch_for_future_reference
git checkout master
git reset --hard f36c2f5
git push -f

      

do

git subtree push --prefix dist origin gh-pages

      

I am getting this error:

git push using:  origin gh-pages
To git@github.com:MYUSER/MYREPO.git
 ! [rejected]        3febf7c0812441c7379710d0a1f5f1ec26adbd9e -> gh-pages (non-fast-forward)
error: failed to push some refs to 'git@github.com:MYUSER/MYREPO.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

      

I checked, but git subtree push doesn't have the -f 'flag ...: - (

UPDATE 2 . I think I found the answer to my last question myself:

git push origin `git subtree split --prefix dist master`:gh-pages --force

      

might be the solution, right? (I haven't run it yet ... :-).

UPDATE 3 : yes, this is a solution to force pushing a subtree.

+3


source to share


5 answers


I would do it like this:

git checkout -b upgrades
git checkout master
git reset --hard f36c2f5
git push -f

      



Explanation:

  • keep the latest (bad) commits by keeping them on a separate branch, for later
  • check master
  • reset master for the commit you mentioned: this is discarded later, commits the captain (but the "upgrades" branch will still have them)
  • hit master, with "-f" = "force" flag as you want to overwrite the remote server to throw some commits (without "-f", git won't let you do that)
+1


source


My usual strategy in this situation is to create a new branch ( git branch <new_branch_name>

) and then reset your current branch to the commit you specify ( git reset --hard <commit>

in this case git reset --hard f36c2f5

). Be careful with git reset --hard

though, as this will blow away any uncommitted changes in your working tree.



The current branch will revert back to stable commit, but your other commits are still available in your newly created branch.

+1


source


One possibility is to create a local branch pointing to the most recent "bad" commit

git branch most-recent-bad-commit

      

and then make your current branch point the last "stable" commit

git reset --hard f36c2f5

      

Note that the latter will also remove any uncommitted changes to tracked files. The corrected files will not be changed.

0


source


Assuming you are on a branch master

, you can create a new branch named experiment

and then return to master in f36c2f5

. For example.

Make sure you have no uncommitted data then create a new branch experiment

git branch experiment 

      

Reset master

to last good commit:

git reset --hard f36c2f5

      

0


source


There are several ways that you could go about this. You can simply use a series of commands git revert

to create new commits that will undo the changes to the commits you no longer want.

git revert 22c0713
git revert 6d5f9f4
...
git revert 7830eea

      

(Note that a team git revert

can accept multiple commits, so you can do it in one command, but I've personally had mixed results with this.)

Alternatively, you can create a new branch in the current HEAD:

git branch for_future_reference

      

And then reset the current (existing) branch to point to the commit you want:

git reset --hard f36c2f5

      

Note that with the latter method, you will need to "force" push ( git push -f

) on the remote in order to rewrite history. Thus, the old method may be preferable, even as a solo developer.

0


source







All Articles