A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master
As you add additional commits on a branch, the branch will move along
The above command lists the branch and shows that we are working on master
List the remote branch
The above command lists remote branches and shows HEAD is pointing to remote master
Create a branch named bug-fix
The above command creates are new branch named bug-fix and update the working copy with new branch. Note this branch is created from master
git status gives the branch on with you are working
vagrant@git-m1:~/git-demo(bug-fix)$ git status
On branch bug-fix
nothing to commit, working directory clean
Lets add a file fix.txt to the branch bug-fix.
vagrant@git-m1:~/git-demo(bug-fix)$ echo "Hello" >> fix.txt
vagrant@git-m1:~/git-demo(bug-fix)$ git add fix.txt
vagrant@git-m1:~/git-demo(bug-fix)$ git commit -m "added fix file"
[bug-fix a8d0a15] added fix file
create mode 100644 fix.txt
Push the local branch bug-fix remote repository git-demo which is present on the git hub server
vagrant@git-m1:~/git-demo(bug-fix)$ git push --set-upstream origin bug-fix
Username for 'https://github.com': padmacho
Password for 'https://padmacho@github.com':
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 266 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/padmacho/git-demo.git
* [new branch] bug-fix -> bug-fix
Branch bug-fix set up to track remote branch bug-fix from origin.
origin points to remote repository . --set-upstream helps to push to remote branch
Switch to branch bug-fix-1
The above command will switch to branch bug-fix-1 from the current working branch enhancement-1
Rename branch bug-fix to bug1
Delete the branch bug1
vagrant@git-m1:~/git-demo(master)$ git branch -d bug1
warning: deleting branch 'bug1' that has been merged to
'refs/remotes/origin/bug-fix', but not yet merged to HEAD.
Deleted branch bug1 (was a8d0a15).
Note: Git complains that you are deleting a branch that was not merged,You cannot delete a branch on which you are currently working on.
-D to do a force delete