Introduction to Git and Git Commands (Working with Branches)

Working with Branches

Working with GitHub Branches

Edit The Repository Using Branches

In the last tutorial we learned how to add a new file in our github repository, I hope it worked out well. If you have not seen it yet here is a link to that article (Set up your repository). In this article we will learn about the further techniques of repository updation, In the last article we made all the modifications in the main repository itself. Sometimes it is not a very good decision to make all the changes in the main repository.  You might need to roll back all the changes while keeping some of the intermediate commits, especially if multiple people are working on the same project. Creating the branch helps you keep the development and changes of all the different contributors separate, until you finally decide to merge them all.

We will learn how to create a branch and make modifications to it. For this tutorial we will not be creating a new repository, we will use the last repository we created.

The following link can get you to that repository;

https://github.com/ap539813/git_command_tutorial


Before we make any more steps, it is important to create the clone of the repository in your local system. To create the clone if you don't already have one, follow the following command.



Make a Clone Of The Repository in Your PC

First step will be to start with cloning the repository in your PC, here I am assuming that you already have github installed in your computer. To make the clone of the repository in your PC, follow the following steps;

  1. Go to the desired folder location in your PC or create a new folder

  2. Creating a new folder for a new project is a good practice as it keeps the development neet and clean

  3. Open the terminal in that folder location

  4. You need to write the following command and hit enter


git clone <link of the repository>


In our example: 👇


git clone https://github.com/ap539813/git_command_tutorial


Once the repository is cloned in your PC, you will be able to make changes to the repository locally and then push and commit those changes in the github repository from your terminal.

In my case I am doing all this stuff on my desktop folder. You can follow the following screenshots for simple references;

Figure 1: git clone command in action

Figure 1: git clone command in action


The cloned folder must contain the files contained in the github repository. Following image shows the cloned repository in our case.


Figure 2: Cloned folder containing the readme file we created

Figure 2: Cloned folder containing the readme file we created


Once you have cloned the repository, it’s time to get into the same folder and take some action.



Now let’s start with the further steps of the modification.

Create a Branch

Start by creating a new branch to the repository, there is no restriction for picking a name for the branch but, it is considered as the best practice to keep the branch name matching with the type of modification or update you are about to perform. In our case we are keeping it as doc_change

To create the branch we can use the following command;


git branch <name of branch>


In our example: 👇


git branch doc_change


Let’s look at the github page before the branch has been created;


Figure 3: Only one branch “main” can be seen

Figure 3: Only one branch “main” can be seen


After executing the command we must see a new branch created in the repository. Now it's time to execute the command. Note that to execute the command you should have the repository cloned in your local system and your terminal should be opened in the same repository. 


Figure 4: Terminal output of git branch command

Figure 4: Terminal output of git branch command


Switch To The doc_change Branch


To make sure that all the changes we make in the files appear in the branch, we need to switch to the branch we just created. To switch to a branch in github is very simple. We can simply use the following command;


git checkout <name of branch>


In our example: 👇


git checkout doc_change


Once we run this command, all the changes will appear in the created documentation. The output after executing the command is shown in the following figure;


Figure 5: Output of the git checkout command

Figure 5: Output of the git checkout command



Make Some Changes in The Code


Our next step is to make some changes in the repository. The change can be made in any file of your choice but I am making the change in the readme file. To make the changes either we can go into the folder of the repository and modify the file using a text editor or we can use command prompt to do the same. I will make the changes using the command prompt;


cat README.md && echo "Added another line to REAMD.md" >> README.md && cat README.md && git status


To make the changes in the readme file I have used the above command, it will modify the readme file and then show the status of the branch as well.


The output of the above mentioned command can be seen in the following figure;


Figure 6: Output of the command for updating the readme file

Figure 6: Output of the command for updating the readme file



Commit the Changes We Just Made


Now since all the changes are made successfully we can commit the changes in the branch. Just like the last tutorial we will make the commit using the terminal commands.

In the last tutorial we did the staging of the changes and the commit in two different steps, but in this tutorial we will perform the two operations in the combined command. The command we will use for the staging the changes and commit is shown below;


git add . && git commit -m <message in quotes>


In our example: 👇


git add . && git commit -m "Modified the readme file"


The output of the above command can be seen in the following figure, ;


Figure 7: Output of the git add . && git commit -m <message in quotes>

Figure 7: Output of the git add . && git commit -m <message in quotes>


Now we are officially done with all the changes in the branch. It's time to push all the commits in the remote branch now.




Push The Commit into the online repository

To make the changes appear on the github repository (branch) we need to push the commit we made on the github repository (branch). To push the commit we use the following command;


git push origin doc_change


Once executed this command all the changes we made will appear on the github repository (branch) we made in the beginning. You can refer to the following figure for the output on terminal after executing the above command.


Figure 8: Result of the “git push origin main"” command

Figure 8: Result of the “git push origin main"” command



Now if we head to the github repository we will be able to see the branch there. The repository page also shows the extra commits we have done in the doc_change branch. Now if we look at our repository it will look something like this;


Figure 8: Result of the “git push origin main"” command

Figure 8: Result of the “git push origin main"” command



Thanks

In this repository we have seen how to make branches, how to update the files in the branches and how to make commits and push in the branches. In the upcoming tutorials we will see the other advanced stuff we can perform using github.



for more of such articles you can follow my website and youtube channel 

Website: https://www.pandeysblogs.com

Youtube channel: https://www.youtube.com/channel/UCriLPQPpmOGP18he5aAlPhQ


0 Comments