Git - Rename a local branch which has 2 commits pushed to remote

Viewed 324

I have a branch which tracks a remote branch, and I've made 2 commits which have been pushed to the remote branch, but not yet PR'd and merged into master.

I now need to rename my local branch, push it to remote, and PR and merge that branch into master.

Would this workflow be correct? Thanks in advance...relatively unfamiliar still with much of Git so I appreciate any input.

  1. Unset Upstream Branch on the local branch
  2. Rename the local branch
  3. Push the local branch to remote
  4. Create a PR to merge the new branch into master
3 Answers

(Generally speaking, you shouldn't need to worry too much about names of branches)

You could just create a new branch and delete the old branch.

  1. git checkout this_name_was_right_and_the_time
  2. git checkout -b feature/this_name_is_definitely right
  3. git push
  4. Create PR from the new branch
  5. Delete the old branch

Hi dude i hope that u understand my workflow. First we have to reset the master. Just make your that nobody else merged something, that will makes it a bit harder for you.

  1. checkout the master and pull the current "state"
$ git checkout master
$ git pull
$ git log
  1. copy the commit id after your 2 merged commits
  2. Reset master (be sure what u do!)
$ git reset --hard <commit-id-that-you-copied>
  1. Check the local master log
git log
  1. delete your remote branch (which was merged) and rename your local branch, than push it again
$ git push <remote_name> :<branch_name>
(e.x. git push origin:your_local_branch
  1. rename local branch
$ git branch -m <your_new_name>
  1. push upstream new branch and creat PR
git branch --set-upstream-to=origin/your_new_name

this is the easiest way but be sure what you are doing.

I found the answers here a bit off target, or somewhat sparse, so I thought I'd track a branch re-name and provide a full answer. This solution is carried out entirely on the local command line.

So, as per the OP, if a local branch is pushed to origin, but is not yet merged, it can still be renamed, and the remote branch deleted, so the local branch can be pushed to replace it under the new name.

Let's say the branch to be renamed is feature/old_name and the new name is feature/new_name.

First of all just check where you are. Sometimes I think I'm still on a particular branch, when I'm not, or I forget I should be on a particular branch to do the operation, so it's good practice to just check where you are:

$ git status
# On branch feature/old_name
nothing to commit, working directory clean

To list all available branches, just to orientate:

$ git branch
  feature/some_name
* feature/old_name
  master

If you're not on the branch you want to rename, make sure you are:

$ git checkout feature/old_name
Already on 'feature/old_name'

Then just rename the local branch with the -m option. For the local branch, it's that simple:

$ git branch -m feature/new_name

Now to check the remote tracking, issue a "very verbose" branch request, and observe the branch that's just been renamed is still tracking on the remote under it's old name:

$ git branch -vv
  feature/some_name fc0ec55 [origin/feature/some_name] Some commit message
* feature/new_name  a64af75 [origin/feature/old_name] Previous commit message
  master            3a0c732 [origin/master] Another commit message

So now it's necessary to delete the remote tracking branch with the old name altogether. The branch with the new name is still checked out:

$ git status
# On branch feature/new_name
nothing to commit, working directory clean

$ git push origin --delete feature/old_name
 - [deleted]         feature/old_name

If you check on your online web GUI (GitLab, GitHub, BitBucket...) you should find the old_name remote branch, and it's commits, are gone. But looking locally now, the local tracking doesn't actually know the remote branch has gone yet:

$ git branch -vv
  feature/some_name fc0ec55 [origin/feature/some_name] Some commit message
* feature/new_name  a64af75 [origin/feature/old_name] Previous commit message
  master            3a0c732 [origin/master] Another commit message

So to make it all happen, push the local branch to origin, under its new name:

$ git push -u origin feature/new_name
Counting objects: 21, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (14/14), 2.46 KiB | 0 bytes/s, done.
Total 14 (delta 11), reused 0 (delta 0)
remote: Create pull request for feature/new_name:
 * [new branch]      feature/new_name -> feature/new_name
Branch feature/new_name set up to track remote branch feature/new_name from origin.

And confirm:

$ git status
# On branch feature/new_name
nothing to commit, working directory clean

$ git branch -vv
  feature/some_name fc0ec55 [origin/feature/some_name] Some commit message
* feature/new_name  a64af75 [origin/feature/new_name] Previous commit message
  master            3a0c732 [origin/master] Another commit message

The branch should now be available on your remote under the new name.

I haven't tested the scenario where someone else has also pushed commits to the remote branch, but as long as you pull the branch before carrying out this sequence, I believe that should also work.

If you're in doubt (git is sometimes like that) you can always zip your local repo first, and keep it out of the way somewhere, so that you can at least restore locally, even by copying in the old files to the repo if necessary ;-)

Related