You can use git branch -m command to rename a local git branch. To rename a remote git branch, you first need to delete the existing branch and create the new branch.
Warning: Do not execute any command in your remote repository without being fully aware of the implications.
Create a new local git repository:
1 2 |
## Create a new git local repository git init myrepo && cd myrepo |
Add some changes and commit:
1 2 3 4 |
## Create first commit in default branch echo "cat" > file1 git add file1 git commit -m "commit1" |
Add a remote git repository to your local repository:
1 2 3 4 5 6 7 8 9 |
## Add a remote repository ## Create an empty remote git repository (i m using GitHub for this demo) ## Add your username and email to git config (optional) git config --add --local user.name cloudaffaire git config --add --local user.email cloudaffaire@gmail.com ## Add remote repository to your local repository git remote add origin git@github.com:CloudAffaire/myrepo.git git branch -M main ## rename of default local branch (master -> main) git push -u origin main |
Create a new local and remote git barnch:
1 2 3 4 5 6 |
## Create a new git local and remote branch (named feature) git checkout -b feature echo "dog" > file2 git add file2 git commit -m "commit2" git push -u origin feature |
Rename a local git branch name:
1 2 |
## Rename the local git branch (feature -> bugFix) git branch -m bugFix |
Rename a remote git branch name:
1 2 3 |
## Rename (delete + create) the remote git branch (feature -> bugFix) git push origin --delete feature ## deletes feature branch git push -u origin bugFix ## creates bugFix branch |
Delete your local and remote git repository