Yarin
Yarin

Reputation: 183749

Git save and recover

I just broke my codebase. I want to rollback to my last commit, but I want to save off the broken version first, so I can analyze it later. What's the best course in this situation?

(I haven't actually tried anything, just looking for some hand-holding- It's been a long day and I don't feel like screwing this up..)

Upvotes: 0

Views: 121

Answers (3)

wkl
wkl

Reputation: 80001

There are a few things you can do:

Most likely the best thing for you to do is to create a separate branch to commit this broken code on, and commit to that branch.

git checkout -b my-broken-branch # creates a new branch called my-broken-branch
# commit as usual...
git checkout - # return to the last branch you were on

When you want to go back to that code, do a git checkout my-broken-branch to change over to that branch.


A quick thing you can do is git stash, which will save your work and you can reapply it later:

git stash

When you want to get those changes back, you can do a git stash apply which will apply the last changes you stashed away (but keep the information in the stash list).

As Andrew mentioned in the comments, you can also do git stash pop, which will apply the least stashed changes, and remove it from the stash list.

There is more you can do with stashing, since you can do multiple stashes and selectively apply them back.

Here's a more detailed explanation when you want to get more in-depth on stashing: http://gitready.com/beginner/2009/01/10/stashing-your-changes.html

Upvotes: 5

Amr Sharaf
Amr Sharaf

Reputation: 86

to reset a git repo to a certian commit:

git reset --hard <tag/branch/commit id>

to save your current version, just commit it to the repo before you reset back to the old one

Upvotes: 2

Ricardo Souza
Ricardo Souza

Reputation: 16456

You can do a commit or a new branch with this broken code. After that, checkout your working code.

git log -1

Take note of the commit name (sha code - can be the first 7 chars). Create a new branch for the broken code

git checkout -b broken
git add -A
git commit -m "Broken code"

Back to your branch and checkout your working code:

git checkout featureX
git checkout abc1234

Another way of doing this is to stash your changes and checkout your code. After that you can stash pop the files to work with them.

Upvotes: 1

Related Questions