These notes cover the basic git operations. The things covered here will be used day to day. These notes use the same repository as created in low level git.
The structure of a commit object is the same as a blob or a tree, the type, the size, the delimiter, and the content. The content of a commit contains the author name and email address, a pointer to a specific tree object, as well as a commit description and an optional parent commit hash. Because of this, we can easily move between or checkout different versions of our project using the git checkout command. This means the working directory and staging area will be updated.
The first thing to do is to set up the author details. Run the following commands, specifying the appropriate details:
git config --global user.name "<NAME>"
git config --global user.email "<EMAIL>"
This sets up default configuration for every project. We can override the configuration for a specific project by omitting the –global flag. Run the following command to examine the current configuration:
git config --list
If you intend to use github to host your repositories, it is recommended to set you local name and email to be the same as used for the github account.
Looking at the current state of our project using the git status command, we see that there are no commits but there are changes to be committed in the staging area. To create a commit, there must be changes in the staging area. Using the git commit command, create our first commit by running:
git commit -m "Our first commit in the project"
This gives us:
[master (root-commit) 4115956] Our first commit in the project
2 files changed, 2 insertions(+)
create mode 100644 file1.txt
create mode 100644 file2.txt
This tells us that we are on the master branch and making the first (root) commit. It shows the sha1 hash of the commit, our message, and tells us what changes we made. In this case, two new files with two pieces of text inserted. If we examine the commit object using git cat-file we see which tree the commit wraps around, the author and committer information, which is usually the same. The time of the commit as an Unix timestamp with the timezone, and the commit message.
Using tree -a, we wont see any new files in the objects directory because the blobs were created in the previous blog.
An overview of some basic git operations:
- git status: Shows the current state of the git repository.
- git add: Add files from working directory to the staging area.
- git commit: Write changes from staging area to git repository.
- git log: Shows the history of commits made to the git repository.
- git checkout: Checkout a branch or commit into the staging area and working directory.
The normal process for creating a new file in a project is to create it in the working directory, stage it and commit it. Run the following commands:
echo "Some text" > file3.txt
git ls-files -s
git status
The output of ls-files will show us that the new file is not in the staging area, it is only in the working directory. The output of status will show us a new added but untracked file.
There are four states in the git file lifecycle:
- Untracked: A file has been created in the working directory.
- Modified: A file has been changed in the working directory.
- Staged: A new or modified file has been added to the staging area.
- Unmodified: A file has been committed and not changed.
To move from untracked or modified to staged, we use the git add command and to move from staged to unmodified, we use the git commit command.
To stage the file, we use the git add command. Run the following code:
git add file3.txt
or
git add .
The . in the second command adds all untracked and modified files to the staging area. If we examine the staging area with git ls-files, we will see our new file has been added. We can unstage a staged file using the got rm command. Run the following commands:
git rm --cached file3.txt
git ls-files -s
The git rm –cached command does not remove the file from the working directory. After running this command, it will be untracked. Re-add the file by using git add. Run git commit:
git commit -m "Second commit"
The file transitions to unmodified state. A new blob for file3.txt is created in the git repository as well as a new tree object and new commit object.