Reputation: 15
I am completely new to Git. I committed some wrong code and then to rectify it, I reset my current branch to the previous one. But somehow this didnt do what i wanted! Now my master has an additional branch name of previous_master (this is where I want the branch to point to). The code I wrongly committed has the branch name original/master! I am not sure how to rectify my mistake. I want the master to also have the branch name original/master.
Upvotes: 0
Views: 5759
Reputation: 1856
little basics
First remember that everything in git is identified by hash. Everything you commit in history is accessible via this hash (git checkout $hash
to change application state to it, git show $hash
to show changes in that commit and so on...).
The branch names works like something like tags - they enable easy organisation of your working tree, but all of them are just hash pointers. You can, if you want, go "back in time" in a branch by using git checkout <hash id>
.
origin/*
Branches which look like something/something
(e.g. origin/master
) are remote branches. Thus origin/master
which means branch "master" on the "origin" remote.
You can show your local branches by:
git branch
and remote by:
git branch -r
You can send your master
to the remote repo called origin
by:
git push origin master
If there is a conflict between the history of the two branches in the push, git will not allow the push it and it will prompt you to manually resolve the conflict before allowing the push.
In your case you need to find which commit (which hash) is your wanted application state and then reset your branch to that commit.
You can show your commit history in terminal by:
git log --graph --decorate --pretty=oneline --abbrev-commit --all
This looks crazy, but its my favourite git command ever, I have it on short alias
You can switch your app state to that commit by:
git checkout $hash
HEAD now is at that hash.
Find your wanted state and that reset your master to that by:
git checkout master
git reset --hard $hash
be careful because this will delete all your uncommitted changes.
All committed history will remain - simply told it will reset your current branch and state to that hash in history.
If you then want to send it to repo call:
git push --force origin master
this will definitively reflect your master state to origin/master (send it to origin remote).
Do not use --force unless you are absolutely sure you want to change history on that remote and never use it if another developer already has used already pulled from origin/master, because will changes the history of origin/master
.
It will do what you want, but you should read about it in more detail:
man git push
Upvotes: 4
Reputation: 212198
If you know that sha of the commit you want master to point at, just do:
$ git checkout master
$ git reset <sha>
If you execute
$ git reflog
You will get a history of what you've done. The strings in the left column are the shas. This information may be a bit intimidating at first, but it should be fairly easy to figure out the one you want. Experiment!
Upvotes: 4