Introduction to Git and Git Commands (Set up Your Repository)

 

Set up Your Repository

Introduction to Git and Git Commands

Create your repository

We will start this tutorial with an example, let's create a new repository in your github account. We will be doing different types of possible operations to the repository and hence covering different git commands along the way. 


Simply go to Your github account and create a repository. In my case I am creating a repository called git_command_tutorial. The link to my git repository is given below;




        Following figure shows the current status of the repository we created;


  
Newly created github repository
Figure 1: Newly created github repository


This is one of the ways, if you want to work on someone else’s repository then you will need to fork their repository in your github account. Once you are done with this step and you have a repository to work on you can proceed with the further steps.




Add a New Code File To This Repository From Your PC

Adding a new code file to your repository is completed in the following steps

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;
  
git clone command in action
Figure 2: 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.


  
Cloned folder containing the readme file we created
Figure 3: 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.

Make The Changes You Need To The Cloned Repository

        In my case I am going to create a python file with some lines of code in it, to keep things simple I will add some simple print commands;


File name:  
file1.py


File Content:


print(‘This is line one!!’)


print(‘This is line two!!’)


I am going to use the terminal to add this file as well. You can add the file manually as well if you want. To add the file we use the following command;
        
sudo nano <filename with extension>


In our example: 👇


sudo nano file1.py

Command to create a new file
Figure 4: Command to create a new file


        Once you use this command it will ask you for password, put in the password and proceed further. A new layout will open where you will fill the line of code you want to fill.


  
Layout to fill in the content of the file
Figure 5: Layout to fill in the content of the file


After filling the content of the file, you will need to press control + X , followed by Y. The terminal will then ask for the file name, once you put in the file name you have to press enter. Following figure shows the file name window.


  
layout to fill in the desired file name
Figure 6: layout to fill in the desired file name


Once you are done with the file name, you can check the folder again, you will find the newly created file.


  
Newly created file1.py file
Figure 7: Newly created file1.py file



Creating The .gitignore File

        A .gitignore file is used to hold the name of the files you want Git to ignore. Usually a .gitignore file is created locally for every repository, but one can add a global .gitignore file to ignore the required files in the entire set of repositories. 
        Before we add the .gitignore file, let's add a dummy file to ignore. We will create a file named dummy_data.txt file. Once done we can add the .gitignore file. To add the dummy text file and .gitignore file we will again use the following command;


sudo nano dummy_data.txt


sudo nano .gitignore
        
Following figure shows the content of the .gitignore file;
  
Contents of the .gitignore file
Figure 8: Contents of the .gitignore file


        Now the .gitignore file has been added, we can proceed to the next step now. 


Check The Status of Your Files

        Checking status is a necessary step to ensure every untracked file. It will show you all the files which are not tracked and are needed to be added in the tracker.
        Command for checking the status is shown below;


git status


        The result generated by the above command is shown in the following figure;
  
Result of the git status command
Figure 9: Result of the git status command


Stage The Changes

        Once we have successfully finished all the above mentioned steps we can proceed with staging the changes and then making a commit in the main repository. To stage the changes we will execute the following command in the terminal;


git add .


The output will look something like below;


  
Result of the “git add .” command
Figure 10: Result of the “git add .” command




Commit Your Changes to Your Local Repository

Now since everything is added as it should be, we need to commit the changes in the local repository. While committing we can add a message as well. The message is usually regarding the changes. For example we can say that “added the code file” or “made some changes in the code file regarding the visuals” or something else which explains the changes you made for that commit.


git commit -m <message you want to write>


In our example: 👇


git commit -m "added a new file file1.py"


Once you execute the above command you will see the following output;
  
Result of the “git commit -m "added a new file file1.py"” command
Figure 11: Result of the “git commit -m "added a new file file1.py"” command




Push The Commit into the online repository

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


git push origin master


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


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



        Now we are finished with the complete process of managing a new file into a git repository. Although this is not limited to a new file only, the changes are also viable for updation in an existing file. Now if we look at our repository it will look something like this;


  
Github repository after all the steps
Figure 13: Github repository after all the steps


Thanks
I hope this article was helpful for you, for more of such articles you can follow my website and youtube channel 

0 Comments