Reputation: 8958
I have done chronologically commits c1, c2, ..., c499, c500. I want to rollback to c300 and not lose c301 to c409. I expect to type a command that creates a new big commit c501.
I don't care about the states of git. I just want the states of the files to be ok.
I have tried :
1) git reset --hard c3
then git push -f
. This erased everything and I had to ask a coworker not to do git pull and send me a zip of what he has
2) git revert HEAD~2
. This looked promising but it fails midway :
error: could not revert 39714f4... blablabla commit message
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
3) git checkout c300
I am now on a new branch with no name. I have tried to rename it and merge it with master but master realizes I am merging with something anterior and doesn't merge anything
4) spent 2 hours on internet and SO
Thanks for your help !!
Upvotes: 0
Views: 203
Reputation: 10541
Here's what I would personally do.
1) Create and checkout a new branch called 'current_project' (this will be your backup)
git checkout -b current_project
2) Commit all of your work to it
git add --all
git commit -m 'my latest commit'
3) Now create another new branch called 'past_commits'
git checkout -b past_commits
4) Make sure you are in the past_commits
branch with the git branch
command
git branch
The branch you're in should have a *
next to it and be colored in green. (Possibly not, depending on your terminal)
5) See all of your commits in the past_commits
branch
git log
You should see a huge list of all your commits, in this format:
commit [commit id]
Author: [your username] <[your email]>
Date: [commit time]
[commit message]
An example from one of my projects:
commit e271b3db885b55c2a2f665c018b97d3f
Author: Starkers <[email protected]>
Date: Sat Apr 11 13:33:30 2014 +0100
fix the production environment
commit c22c00636eb3f7234eea3789f001e66f
Author: Starkers <[email protected]>
Date: Fri Apr 10 14:37:12 2014 +0100
configure railgun correctly
6) Now you can jump back to any commit including anything in the 301 to 409 range, (or indeed any commit at all) by checking out a specific commit:
git checkout [commit id]
For example, if I wanted to checkout what my project was like the picosecond I configured the railgun gem correctly, I'd run:
git checkout c22c00636eb3f7234eea3789f001e66f
so you'd need to find a commit in the 301 - 401 range and check it out by pasting its commit id.
Once you've looked at everything, return to the present by running
git checkout past_commits
That will automatically move you back to the latest commit in the branch, which should be your 'my latest commit' commit.
If you want to make changes in the past and branch off your project in a new direction, I would use a hard reset to reset back to a specific commit. Be careful, as a hard reset is destructive. It will delete all of your untracked changes, and all of the commits in the current branch older than the commit your resetting back to!
reset --hard [commit id]
Now work away to your heart's content in this branch, committing to it normally (see step 2). When you want to go back to the latest commit of your original project, run
git checkout current_project
And everything will be how it was at the time of 'my latest commit'
Upvotes: 0
Reputation: 21353
As stated in my comment if you want to reset to c409 git checkout c409 . && git commit -a -m "Reverted to c409"
The dot in the checkout command is important and the command should be executed from the repository root. Also creates a nice reversion commit instead of 100~ reversion commits created by git revert
. THIS WILL DESTROY c409 - c500
Upvotes: 1
Reputation: 44619
If you just want to keep the commits somewhere for reference, just create a branch to reference it.
git checkout master # Assuming, master is the branch you want to rollback
git branch backup # This is your backup, it points to the same state as master right now.
git reset --hard c300 # Revert your branch to the target commit
git push --force # overwrite the repository HEAD on your server
Then anytime you want to access your removed commit, just git checkout backup
.
The important point to understand here is that in git, a branch is only a label pointing to a commit.
By the way, next time you lose a commit, checkout git reflog
to find the commit id you where before.
Upvotes: 2