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

Updated:   |  Git (Version Control) articles

The git checkout command can be used to:


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.

 


Switch to another branch

The git branch command can be used to view the shorthand name of the branch you are in. 

git branch

 

Something like this will be returned.The wildcard character is used to identify the branch you are currently using (master in this example).

* master

 

The -a or --all flag can be used to display all of the branches in the repository.

~]# git branch --all
  development
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feat/logging
  remotes/origin/master

 

If you try to switch to the branch you are currently on, the following will be returned.

~]$ git checkout master
Already on 'master'

 

Assuming there is already a branch "development", here is how you would switch to the development branch using the git checkout command.

~]# git checkout development
Switched to a new branch 'development'

 




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 a1ad0f in the box below so that we can be sure you are a human.