DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published at yuscode.hashnode.dev

Chapter 2 - Get Started With Git

Table of Contents

Initializing a Local Git Repository

  • Open a terminal and create a folder. For this tutorial, let's create a folder called countries.

    mkdir countries
    
  • Navigate into the countries directory you just created.

    cd countries
    
  • Next, initialize (create) a local Git repository in the countries directory. But why is this important? When you initialize a local Git repository, Git creates a new subdirectory called ".git" in the current directory. The ".git" directory contains all the necessary files and metadata for Git to track changes and manage versions of your project. To initialize a local Git repository in the countries directory, simply run the code below

    git init
    
  • Create a file inside the directory. For this tutorial, we'll create a file named Senegal.

    touch Senegal
    
  • Open the Senegal file using your favorite text editor (Nano or Vim). Once you have opened the file, write the following inside it: The capital of Senegal is Dakar. Next, save what you have written and then exit the text editor.

Setting up Your Git Identity

Setting up your Git identity is important because the information is used to identify the author of commits in your Git repository.

  • git config --global user.name "[name]". This command sets the username associated with your Git commits. For example, if your name is Jordan, then your code would look like this:

    git config --global user.name "Jordan"
    
  • git config --global user.email "[email address]": This command sets the email address associated with your Git commits. For example, if your email address is example@yahoo.com, then your code would look like this:

    git config --global user.email "example@yahoo.com"
    

Adding Files to the Staging Area

In Git, the staging area (also known as the index) is a temporary holding area where you prepare changes to be committed. It's like a "draft" version of your changes before you finalize them. If you have been following this tutorial, there should be only one visible file "Senegal" in the "countries" directory thus far. Before adding the "Senegal" file to the staging area, let's first find out which file/files are currently in the staging area. We can do this through the following command:

git status
Enter fullscreen mode Exit fullscreen mode

Image description

As you can see from the output, the file Senegal is untracked, meaning it's yet to be added to the staging area. To add it to the staging area, simply run:

git add Senegal
Enter fullscreen mode Exit fullscreen mode

When you run git status again, you will notice that the Senegal file has been added to the staging area.

Image description

Committing Your Files

When you commit your files, you save the changes you've made to your files in the staging area to your local repository. The commit command is git commit -m "commit message". Let's commit the Senegal file.

git commit -m "first country"
Enter fullscreen mode Exit fullscreen mode

Let's go ahead and create two more files; Canada and India.

touch Canada India
Enter fullscreen mode Exit fullscreen mode

Open both files one after the other in a text editor. Inside the Canada file, write, The capital of Canada is Ottawa. Inside the India file, write, The capital of India is New Delhi. Save both files and then exit.

If you run git status, you will notice that two files (Canada and India) are untracked. They are only in the working directory and not yet in the staging area.

Image description

To add both files to the staging area, you can run the command git add [file_name] for each of the files. However, this can be cumbersome especially if you want to add numerous files to the staging area. Thankfully, a simple code helps us add all files in a directory to the staging area. Simply run:

git add .
Enter fullscreen mode Exit fullscreen mode

Image description

Next, let's commit these changes:

git commit -m "second country and third country"
Enter fullscreen mode Exit fullscreen mode

If you want to see what commits you have made, simply run:

git log
Enter fullscreen mode Exit fullscreen mode

Image description

How to Revert to Previous Commits

To demonstrate how to revert to previous commits, let's open up one of the files, e.g., Canada in a text editor and change its content from The capital of Canada is Ottawa to The capital of Canada is Toronto. Save this changes and exit the text editor. If you run git status, it will output the modified file in red color.

Image description

If you want to see the difference between the current Canada file and the previous saved one, you can use the git diff filename command.

git diff Canada
Enter fullscreen mode Exit fullscreen mode

Image description

The original content of the Canada file is shown in red color, while the new content is shown in green color.

To revert to the previous saved version of Canada file, you can use the git checkout filename command.

git checkout Canada
Enter fullscreen mode Exit fullscreen mode

To confirm that the Canada file's contents have been reverted to the original state, run:

cat Canada
Enter fullscreen mode Exit fullscreen mode

If you have already committed the wrong Canada file and you want to revert back to the original, simply run:

git revert hash/PID of last commit
Enter fullscreen mode Exit fullscreen mode

NB: The hash of your last commit are those yellow alpha-numeric characters you see if you run git log.

Image description

For example, if the hash of your last Canada file commit is 9064aff69803d0536059daaadad89fd607ef5c3a, you can revert to the previous state by running:

git revert 9064aff69803d0536059daaadad89fd607ef5c3a
Enter fullscreen mode Exit fullscreen mode

Finally, if you want to remove everything inside your current directory from the staging area, simply run:

git rm --cached -r .
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this chapter, we covered how to initialize a local Git repository, set up a Git identity, add files to a staging area, commit your files, and also how to revert to your previous commits. Remember, committing changes only saves them to your local repository. To share changes with others, you'll need to push them to a remote repository using git push. We'll cover this in the next chapter.

Feel free to comment and share this article. Please follow my blog for more insights on Git!

Top comments (0)