VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 技术文档

    • Git User Manual
      • Installation
      • Creating a Repository
      • Adding Files to a Repository
        • Commit Message Conventions
      • Version Management
        • Committing Changes
        • Viewing Version History
        • Reverting to a Previous Version
        • Undoing Changes
        • Deleting Files
      • Remote Repositories
        • SSH Authentication
        • Linking a Remote Repository
        • Viewing Remote Repositories
        • Removing a Remote Repository
        • Cloning a Project from a Remote Repository
        • Cloning a Specific Branch
      • Branch Management
        • Creating a Branch
        • Viewing Branches
        • Switching Branches
        • Merging Branches
        • Deleting a Branch
        • Renaming a Branch
      • Help Commands
      • References
    • Markdown Tutorial
    • Common npm Commands
    • npm package.json Properties Explained
    • YAML Language Tutorial
    • Renaming a Git Branch
  • GitHub技巧

  • Nodejs

  • 博客搭建

  • 技术
  • 技术文档
xugaoyi
2019-12-25
Contents

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"
1
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
1
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 init in 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"
1
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
...
1
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
1
2

# Viewing Version History

# View version history
git log   # Shows version numbers, commit times, and other information
1
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^
1
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>
1
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
1

# 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 contains id_rsa and id_rsa.pub files. If they already exist, skip to the next step. If not, open Shell (on Windows, open Git Bash) and create an SSH Key:
# Create SSH Key
ssh-keygen -t rsa -C "your-email-address"
1
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.pub file 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
1
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
1
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, and git status should show no changes. If there are changes, first use git add . to add them to the staging area, then git commit -m 'commit description' to commit to the repository, and then execute the above commands.

If you checked Initialize this repository with a README when creating the repository, you need to pull the README.md file to your local repository first with git 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
1
2

# Viewing Remote Repositories

# View remote repositories
git remote -v
1
2

# Removing a Remote Repository

# Remove a remote repository
git remote rm <repo-name>
1
2

# Cloning a Project from a Remote Repository

# Clone a project from a remote repository
git clone <repo-url>
1
2
# Cloning a Specific Branch
# Clone a specific branch
git clone -b <branch-name> <repo-url>
1
2

# Branch Management

# Creating a Branch

# Create a branch
git checkout -b <branch-name>
1
2

# Viewing Branches

# View branches
git branch
1
2

When viewing branches, the branch marked with * indicates the current branch.

# Switching Branches

# Switch branches
git checkout <branch-name>
1
2

# Merging Branches

# Merge a local branch
git merge <branch-name>

# Merge a remote branch
git merge <remote-repo-name>/<branch-name>
1
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>
1
2

# Renaming a Branch

# Rename a branch
git branch -m <old-branch-name> <new-branch-name>
1
2

# Help Commands

If you're unsure about a command, use git help to display an introduction to Git commands.

# Help command
git help
1
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.
1
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)

Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36
Markdown Tutorial

Markdown Tutorial→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode