![]() ![]() I have not covered any examples with -M as it can be destructive and will rename your branch, but you will lose the old branch with that name and those commits because branch names must be unique. One can safely rename a branch with ' -m' (move), but one has to be careful with ' -M', because it forces the rename, even if there is an existing branch with the same name already. In this tutorial we covered different examples to rename a git branch both locally and remotely. Let us execute this command on our workstation: git push -set-upstream origin new-issue-7843īranch 'new-issue-7843' set up to track remote branch 'new-issue-7843' from 'origin'.Īnd you are all set to use the new branch name. Step-4: Reset the upstream branch for the new-name local branchįor every branch that is up to date or successfully pushed, add upstream (tracking) reference: $ git push -set-upstream origin Verify the list of branch on local and remote repo:ĪLSO READ: Set up GitHub with Visual Studio code Remote: To create a merge request for new-issue-7843, visit: Let us convert this syntax into command: git push origin :issue-7843 new-issue-7843 Next we will delete the old branch from the remote repository and push the new branch to the remote repo using following syntax: $ git push origin : Step-3: Delete the old-name remote branch and push the new-name local branch Verify the new branch name in your local repo: git branch Since we are in a different branch we will execute the following command to perfom the rename: git branch -m issue-7843 new-issue-7843 OR you must use the following command by specifying both old and new branch name: $ git branch -m you skipped the step-1 above): $ git branch -m If you are still in the same branch (i.e. Total 4 (delta 1), reused 1 (delta 0), pack-reused 0Īs you can see our main branch is ahead of new-feature branch by one commit: git log -onelineĢ0412c0 ( HEAD -> main, origin/main, origin/HEAD) Added index.html templateĬf00196 (origin/new-feature, origin/issue-7843, new-feature) Added patch-1ĥe04f57 Created new script to add git switch main Now we can also push our previously committed changes to the main branch without loosing any changes: git push origin main So our remote branch has been successfully renamed: git branch -remotes Remote: To create a merge request for new-feature, visit: New_name: New branch git push origin origin/feature:refs/heads/new-feature :feature origin, old and new are names of branches old and new respectively. The syntax to be used: git push /:refs/heads/ : Let us go ahead and rename this branch name on the remote server only. So now issue-new-7843 exists in my local workstation as well as remote server. Verify the list of remote branches: git branch -remotes Remote: To create a merge request for issue-new-7843, visit: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 So let's push this to the remote server: git push origin issue-new-7843 In our previous example we had renamed our local git branch to issue-new-7843 but it was not pushed to git server. In this scenario we assume that the respective remote branch is also cloned locally wherein you wish to rename the remote branch only, leaving the local branch with the existing name. Make it executable and place it in any directory that is in your PATH.Scenario-1: When remote branch is also cloned locally The file can be named anything as long as it begins with a git- prefix. To do that, save this piece of shell script code as a file named git-rename-remote-branch: #!/bin/shĮcho "Rationale : Rename a branch on the server without checking it out."Įcho "Example : $(basename $0) origin master release" If you find yourself renaming remote branches a lot, then it is better to add this as a Git command. This is the invocation to rename a branch old_branch on a remote named foo_remote to a new branch name new_branch: $ git push foo_remote foo_remote/old_branch:refs/heads/new_branch :old_branch ![]() If you want to rename a remote branch, say a branch that is on Github, from the comfort of your shell then you are in luck for one such experience! Git is known for its confusing and difficult commands. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |