DEV Community

Cover image for How to get started with branches using git commands on a local repository
Jose Horta Calvario
Jose Horta Calvario

Posted on • Edited on

How to get started with branches using git commands on a local repository

Introduction

Application developers rarely work alone. Large web/cloud/mobile development and data science projects will include many people โ€“ front-end developers, back-end developers, database administrators, repository administrators, and others. Every change by every contributor must be tracked and controlled to enable collaboration, accountability and version management. This type of distributed version control is extremely important when managing both small and large software projects.

Branches are the heart of workflows in Git-based version control systems like GitHub. In this Blog article you will become familiar with creating and using branches, and merging your changes to the main branch using your Local machine's terminal integrated with an IDE (Visual Studio Code).

As you start working with GitHub repositories and automating workflows, using the web interface can be limiting and more time-consuming. This is where Git commands come in. You can use from your own desktop terminal or a Cloud IDE - wherever you develop your code.

In this article you will become familiar with git bash and various new Git commands to get started with branches using git commands on a local repository.

Estimated time : 25 mins

Objectives

After completing this tutorial you will be able to use git commands to work with branches on a local repository, including:

  1. create a new local repository using git init
  2. create and add a file to the repo using git add
  3. commit changes using git commit
  4. create a branch using git branch
  5. switch to a branch using git checkout
  6. check the status of files changed using git status
  7. review recent commits using git log
  8. revert changes using git revert
  9. get a list of branches and active branch using git branch
  10. merge changes in your active branch into another branch using git merge

Pre-requisites

If you intend to run this lab on your own system, please ensure you have Git Bash for Windows installed.

To complete this tutorial, you will need the following:

For more details on accomplishing this, review the next step "Installing Git on Windows".

Note: This hands-on lab does not require you to have created a GitHub account and added a project to it, as covered in the article How to get started with Git and GitHub or the follow-up tutorial How to get started with Branching & Merging using Github?, but feel free to read these articles if you want to learn more.

Installing Git on Windows

For Windows, the official build is available for you to download through the Git website. Clicking here will go to the download page automatically.

There is also an open-source project called Git for Windows, which is separate from the official Git website. This tool provides both command line and graphical user interface tools for using Git effectively on your Windows machine. For more information about this project and to inspect and download the code, visit the Git for Windows project site.

Once Git is fully installed, you can continue on to the section on Setting Up Git.

Setting Up Git

Now that you have Git installed, you need to do a few things so that the commit messages that will be generated for you will contain your correct information.

The easiest way of doing this is through the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do.

  • You can go ahead and add this information by typing:
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
Enter fullscreen mode Exit fullscreen mode
  • You can review all of the configuration items that have been set by typing:
$ git config --list
Enter fullscreen mode Exit fullscreen mode
  • This will be the output:
$ user.name=Your Name
$ user.email=youremail@domain.com
Enter fullscreen mode Exit fullscreen mode

There are many other options that you can set, but this is the essential one needed to prevent warnings in the future.

With Git installed and set up on your local machine, you are now ready to use Git for version control of your own software projects as well as contribute to open-source projects that are open to the public.

Hands-on Lab

1. Initialize: Open a new terminal window

Let's first open a terminal window in our IDE where we can start entering our shell and git commands. For this short tutorial I'll be using Visual Studio Code for Windows.

1 - Click on the Terminal menu to the right of this instructions pane and then click on New Terminal.

1

2 - This will add a new Terminal window at the bottom where you can start entering commands. Now at the ride-side after the plus (+) button click on the arrow down and choose bash.

Note: Before you can commit changes, you need to have git installed and tell git who you are. Please go to Installing Git on Windows and Setting up Git.

2

2. Create a new local repo

3 - Now let us create a new directory for your local repository.

Create a your-repo directory by copying and pasting the mkdir command below into the terminal:

$ mkdir your-repo
Enter fullscreen mode Exit fullscreen mode

3

4 - Go into the your-repo directory by copying and pasting the cd command below:

$ cd your-repo
Enter fullscreen mode Exit fullscreen mode

5 - In this your-repo directory lets create a new local git repository using the git init command. Copy and paste the command below into the terminal:

$ git init
Enter fullscreen mode Exit fullscreen mode

4

6 - 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:

$ ls -la .git
Enter fullscreen mode Exit fullscreen mode

The output shows the contents of the .git sub-directory which houses the local repo:

5

3. Create and Add a file to the local repo

7 - Now lets create an empty file using the following touch command:

$ touch newfile
Enter fullscreen mode Exit fullscreen mode

8 - Add this file to the repo using the following git add command:

$ git add newfile
Enter fullscreen mode Exit fullscreen mode

6

4. Commit changes

9 - Before you can commit your changes (if you haven't done it in the previous setps), you need to tell git who you are. You can do this using the following commands (replace your user name: first and last name and your user Email address):

$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
Enter fullscreen mode Exit fullscreen mode

10 - To confirm type the following commands:

$ git config --list
Enter fullscreen mode Exit fullscreen mode
  • This will be the output:
$ user.name=Your Name
$ user.email=youremail@domain.com
Enter fullscreen mode Exit fullscreen mode

11 - Once the repo has the newfile, let's commit your changes using the the following git commit command. Note that the commit requires a message which you include using the -m parameter:

$ git commit -m "added newfile"
Enter fullscreen mode Exit fullscreen mode

7

5. Create a branch

12 - Your previous commit created a default main branch called main.

13 - To make subsequent changes in your repo, create a new branch in your local repostitory. Copy and paste the following git branch command into the terminal to create a branch called yourfirstbranch:

$ git branch yourfirstbranch
Enter fullscreen mode Exit fullscreen mode

6. Get a list of branches and active branch

14 - Let's check which branches your repo contains by pasting the following git branch command into the terminal:

$ git branch
Enter fullscreen mode Exit fullscreen mode

15 - Note the output lists two branches - the default main branch with an asterix * next to it indicating that it is the currently active branch, and the newly created yourfirstbranch:

8

7. Switch to using a different branch

16 - Since you now want to work in the new branch issue the following git checkout command to make it the active branch to make your changes in:

$ git checkout yourfirstbranch
Enter fullscreen mode Exit fullscreen mode

17 - Let's verify that the new branch is now the active branch by issuing the following git branch command:

$ git branch
Enter fullscreen mode Exit fullscreen mode

18 - Note that the asterix * is now next to the yourfirstbranch indicating that it is now active:

9

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:

$ git checkout -b yourfirstbranch
Enter fullscreen mode Exit fullscreen mode

8. Make changes in your branch and check the status of files added/changed

19 - Lets make some changes in your new branch called yourfirstbranch. 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 your newfile." into the file:

$ echo 'Here is some text in your newfile.' >> newfile
Enter fullscreen mode Exit fullscreen mode

20 - Verify the text has been added by pasting the following cat command:

$ cat newfile
Enter fullscreen mode Exit fullscreen mode

10

21 - Now let's create another file called README.md using the following command:

$ touch README.md

Enter fullscreen mode Exit fullscreen mode

11

22 - And now add it to the repo with the following git add command:

$ git add README.md
Enter fullscreen mode Exit fullscreen mode

23 - So far in your new branch you have edited the newfile and added a file called README.md. You can easily verify the changes in your current branch using the git status command:

$ git status
Enter fullscreen mode Exit fullscreen mode

24 - 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 you added it to the branch using git add . However, even though you modified the file called newfile you did not explicitly add it using git add and hence it is not ready to be committed:

12

25 - 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:

$ git add *
Enter fullscreen mode Exit fullscreen mode

26 - Let's check the status again:

$ git status
Enter fullscreen mode Exit fullscreen mode

27 - The output now shows both the files can now be comitted:

13

9. Commit and review commit history

28 - Now that your changes are ready, you can save them to the branch using the following commit commmand with a message indicating the changes:

$ git commit -m "added readme.md modified newfile"
Enter fullscreen mode Exit fullscreen mode

29 - You can issue the following git log command to get a history of recent commits:

$ git log
Enter fullscreen mode Exit fullscreen mode

30 - The log shows 2 recent commits - the last commit to yourfirstbranch as well as the previous commit to main:

14

10. Revert committed changes

31 - Sometimes you may not fully test your changes before comitting them and may have undesirable consequences ... you can back out your changes by using a git revert command like the following. You can either specify the id of your commit that you can see from the previous log output or use the shortcut HEAD to rollback the last commit:

$ git revert HEAD --no-edit
Enter fullscreen mode Exit fullscreen mode

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 key.

32 - The output shows the most recent commit with the specified id has been reverted:

15

11. Merge changes into another branch

33 - Lets make one more change in your currently active yourfirstbranch using the following commands:

  • Let's check the status again:
$ git status
Enter fullscreen mode Exit fullscreen mode
  • then create file:
$ touch goodfile
Enter fullscreen mode Exit fullscreen mode
  • then add that file to git:
$ git add goodfile
Enter fullscreen mode Exit fullscreen mode
  • now add a commit message:
$ git commit -m "added goodfile"
Enter fullscreen mode Exit fullscreen mode

16

34 - Finally add git log. The output of the log shows the the newly added goodfile has been comitted to the yourfirstbranch branch:

$ git log
Enter fullscreen mode Exit fullscreen mode

17

35 - Now let's merge the contents of the yourfirstbranch into the main branch. You will first need to make the main branch active using the following git checkout command:

$ git checkout main
Enter fullscreen mode Exit fullscreen mode

36 - Now lets merge the changes from yourfirstbranch into main

$ git merge yourfirstbranch
Enter fullscreen mode Exit fullscreen mode

18

37 - Output shows the successful merging of the branch:

$ git log
Enter fullscreen mode Exit fullscreen mode

19

38 - Now that changes have been merged into main branch, the yourfirstbranch can be deleted using the following git branch command with the -d option:

$ git branch -d yourfirstbranch
Enter fullscreen mode Exit fullscreen mode

20

12. Practice on your own (optional)

  1. Create a new directory and branch called newbranch
  2. Make newbranch the active branch
  3. Create an empty file called newbranchfile
  4. Add the newly created file to your branch
  5. Commit the changes in your newbranch
  6. Revert the last committed changes
  7. Create a new file called newgoodfile
  8. Add the latest file to newbranch
  9. Commit the changes
  10. Merge the changes in newbranch into master

Summary

In this short tutorial, you have learned how to create and work with branches using git commands in a local repository. In subsequent articles I'll show, how you can synchronize changes in your local repository with remote GitHub repositories.

Git and GitHub learning resources

There are a lot of helpful Git and GitHub learning resources on the web. This is a short list of my favorites!

Conclusion

Congratulations! In this article, you learned how to:

  • Install git and setting up your personal details integrated with your IDE.
  • Create a new directory using your IDE terminal.
  • Initialize Git in your project directory.
  • Create a new file in your project directory.
  • Make new branch.
  • Create another file in your other branch.
  • Add the newly created file to your branch.
  • Commit the changes in your new branch.
  • Revert the last committed changes.
  • Add the latest file to your new branch.
  • Commit the changes.
  • Check the git status.
  • Get a history of recent commits.
  • Merge the changes in new branch.

Now you know more about the basics of Git and GitHub. Feel free to test them out to make sure you understand how they work.

Download this Git commands cheat sheet to have all Git commands in one place for future use.

What's Next?

If you want to follow-along with more advanced stuff in the next article I'll talk about Cloning and Forking GitHub Projects using Git commands that are the heart of workflows in Git-based version control systems like GitHub.

Thanks for the read! Now go practice & build something awesome!

Top comments (0)