Branching allows us to work on different versions of the same repository. Using branches we can work on new features without making changes to the original master branch. In this tutorial, let’s learn how to create a branch using GitHub and Git client.
Create a branch on GitHub
Log in to GitHub.
Navigate to the project repository.
Click on the master branch.
Enter the new branch name.
Click on the Create branch link.
Create a Git branch
We can use the git branch command to create, list branches on the command line.
$ git branch <branchname>
To create a new branch using the Git command line and to switch to the newly created branch, issue the command:
$ git checkout -b git-branch-demo
$ git checkout -b git-branch-demo Switched to a new branch 'git-branch-demo' [email protected] MINGW64 ~/SampleProject (git-branch-demo) $ git status On branch git-branch-demo Untracked files: (use "git add <file>..." to include in what will be committed) src/GitBranchFeatureDemo.java nothing added to commit but untracked files present (use "git add" to track) [email protected] MINGW64 ~/SampleProject (git-branch-demo) $ git add . [email protected] MINGW64 ~/SampleProject (git-branch-demo) $ git commit -m "Git-demo-branch Commit" [git-branch-demo 33d4a37] Git-demo-branch Commit 1 file changed, 9 insertions(+) create mode 100644 src/GitBranchFeatureDemo.java [email protected] MINGW64 ~/SampleProject (git-branch-demo) $ git push fatal: The current branch git-branch-demo has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin git-branch-demo [email protected] MINGW64 ~/SampleProject (git-branch-demo) $ git push --set-upstream origin git-branch-demo Enumerating objects: 6, done. Counting objects: 100% (6/6), done. Delta compression using up to 4 threads Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 509 bytes | 101.00 KiB/s, done. Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: remote: Create a pull request for 'git-branch-demo' on GitHub by visiting: remote: https://github.com/yskumar007/SampleProject/pull/new/git-branch-demo remote: To https://github.com/yskumar007/SampleProject.git * [new branch] git-branch-demo -> git-branch-demo Branch 'git-branch-demo' set up to track remote branch 'git-branch-demo' from 'origin'.
Create branch using Git GUI
We can also create a branch using Git GUI.
From the Git GUI menu options choose Branch >> Create…
Enter branch name and click on the Create button.