punkish
punkish

Reputation: 15248

convert a checkout to master

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."

Upvotes: 1

Views: 1639

Answers (4)

bribroder
bribroder

Reputation: 86

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.

Upvotes: 0

Bruno
Bruno

Reputation: 122669

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 ...

Upvotes: 3

bengreenier
bengreenier

Reputation: 285

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.

Upvotes: 0

Joe
Joe

Reputation: 2987

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

Upvotes: 1

Related Questions