Hello! Welcome to my beginner's guide to Git. For the past two weeks, Mod 0 Session 2B has been working on familiarizing ourselves with some entry-level intricacies of Git. Below you find some basics that I've learned along the way.
Git is a resource that is used to archive and track edits/updates of files as they are saved and updated. Simply put, it is a Version Control System (VCS). By using Git, you will be able to look at earlier versions of files at each point which you've saved.
To begin, have a file in mind for which you would like to track changes. Assuming you have already downloaded Git to your terminal, let's begin by opening your terminal.
Once you have opened terminal, enter into the directory that contains the file you would like to be tracked. For example, if I have a directory called "Tea", and "Tea" contains files I'd like to track, I would want to enter the directory by typing cd Tea into the command line.
From here, we want to let Git know that we'd like for this directory to be tracked. To do this, we must run the command git init. After running this command, Git knows that this is the directory that you would like to be tracked, and Git will track all of the files that are present in this directory. Note that if there is an empty folder in a directory you'd like to track, Git will ignore it.
If we want to be sure that Git is indeed tracking the directory we've asked it to track, we may run the command git status to prove that it is doing what we hope it will. When we run the command, we should see a message along these lines:
acerveny~/Tea$ git init
Initialized empty Git repository in /Users/acerveny/Tea/.git/
acerveny~/Tea$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
black_tea.txt
green_tea.txt
white_tea.txt
nothing added to commit but untracked files present (use "git add" to track)
acerveny~/Tea$
Excellent; this is exactly what we want to see! Breaking down the message, we can see that there have been no commits yet (true! we will get to that in a minute), and we can see that our directory contains three untracked files. Remember, just because we have initialized Git does not mean that it has been tracking our files. Rather, we have directed it to the location of files we want to track.
So, in summary, we have gone the following:
- opened to the directory that contains files we would like to track
- ran
git init, which points Git to where our files are that we'd like to track. - ran
git status, which tells us what files Git is planning to track, once they've been added.
When we are ready to specify a file to be tracked, we enter the following into the commandline: git add <filename>. This brings our file to what is referred to as the 'staging area'. The staging area is where files are kept before we are ready to 'commit' them. When we commit a file, we are formalizing the changes in a version, so the staging area is where we place files to make sure everything is accounted for before formalizing them as a 'commit'.
Let's say we want to add black_tea.txt to the staging area. To do this, we would run git add black_tea.txt. After this is code has been run, it would be good practice to run git status. It should return the following:
acerveny~/Tea$ git add black_tea.txt
acerveny~/Tea$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: black_tea.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
green_tea.txt
white_tea.txt
Again, this is exactly what we want to see. We have instructed Git to add black_tea.txt to the staging area, and it is now being tracked for changes. We have not added any changes, so "No commits yet" is accurate. Since this is the original version of the file, sans edits, we should formalize this version of the file through a 'commit'.
When we are ready to formalize this version of the file, we must tell Git that we are ready to 'commit'. We do this by running the command git commit -m <short message to describe version>. As this is the original file, and no edits have been added, we should indicate this by following -m with the text 'Initial commit'. This signals to us, as well as others that may be working on this project, that this version is the original. It should return the following:
acerveny~/Tea$ git commit -m "Initial commit"
[master (root-commit) 1c747c9] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 black_tea.txt
acerveny~/Tea[master ?]$
Congratulations! You have entered your first Git commit.
Let's reflect on what we've done:
- initialized a directory to be tracked;
- added a file to be tracked; and
- commited a file
Nice work so far! Thank you for stopping by my beginner's guide.