Git Basics 01
1- Installation and setup
The most common way to use Git is via a command-line program called git, which lets us transform an ordinary Unix directory into a repository (or repo for short) that enables us to track changes to our project
Check if Git is already installed: start the terminal window and use
which
(you should have already installed this when installing linux)If its not there, install Git: You can either use some pacakage management tool or depending on your distro use the command line to install it. For example on Ubuntu you would write:
After installing Git but before starting a project, we need to perform a couple of one-time setup steps for Git to identify your changes by name and email
Git stores global configuration settings in a hidden text file located in your home directory. By inspecting the file ~/.gitconfig with a tool of your choice (cat, less, a text editor, etc.), confirm that the configuration you set up corresponds to simple text entries in this file.
2- Initializing the repo
Start creating a project and put it under version control with Git
Make a directory with the generic name website inside a repositories directory called repos using the following command:
Even though the website directory is empty, we can already convert it to a repository, which is like an enhanced directory with the additional ability to track changes to every file and subdirectory. The way to create a new repository with Git is with the
init
command, which creates a special hidden directory called .git where Git stores the information it needs to track our project’s changes.
3- Making our first commit
Git won’t let us complete the initialization of the repository while it’s empty, so we need to make a change to the current directory.
Create a main page index.html using the following command:
Use the
git status
command to see the result. It would look something like this:A file that is “untracked” means Git doesn’t yet know about it. We can add it using the git add command
Use the git add command with
-A
or with.
which tells Git to add all untracked files.or
The status of the file has now been promoted from untracked to staged, which means the file is ready to be added to the repository.
After putting changes in the staging area, we can make them part of the local repository by committing them using
git commit
. You can think of a commit as a snapshot of the Git repository. Use the command-line option-m
to include a message indicating the purpose of the commit.Use
git log
to see a record of your commit:A commit is identified by a hash, which is a unique string of letters and numbers that Git uses to label the commit and which lets Git retrieve the commit’s changes.
This is the main Git status sequence for changing a file:
3- Viewing the diff
It’s often useful to be able to view the changes represented by a potential commit before making it.
Let us add something to our index.html file like the text
hello, world
.Lets now use
git diff
, which by default just shows the difference between the last commit and unstaged changes in the current project:
Having a Git repository for your project is very useful, but lets take it up a level and see how we can use Git for collaboration. Get to the next documentation.
Last updated
Was this helpful?