Trying to get a branch I created after working in a detached HEAD for a while, to be my master branch, but concerned that I may use the wrong sequence of actions and mess up my repo (local and remote)...
Series of actions (in "fork", a git GUI) resulting in the status shown in the image below:
- commit tagged "v0.56.4 release" pushed to remote
- did some changes and messed up my code. Stashed the changed files and checked out the previous commit to "revert" to a clean code
- continued development from there on, regularly committing and pushing changes to remote, only mildly bothered by the "detached HEAD" warning I was getting, as all the literature told me this would be an easy fix.
- commit tagged "v0.57 release 1" followed by creating a new branch called "tmp".
How do I make this "tmp" branch my new "master" branch, leaving the "stash@{0}" changes as a dead branch I have no intention to revisit?
This thread appears to provide close to the solution I am looking for, except that it doesn't mention any stashing of anything, so I am not sure what potential conflict/issue could result from following its sequence of actions.
CodePudding user response:
To get rid of the stash entry run (assuming the image is correct and it is the latest one):
git stash drop
Reconciling branches tmp
and master
can be done in many ways. Branches in Git are just stickers that you stick to commits to keep track of which is which.
You can just remove master
and create a new sticker on the current tmp
.
Assuming you're on tmp
right now:
git branch -d master
git checkout -b master
Or you can unstick master
and move it to tmp
:
git checkout master
git reset --hard tmp
Or you can use the merge mechanism and merge the changes from tmp
to master
. master
is an ancestor of tmp
so merge can be fast-forwarded which again just ends up in moving the branch label:
git checkout master
git merge --ff-only tmp
There are probably tons of other ways. The point is: don't worry, you didn't corrupted your repository until you've lost some commit you wanted to keep, and even then it's probably still recoverable because Git doesn't garbage-collect unused things immediately.
The last thing to do is to remove tmp
. (Do it after making sure the master
is there already.)
git branch -d tmp
CodePudding user response:
Important: copy the local repository to a backup folder before proceeding.
These commands will update the master branch to be the same as tmp branch, and delete the tmp branch:
$ git checkout master
$ git rebase tmp
$ git branch -d tmp
And these will create a branch for the stashed files:
$ git checkout -b stash_branch
$ git stash pop
$ git commit -m "stash commit"
Here is the procedure for master branch update, in a sample repo (mine is named main
instead of master
):
~ $ mkdir x && cd $_
~/x $ git init .
Initialized empty Git repository in /Users/teixeira/x/.git/
~/x $ touch a && git add . && git commit -m "a"
~/x $ git checkout -b tmp
~/x $ ls
a
~/x $ touch b && git add . && git commit -m "b"
~/x $ touch c && git add . && git commit -m "c"
~/x $ git log --oneline
6c566d0 (HEAD -> tmp) c
f6c9cfa b
20a3d76 (main) a
~/x $ git checkout main
Switched to branch 'main'
~/x $ git rebase tmp
Successfully rebased and updated refs/heads/main.
~/x $ git log --oneline
6c566d0 (HEAD -> main, tmp) c
f6c9cfa b
20a3d76 a
~/x $ git branch -d tmp
Deleted branch tmp (was 6c566d0).
~/x $ git log --oneline
6c566d0 (HEAD -> main) c
f6c9cfa b
20a3d76 a