A good Git workflow should make use of branches for feature building, so that you dont clutter up your master department with every trade you make to your code. finding out find out how to rename a branch in Git can assist maintain your venture historical past easy and easy to take into account in relation to figuring out when a new feature was offered into your code. on this guide, well cover find out how to rename a neighborhood Git department, in addition to how one can rename a remote Git branch, and even rename a far flung Github department. These all will help keep your construction drift nice, clean, and straightforward to manage down the highway when coming again to a venture. Rename an area Git branch To rename a neighborhood Git department, we can use the Git branch -m command to switch the name: 1 2 three four 5 6 7 eight 9 $ git branch * feature1 grasp $ git branch -m feature1 feature2 $ git branch * feature2 master That used to be easy, right? Rename a far off Git department if you happen tore simply searching for the command to rename a far flung Git branch, this is it: 1 git push -u origin feature2:feature3 Full example to Rename a far flung GitHub department to provide an explanation for renaming a faraway Git department, well use a fictional GitHub venture to point out the method. Clone a GitHub Repo First, we clone the GitHub undertaking to our native box with the Git clone command: 1 2 3 4 5 6 $ git clone https://github.com/myusername/myproject.git Cloning into ‘myproject’ … far off: Counting objects: 3, finished. far off: total three (delta 0), reused zero (delta zero) Unpacking objects: a hundred% (three/three) finished. Checking connectivity… performed. Now our myproject repository used to be cloned from GitHub. The Git far off command shows all tracked repositories we will pull from and push to: 1 2 $ git far off origin in this case, we just see the default Git foundation identify for a far off server. we will seem extra verbosely at this with the Git far off -v command and flag: 1 2 three $ git far off -v beginning https://github.com/myusername/myproject.git (fetch) beginning https://github.com/myusername/myproject.git (push) Push native Git department to GitHub Repo Now that you’ve a GitHub challenge downloaded in the neighborhood, which you could create a local branch: 1 2 $ git checkout -b feature2 Switched to a new department ‘feature2’ this is shorthand for: 1 2 3 $ git department feature2 $ git checkout feature2 Switched to a brand new department ‘feature2’ After making changes, push feature2 to the far off repository named starting place: 1 2 3 4 5 6 7 eight 9 10 $ echo “mychange” > newfile.txt $ git add newfile.txt $ git commit -m “newfile.txt added” [feature2 c9acad71 newfile.txt added 1 file changed, 1 insertion(+) create mode 100644 newfile.txt $ git push origin feature2 Total 3 (delta 0), reused 0 (delta 0) To https://github.com/myusername/myproject.git Checkout a Remote Git Branch From a GitHub Repo If the feature2 branch just existed remotely on GitHub, you wont be able to push it. You can use the Git branch -a command to see all available local and remote branches: 1 2 3 4 5 $ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/feature2 remotes/origin/master You need to first check out the remote repository branch, give it a local name, and switch to it: 1 2 3 $ git checkout origin/feature2 -b feature2 Branch feature2 set up to track remote branch feature2 from origin Switched to a new branch ‘feature2’ Rename Git Branch on Remote GitHub Repo Now we can rename the remote branch to feature3 and push it to the remote origin: 1 2 3 4 5 $ git push -u origin feature2:feature3 Total 0 (delta 0), reused 0 (delta 0) To https://github.com/myusername/myproject.git * [new branch] feature2 -> feature3 branch feature2 set up to observe far off branch function three from beginning. We now linked our native feature2 branch to the far flung feature3 department. Any adjustments within the feature2 local department shall be pushed to feature3. to repair this, we should also do a local Git branch rename like we did before: 1 2 3 4 $ git branch -m feature2 feature3 $ git branch * feature3 master Delete Git branch on far flung GitHub Repo After renaming a far off department, the previous department remains as well. that you may simply delete the old department when you not want it: 1 2 3 4 5 6 7 8 9 10 eleven 12 13 14 15 16 17 18 $ git branch -a * feature3 grasp remotes/origin/HEAD -> origin/master remotes/beginning/feature2 remotes/origin/feature3 remotes/origin/grasp $ git push beginning :feature2 To https://github.com/myusername/myproject.git – [deleted] feature2 $ git department -a * feature3 grasp remotes/origin/HEAD -> origin/master remotes/starting place/feature3 remotes/beginning/grasp tracking concerns With Git and Troubleshooting Them tracking concerns arise from a misplaced monitoring reference with a far flung repository:department. These concerns are indicated through Git the use of messages like those under whilst you try to push or pull code: 1 2 three $ git push starting place feature4 error: src refspec feature4 does not fit any. error: did not push some refs to https://github.com/myusername/myproject.git this could occur if the far flung branch used to be renamed to feature4, however the native wasnt updated. you should use the Git department -a command to lend a hand see how to restoration local vs far flung branch issues. Then you should use Git checkout and Git branch -u to correct the connection. eventually, rename the local department to compare the far flung department for simpler future administration: 1 2 three four 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 $ git branch -a feature3 * grasp remotes/starting place/HEAD -> beginning/grasp remotes/foundation/feature3 remotes/beginning/feature4 remotes/foundation/grasp $ git checkout feature3 Switched to department ‘feature3’ $ git branch -u starting place/feature4 branch feature3 set as much as track remote branch feature4 from beginning. $ git department -m feature3 feature4 $ git branch * feature4 grasp $ git push foundation feature4 To https://github.com/myusername/myproject.git 3cc5caa..5cedaff feature4 -> feature4 Conclusion optimistically you at the moment are comfortable renaming Git branches either in the community or remotely. mastering the flexibility to use the Git department rename functionality can prevent a number of building complications down the highway.