Manage Local Git Branches
Our Git environment is ready for us to start work on our repository. Typically, especially on a project with many contributors, Git branches allow each contributor to have as many non-overlapping copies of the repository as they need, to make and manage their changes. We will do some basic work with branches, so we can, later, contribute our changes to the main branch of the GitHub repository.
View, Create, Switch To, & Remove Branches
-
From the Docker Container prompt, list your local Git branches with the following command:
# Display all local git branches git branch
-
Notice that only the main branch cloned to your local Git repository from GitHub.
Info
- This is normal/expected behavior; only the main branch clones from GitHub because other branches (like the branch1 branch we created in GitHub) are likely other peoples' work in-progress and any work we do should originate from the main branch of the GitHub repository.
- The asterisk to the left of "main" indicates that main is our current, working branch.
-
List all Git repository branches, local and remote, with the following command:
# Display all local and remote repository git branches git branch -a
-
Notice how we see:
- A main branch.
- This is our local copy of the main branch.
- An origin/main branch alias (for remotes/origin/HEAD).
- This is the copy of the main branch in Github.
- And an origin/remotes/branch1 branch.
- This is the copy of the branch1 branch in Github.
Info
Origin is how our local repository refers to GitHub so origin/main represents the main branch on GitHub.
- A main branch.
-
Create a new, local branch named branch2, with the following command:
# Create a new local git branch named 'branch2' git branch branch2
-
List your local branches again with the following command:
# Display all local git branches git branch
-
Notice your new branch, branch2 and also that the asterisk next to main tells us that, even though we just created branch2, our working branch is still main.
-
Again, list all branches, local and remote, with the following command:
# Display all local and remote repository git branches git branch -a
-
Notice that branch2 does not exist as a remote branch in GitHub. This is normal behavior and something we will work with later on.
-
Switch your working branch to branch2 with the following command:
# Switch the working branch from 'main' to 'branch2' git checkout branch2
-
Verify your current, working branch with the following command:
# Display all local git branches git branch
-
Notice the asterisk next to branch2 which indicates that branch2 is our current, working branch.
-
Create and simultaneously switch to a new branch with the following command:
# Create and switch the working branch to a new local git branch named 'branch3' git checkout -b branch3
-
The
git checkout
command allows you to switch between branches.- The
git checkout
command with the-b
flag creates a new branch and switches to the new branch.
Tip
The
git checkout -b branch_name
command is just a shortcut for thegit branch branch_name
command followed bygit checkout branch_name
command.
- The
-
List your local branches again with the following command:
# Display all local git branches git branch
-
Notice the asterisk next to branch3 which indicates that branch3 is our current, working branch.
-
For our purpose, we only need one branch, in addition to the main branch, so we can remove one of our new branches with the following command:
# Delete the local branch named 'branch2' git branch -d branch2
Tip
Git will not allow you to delete your current, working branch.
-
Review your local branches with the following command:
# Display all local git branches git branch
-
Notice that branch2 is no longer available.
We can make some changes to our repository within branch3 without impacting the main branch. Click the link below to continue: