Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published atyuscode.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 calledcountries.

    mkdircountries
  • Navigate into thecountries directory you just created.

    cdcountries
  • Next, initialize (create) a local Git repository in thecountries 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 thecountries directory, simply run the code below

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

    touchSenegal
  • Open theSenegal 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 isexample@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 modeExit fullscreen mode

Image description

As you can see from the output, the fileSenegal 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 modeExit fullscreen mode

When you rungit status again, you will notice that theSenegal 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 isgit commit -m "commit message". Let's commit theSenegal file.

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

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

touchCanada India
Enter fullscreen modeExit fullscreen mode

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

If you rungit 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 commandgit 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 modeExit fullscreen mode

Image description

Next, let's commit these changes:

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

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

git log
Enter fullscreen modeExit 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 fromThe capital of Canada is Ottawa toThe capital of Canada is Toronto. Save this changes and exit the text editor. If you rungit status, it will output the modified file in red color.

Image description

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

git diff Canada
Enter fullscreen modeExit fullscreen mode

Image description

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

To revert to the previous saved version ofCanada file, you can use thegit checkout filename command.

git checkout Canada
Enter fullscreen modeExit fullscreen mode

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

catCanada
Enter fullscreen modeExit fullscreen mode

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

git reverthash/PID of last commit
Enter fullscreen modeExit fullscreen mode

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

Image description

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

git revert 9064aff69803d0536059daaadad89fd607ef5c3a
Enter fullscreen modeExit fullscreen mode

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

gitrm--cached-r.
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hello. My name is Yusuf Isah, and I am a DevOps enthusiast from Nigeria. I am also passionate about Technical Writing.
  • Joined

More fromYusuf Isah

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp