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).
git config --global user.name "<your_name>"
git config --global user.email "<your_email@example.com>"
Tip
It’s highly recommended that you set <your_name>
to be your GitHub username and set <your_email@example.com>
to be your GitHub no-reply email, which looks like ID+USERNAME@users.noreply.github.com
. 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.
# default editor
git config --global core.editor 'code --wait'
# default diff tool
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
# default merge tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
Learn more:
Getting Help
To get help when using Git, run:
git help <verb>
# Or
git <verb> --help
# Or
man git-<verb>
Getting A Git Repository
Creating One In An Existing Directory
To create a new Git repo in the current directory, run:
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:
git add <directory>/<file>
To undo git add
(unstage file(s)):
- Use
git reset
:
git reset # unstage everything
git reset <file> # unstage specific file(s)
- Use
git restore
:
git restore --staged <file> # unstage specific file(s)
git restore --staged :/ # unstage all files
- Use
git rm
:
git rm --cached <file>
[!INFO]
git reset
is equivalent togit restore --staged :/
.git reset <file>
is equivalent togit restore --staged <file>
. Butgit rm --cached <file>
is unique. Actually, it means remove<file>
from theStaging Area
, while the other two commands move<file>
in theStaging Area
back to theWorking Directory
.
Checking Status
To check the status of the files in the repo, run:
git status
# A Short Version
git status -s
# Or
git status --short
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
# shows unstaged, uncommitted changes
git diff
# shows all uncommitted changes (including staged ones):
git diff HEAD
# shows only staged changes
git diff --staged
git diff --cached
Committing
To commit your files, run:
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:
git config --global core.editor <path_to_editor_or_command_to_open_editor>
Or, you can simplify this by using -m
option:
git commit -m <commit_message>
Oops, I forgot to add this file! If you meet this problem, you can amend it like this:
git commit -m "a wrong commit message"
git add <forgotten_file>
git commit --amend
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:
git commit -a
This command is equivalent to these two command bellow:
git add <all_tracked_files_that_have_been_modified>
git commit
[!INFO] The links below maybe helpful if you want to write better commit messages:
History
To review the commit history, run:
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
[!DANGER] 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:
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
git clean # deletes files that are not tracked by Git
git clean -d # deletes directories
git clean -x # deletes untracked files, including ignored files in `.gitignore` and `.git/info/exclude`