1

I need a bit of help with git. My current "master" had some issues, so I checkout a version from a while back that I knew worked.

$ git checkout <hash>

Let's call that "previous." Then I made edits to "previous" and got things working just the way I wanted to. Then I commited and pushed "previous" to the git repo. Of course, on the repo, the "master" is still the old "master" with issues. I guess what I have to do is convert the "previous" (with the new edits) on my computer into "master" and then push that to the repo. How do I do that? I have read up on rebase and tried that, but that doesn't work. I get the error message that "Cannot rebase: You have unstaged changes."

punkish
  • 13,598
  • 26
  • 66
  • 101

4 Answers4

3

Presumably, what you've done with git checkout <hash> is get a new detached HEAD.

Optionally, keep a ref of the incorrect master (to be called oldmaster):

git branch oldmaster master

Delete the master reference:

git branch -D master

Make your current working branch (the detached HEAD) the new master branch.

git checkout -b master

Then, commit your changes (perhaps after adding new files if needed):

git commit ...
Bruno
  • 119,590
  • 31
  • 270
  • 376
  • your suggestion seems most logical to me. I tried it, and it seems to work, almost. The only problem is that now my local master is behind the remote master. So, I can't `git push`. I am being asked to first `git pull`, but if I do that then my local changes will be clobbered. What I want to do is just make the current local master also my remote master. How do I do that? – punkish Oct 16 '12 at 00:33
  • Force the push with `+`: `git push +master`. This can be OK if you're the only user of that remote repository, but this can cause great confusion if someone else has pulled that master branch. – Bruno Oct 16 '12 at 00:34
  • thanks... before I saw your comment, I did a `git push --force` and that seems to have done the trick. Yes, I am the only one working on this project. – punkish Oct 16 '12 at 00:37
1

I'm not sure how far back you have to go in "master" but you could do a git revert <hash> all the way back to the point you branched. This would "undo" all the commits, without loosing history.

This post might help you: Revert multiple git commits

Community
  • 1
  • 1
Joe
  • 2,987
  • 15
  • 24
0

Perhaps try running git checkout master; git status; and see if you in fact do have unstaged changes in the master branch. if this is the case, simply commit or perhaps stash these changes before your rebase. You should also verify that you successfully committed your changes on the previous branch.

bengreenier
  • 285
  • 2
  • 9
0

A simpler way to walk your master branch back is to use reset:

git checkout master
git reset --hard <hash>

Then your branch will be behind the origin master by some number of commits. To fix:

git push origin master -f

This will forcibly drop the commits ahead of your current position.

bribroder
  • 86
  • 6