Creating and delete local git branches is easy:
# create a new branch and switch to it
PS> git checkout -b testing2
Switched to a new branch 'testing2'
# if you want to create a new branch but don't want to switch to it do this instead:
# git branch testing2
# switch back to another branch (required so I can delete it)
PS> git checkout testing
Switched to branch 'testing'
# delete the previously created branch
PS> git branch -d testing2
Deleted branch testing2 (was 43d7d10).
Once you have created a local branch, you can push it to the remote. Note: the branch must exist locally.
- OPTION 1: switch to the branch locally, and then push
12345678PS> git checkout testing2Switched to branch 'testing2'# git push [remote repository] [branch name]PS> git push origin testing2Total 0 (delta 0), reused 0 (delta 0)To email@example.com:rakheshster/PS-AppMgmt.git* [new branch] testing2 -> testing2
- OPTION 2: stay on the current branch, and when pushing specify the local branch you want to push
12345# git push [remote repository] [branch name] where [branch name] is [local branch name]:[remote branch name]PS> git push origin testing2:nottesting2Total 0 (delta 0), reused 0 (delta 0)To firstname.lastname@example.org:rakheshster/PS-AppMgmt.git* [new branch] testing2 -> nottesting2
Notice the syntax: you specific the remote repository name
originand then specify the local branch name, colon, the remote branch name. The remote branch can have a different name from the local branch too.
The proper syntax of the
git pushcommand can be seen in option 2; while option 1 is the simplified special case version. The
[local branch name]:[remote branch name]part is known as a
Which brings us to delete remote branches. To delete remote branches, you do the same as above except that you push an empty local branch to the remote branch that you want to delete – effectively nullifying the remote branch! It’s non-intuitive in a way, but I find it very geeky and logical. It’s as though you no longer had a
delete command on your file system, and if you wanted to deleted a file you copied
/dev/null to it thus deleting it.
# delete remote branch
PS> git push origin :nottesting2
Enter passphrase for key '/c/Users/rakhesh/.ssh/id_rsa':
- [deleted] nottesting2