The git checkout command can be used to:
- Download one or more files from the origin Git repository to a directory on your local PC.
- Switch to another branch
- Create a new branch
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 can be used to create a new branch that contains all of the files of the branch you are currently in.
git branch development
Now the --all flag should include the "development" branch. The wildcard character shows that you are still in the "master" branch.
development
* master
remotes/origin/HEAD -> origin/master
remotes/origin/feat/logging
remotes/origin/master
The git checkout command can be used to switch to the newly created branch.
~]# git checkout development
Switched to a new branch 'development'
Or the git checkout command with the -b option can be used to create a new branch (named development in this example) and switch to the new branch, as a sort of all-in-one command.
~]# git checkout -b development
Switched to a new branch 'development'
Now the git branch --all command should show that development is the currently selected branch.
~]# git branch --all
* development
master
remotes/origin/HEAD -> origin/master
remotes/origin/feat/logging
remotes/origin/master
It is probably a good idea to set the upstream, which is almost always origin/main or origin/master so that the branch is tracking any changes made in the main or master branch.
git branch --set-upstream-to=origin/master development
Did you find this article helpful?
If so, consider buying me a coffee over at