Git User Manual
# Git User Manual
# Installation
Download from the official website: https://git-scm.com/downloads (opens new window). Use the default settings during installation.
After installation, find Git -> Git Bash in the Start menu. If a command-line-like window pops up, Git has been installed successfully!
One last step of configuration is needed. Enter the following in the command line:
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
2
Since Git is a distributed version control system, every machine must identify itself with your name and email address.
# Creating a Repository
# Create a repository
mkdir <repo-name>
# Enter the repository
cd <repo-name>
# Display the current directory
pwd
# Initialize the current directory as a Git repository
git init
2
3
4
5
6
7
8
9
10
11
You don't have to create a Git repository in an empty directory. You can also run
git initin an existing directory with files.
# Adding Files to a Repository
Place files in the repository directory. For example, create a new test.txt file, then use git add test.txt to tell Git to add the file to the staging area, and then use git commit -m "commit description" to tell Git to commit the file to the repository.
# Add a specific file or folder to the staging area (files need extensions)
git add <file-or-folder-name> # Single file
git add <file-or-folder-name> <file-or-folder-name> # Multiple files
# Or add all files to the staging area at once
git add .
# Commit files from the staging area to the repository
git commit -m "commit description"
2
3
4
5
6
7
8
9
# Commit Message Conventions
Writing clear descriptions when committing code is beneficial for version management. It's recommended to use the following keywords in commit messages:
# add
# rm (remove)
# update
# change
# implement
# release
# fix
...
2
3
4
5
6
7
8
# Version Management
# Committing Changes
After modifying test.txt, run git status to see which files have been modified, then use git add test.txt and
git commit -m "change description" to commit the modified file to the repository. After committing, you can use git status again to check the current status.
# Show files that are new/deleted/modified
git status
2
# Viewing Version History
# View version history
git log # Shows version numbers, commit times, and other information
2
You can also use a GUI tool to view Git version history: Right-click in the repository directory >
Git GUI Here
# Reverting to a Previous Version
First, Git needs to know which version is the current one. In Git, HEAD represents the current version, which is the latest commit 1094adb... (note that your commit ID will be different from mine). The previous version is HEAD^, the one before that is HEAD^^, and going back 100 versions by writing 100 ^ characters would be impractical, so you write HEAD~100 instead.
# Revert to the previous version
$ git reset --hard HEAD^
2
At this point, checking git log reveals that the latest version is gone. How do you get back to the original latest version? You need to know the version number of that latest version.
# Jump to a specific version
git reset --hard <first-few-digits-of-version-number>
2
But what if you don't know the version number? Git provides a command git reflog to record every command you've used.
git reflog
# Undoing Changes
https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536 (opens new window)
# Deleting Files
https://www.liaoxuefeng.com/wiki/896043488029600/900002180232448
# Remote Repositories
# SSH Authentication
To allow your machine to connect to remote repositories, SSH authentication is required for the first time.
- Step 1: Create an
SSH Key. In your home directory (C:\Users\dell), check if there is a .ssh directory. If there is, check if it containsid_rsaandid_rsa.pubfiles. If they already exist, skip to the next step. If not, open Shell (on Windows, open Git Bash) and create anSSH Key:
# Create SSH Key
ssh-keygen -t rsa -C "your-email-address"
2
- Step 2: Log in to GitHub, go to your avatar (top right) > Settings > SSH and GPG keys > Add SSH Key, and paste the contents of the
id_rsa.pubfile into the key text box.
# Linking a Remote Repository
After SSH authentication is complete, create a repository on GitHub. Remember to uncheck Initialize this repository with a README when creating the repository, then run the following locally:
# Link a remote repository (the repository name is typically 'origin')
git remote add <repo-name> <remote-repo-url>
# Example
git remote add origin git@github.com:xugaoyi/test.git
2
3
4
5
Next, you can push all local repository contents to the remote repository:
# Push files to the remote repository
git push -u <repo-name> <branch-name>
# Example
git push -u origin master
2
3
4
5
Since the remote repository is empty, when we push the master branch for the first time, we add the -u parameter. Git will not only push the local master branch to the new remote master branch, but also link the local master branch with the remote master branch, simplifying future push and pull commands.
Prerequisites: The directory must already be initialized as a repository with
git init, andgit statusshould show no changes. If there are changes, first usegit add .to add them to the staging area, thengit commit -m 'commit description'to commit to the repository, and then execute the above commands.If you checked
Initialize this repository with a READMEwhen creating the repository, you need to pull theREADME.mdfile to your local repository first withgit pull.You can link multiple remote repositories. Make sure to give different remote repositories different names; commits are pushed separately to different remote repositories by name.
# Simplified push command
git push
2
# Viewing Remote Repositories
# View remote repositories
git remote -v
2
# Removing a Remote Repository
# Remove a remote repository
git remote rm <repo-name>
2
# Cloning a Project from a Remote Repository
# Clone a project from a remote repository
git clone <repo-url>
2
# Cloning a Specific Branch
# Clone a specific branch
git clone -b <branch-name> <repo-url>
2
# Branch Management
# Creating a Branch
# Create a branch
git checkout -b <branch-name>
2
# Viewing Branches
# View branches
git branch
2
When viewing branches, the branch marked with * indicates the current branch.
# Switching Branches
# Switch branches
git checkout <branch-name>
2
# Merging Branches
# Merge a local branch
git merge <branch-name>
# Merge a remote branch
git merge <remote-repo-name>/<branch-name>
2
3
4
5
Note: This merges the specified branch into the current branch, not the current branch into the specified branch.
Typically, you switch to the main branch first, then merge the feature branch into the main branch.
# Deleting a Branch
# Delete a branch
git branch -d <branch-name>
2
# Renaming a Branch
# Rename a branch
git branch -m <old-branch-name> <new-branch-name>
2
# Help Commands
If you're unsure about a command, use git help to display an introduction to Git commands.
# Help command
git help
2
$ git help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# References
https://www.liaoxuefeng.com/wiki/896043488029600 (opens new window)