Bootstrap FreeKB - Git (Version Control) - Create branch using the git branch command
Git (Version Control) - Create branch using the git branch command

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Branches are used as an isolated way to make changes to files in a repository. A common example would be to create a new branch using the git branch or git checkout command, switch to the new branch using the git checkout command, make a change to a file, commit the change using the git commit command, and then merge the branch to the master branch using the git merge command.

 


The git branch command with the -a or --all flag can be used to display all of the branches in the repository

git branch --all

 

Which will return something like this. The wildcard character is used to identify the branch you are currently using (master in this example).

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/logging
  remotes/origin/master

 

Here is how you can create a new branch that contains all of the files of the branch you are currently in.

git branch feature/foo

 

Optionally, you can use the --set-upstream-to option to set the "parent" branch of the "child" branch.

git branch --set-upstream-to master

 

Now the --all flag should include the "feature/foo" branch. The wildcard character shows that you are still in the "master" branch.

  feature/foo
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/foo
  remotes/origin/feature/logging
  remotes/origin/master

 

The git checkout command can be used to switch to the newly created branch.

~]# git checkout feature/foo
Switched to a new branch 'feature/foo'

 

Or, the git checkout command with the -b option can be used to create the feature/foo branch and switch to the feature/foo branch, as a sort of all-in-one command.

~]# git checkout -b feature/foo
Switched to a new branch 'feature/foo'

 

Now the git branch --all command should show that feature/foo is the currently selected branch.

~]# git branch --all
* feature/foo
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature/foo
  remotes/origin/feature/logging
  remotes/origin/master

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 918931 in the box below so that we can be sure you are a human.