Simple Git Tutorial
This is mainly a simple tutorial of Git. Some info about GitHub also included. You may also read Simple GitHub Tutorial.
Brief
You may have learnt Git and GitHub before, and also may have confused them because their similar names. What are these two things on earth?
In short, Git is a Version Control System (VCS), which enables you to create a repository of your codes, to track changes in your project and to manage your files efficiently. GitHub is a platform used to host remote Git repositories for collaboration and sharing projects.
In this post, you will learn the basic usage of Git. If you are looking for info about working with GitHub, see _Simple GitHub Tutorial.
Want to go deeper? See Resources.
Installing Git
Download Git from the official website:
Or use your favorite package manager by yourself.
Basic Configuration
Before starting to use Git, you should tell Git your name and email. The commands below set your name and email globally (which means they are visible to all your local repositories).
1 | git config --global user.name "<your_name>" |
It's highly recommended that you set <your_name>
to be your GitHub username and set <[email protected]>
to be your GitHub no-reply email, which looks like [email protected]
. You can find the no-reply email here (under the Primary email address
section).
Learn more:
You may also set your default editor and default diff and merge tools. Here I set to VS Code.
1 | # default editor |
Learn more:
Getting Help
To get help when using Git, run:
1 | git help <verb> |
Getting A Git Repository
Creating One In An Existing Directory
To create a new Git repo in the current directory, run:
1 | git init |
This command will create a directory called .git
.
Cloning One from An Existing Repo
Files' States in a Git Project
There are three main states in Git: modified, staged, and committed. And this leads us to three main sections of a Git project: the Working Directory
, the Staging Area
and the Git Directory
.
You modify your files in the Working Directory
, stage them (using git add
) to the Staging Area
, and commit them (using git commit
) to the Git Directory
.
Also see Git - What is Git?.
Tracking Files
To add file(s) to your tracked list (or more precisely, put file(s) onto the Staging Area
), run:
1 | git add <directory>/<file> |
To undo git add
(unstage file(s)):
- Use
git reset
:
1 | git reset # unstage everything |
- Use
git restore
:
1 | git restore --staged <file> # unstage specific file(s) |
- Use
git rm
:
1 | git rm --cached <file> |
git reset
is equivalent to git restore --staged :/
. git reset <file>
is equivalent to git restore --staged <file>
. But git rm --cached <file>
is unique. Actually, it means remove <file>
from the Staging Area
, while the other two commands move <file>
in the Staging Area
back to the Working Directory
.
Checking Status
To check the status of the files in the repo, run:
1 | git status |
In the short version, there are two columns in the front of every line. The left column shows the status of the Staging Area
. The right column shows the status of the Working Directory
. For example:
??
: untracked fileA
: file newly added to the stageM
: modified file
Checking Differences
1 | # shows unstaged, uncommitted changes |
Committing
To commit your files, run:
1 | git commit |
This command will open a editor. You need to edit your commit message here. Use the command below to set a default editor:
1 | git config --global core.editor <path_to_editor_or_command_to_open_editor> |
Or, you can simplify this by using -m
option:
1 | git commit -m <commit_message> |
Oops, I forgot to add this file! If you meet this problem, you can amend it like this:
1 | git commit -m "a wrong commit message" |
The second commit will replace the first commit, as if the first commit never occurred.
If you are tired of using the stage (git add
first,git commit
later), you can use -a
flag like this to skip it:
1 | git commit -a |
This command is equivalent to these two command bellow:
1 | git add <all_tracked_files_that_have_been_modified> |
How to write better commit messages?
The links below maybe helpful if you want to write better commit messages:
History
To review the commit history, run:
1 | git log |
-p
or--patch
flag make logs output in the patch way.-<n>
flag to limit the number of logs (e.g.-2
only show two latest logs).--stat
flag shows simple statistics.--pretty
option provides some different ways to show the history.--pretty=oneline
option make every commit be showed in one line--pretty=short
--pretty=full
--pretty=fuller
--graph
flag adds some ascii characters to vividly show your history.
Discarding Changes in Working
In Git, it's usually easy to restore the files have been committed, but usually very hard to restore the files haven't been committed.
To discard changes in working, run:
1 | git checkout -- <file> |
This command will abandon the modifications to the file after your last commit, where --
indicates that the following content should be treated as file parameters, even if they resemble options.
Cleaning Files and/or Directories
1 | git clean # deletes files that are not tracked by Git |