Posted on • Originally published atyuscode.hashnode.dev
Chapter 2 - Get Started With Git
Table of Contents
- Initializing a Local Git Repository
- Setting up Your Git Identity
- Adding Files to the Staging Area
- Committing Your Files
- How to Revert to Previous Commits
- Conclusion
Initializing a Local Git Repository
Open a terminal and create a folder. For this tutorial, let's create a folder called
countries
.mkdircountries
Navigate into the
countries
directory you just created.cdcountries
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 thecountries
directory, simply run the code belowgit init
Create a file inside the directory. For this tutorial, we'll create a file named
Senegal
.touchSenegal
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 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
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
When you rungit status
again, you will notice that theSenegal
file has been added to the staging area.
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"
Let's go ahead and create two more files;Canada
andIndia
.
touchCanada India
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.
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.
Next, let's commit these changes:
git commit-m"second country and third country"
If you want to see what commits you have made, simply run:
git log
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.
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
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
To confirm that theCanada
file's contents have been reverted to the original state, run:
catCanada
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
NB: The hash of your last commit are those yellow alpha-numeric characters you see if you rungit log
.
For example, if the hash of your lastCanada
file commit is 9064aff69803d0536059daaadad89fd607ef5c3a, you can revert to the previous state by running:
git revert 9064aff69803d0536059daaadad89fd607ef5c3a
Finally, if you want to remove everything inside your current directory from the staging area, simply run:
gitrm--cached-r.
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)
For further actions, you may consider blocking this person and/orreporting abuse