Simple Git Tutorial
Published at 2023-03-11
Updated at 2024-11-12
Licensed under CC BY-NC-SA 4.0
git
version-control
software-engineering
tool
collaboration
Table of Content
- Brief
- Installing Git
- Basic Configuration
- Getting Help
- Getting A Git Repository
- Creating One In An Existing Directory
- Cloning One from An Existing Repo
- Files’ States in a Git Project
- Tracking Files
- Checking Status
- Checking Differences
- Committing
- History
- Discarding Changes in Working
- Cleaning Files and/or Directories
- Next Steps
- Resources
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.
Installing Git
Download Git from the official website:
Or use your favorite package manager by yourself. For example, use apt
:
sudo apt update
sudo apt install git
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 "<[email protected]>"
Tip
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.
# 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>
For example, if you are confused with git add
, run:
git help add
Getting A Git Repository
It’s time for us to prepare a git repository. Here are two choices:
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
.
When a commit is made, a commit record is generated, which you can think of as a ‘snapshot’ of your code repository.
Tip
Please don’t misunderstand what is meant by ‘commit’ here! Everything here happens in your local Git repository.
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>
Note
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:
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
Note
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
Caution
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`