Getting Started with Branches using Git Commands
Getting Started with Branches using Git Commands
Effort : 25 mins
Objectives
After completing this lab you will be able to use git commands to work with branches on a local repository, including:
Pre-requisites
This lab is designed to be run on Skills Network - Cloud IDE which is runs on a Linux system in the cloud and already has git installed.
If you intend to run this lab on your own system, please ensure you have git (on Linux or MacOS) or GitBash (on Windows) installed.
1. Click on the Terminal menu to the right of this instructions pane and then click on New Terminal.
about:blank 1/8
5/6/23, 8:55 PM about:blank
2. This will add a new Terminal window at the bottom where you can start entering commands.
1. 1
1. mkdir myrepo
Copied!
2. Go into the myrepo directory by copying and pasting the cd command below:
1. 1
about:blank 2/8
5/6/23, 8:55 PM about:blank
1. cd myrepo
Copied!
3. In this myrepo directory lets create a new local git repository using the git init command. Copy and paste the command below into the
terminal:
1. 1
1. git init
Copied!
4. A new local repository is now created, which you can verify by doing a directory listing by pasting the following command into the terminal
window:
1. 1
1. ls -la .git
Copied!
The output shows the contents of the .git sub-directory which houses the local repo:
1. 1
1. touch newfile
Copied!
2. Add this file to the repo using the following git add command:
1. 1
Copied!
1. 1
2. 2
Copied!
about:blank 3/8
5/6/23, 8:55 PM about:blank
2. Once the repo has the newfile in it let’s commit our changes using the the following git commit command. Note that the commit requires a
message which we include using the -m parameter:
1. 1
Copied!
1. To make subsequent changes in our repo, lets create a new branch in our local repostitory. Copy and paste the following git branch command
into the terminal to create a branch called my1stbranch:
1. 1
Copied!
1. 1
1. git branch
Copied!
2. Note the output lists two branches - the default master branch with an asterix * next to it indicating that it is the currently active branch, and the
newly created mys1stbranch:
1. 1
Copied!
2. Let’s verify that the new branch is now the active branch by issuing the following git branch command:
1. 1
1. git branch
Copied!
3. Note that the asterix * is now next to the my1stbranch indicating that it is now active:
about:blank 4/8
5/6/23, 8:55 PM about:blank
As a shortcut to creating and branch using git branch and then making it active using git checkout you can use the shortcut like follows
with the -b option that creates the branch and makes it active in one step:
1. 1
Copied!
Exercise 7: Make changes in your branch and check the status of files
added/changed
1. Lets make some changes in your new branch called my1stbranch. Start by adding some text to newfile by pasting the following command into
the terminal that will append the string “Here is some text in my newfile.” into the file:
1. 1
Copied!
2. Verify the text has been added by pasting the following cat command:
1. 1
1. cat newfile
Copied!
3. Now let’s create another file called readme.md using the following command:
1. 1
1. touch readme.md
Copied!
4. And now add it to the repo with the following git add command:
1. 1
Copied!
5. So far in our new branch we have edited the newfile and added a file called readme.md. We can easily verify the changes in our current branch
using the git status command:
1. 1
1. git status
Copied!
6. The output of the git status command shows that the files readme.md has been added to the branch and is ready to be committed, since we we
added it to the branch using git add . However, even though we modified the file called newfile we did not explicitly add it using git add and
hence it is not ready to be committed:
about:blank 5/8
5/6/23, 8:55 PM about:blank
7. A shortcut to adding all modifications and additions is to use the following git add command with an asterix * … this will also add the
modified file newfile to the branch and make it ready to be committed:
1. 1
1. git add *
Copied!
1. 1
1. git status
Copied!
9. The output now shows both the files can now be comitted:
1. 1
Copied!
2. We can issue the follwoing git log command to get a history of recent commits:
1. 1
1. git log
Copied!
3. The log shows 2 recent commits - the last commit to my1stbranch as well as the previous commit to master:
1. 1
Copied!
NOTE: If you don’t specify the --no-edit flag you may be presented with an editor screen showing the message with changes to be
reverted. In that case, press the Control (or Ctrl) key simultaneously with X.
2. The output shows the most recent commit with the specified id has been reverted:
1. 1
2. 2
3. 3
4. 4
1. touch goodfile
2. git add goodfile
3. git commit -m "added goodfile"
4. git log
Copied!
2. The output of the log shows the the newly added goodfile has been comitted to the my1stbranch branch:
3. Now let’s merge the contents of the my1stbranch into the master branch. We will first need to make the master branch active using the following
git checkout command:
1. 1
Copied!
Copied!
about:blank 7/8
5/6/23, 8:55 PM about:blank
6. Now that changes have been merged into master branch, the my1stbranch can be deleted using the following git branch command with the -d
option:
1. 1
Copied!
Summary
In this lab, you have learned how to create and work with branches using git commands in a local repository. In a subsequent lab you will learn how to
synchronize changes in your local repository with remote GitHub repositories.
Author(s)
Rav Ahuja
Other Contributor(s)
Richard Ye
Changelog
Date Version Changed by Change Description
2022-01-14 1.0 Rav Ahuja Initial version created
2022-01-27 1.1 Richard Ye Added git config instructions
2023-04-03 1.2 Lavanya Rajalingam Updated New SN logo
about:blank 8/8