Commonly Used Git Commands.

This is an automatically translated post by LLM. The original post is in Chinese. If you find any translation errors, please leave a comment to help me improve the translation. Thanks!

Git File Management System

This blog is a reprint, the original address is https://www.cnblogs.com/chenwolong/p/GIT.html

  • Workspace: working directory
  • Index / Stage: staging area
  • Repository: repository (or local repository)
  • Remote: remote repository

Create a new code repository

1
2
3
4
5
6
7
8
# Create a Git code repository in the current directory
$ git init

# Create a directory and initialize it as a Git code repository
$ git init [project-name]

# Download a project and its entire code history
$ git clone [url]

Configuration

Git's configuration file is .gitconfig, which can be in the user's home directory (global configuration) or in the project directory (project configuration).

1
2
3
4
5
6
7
8
9
# Show the current Git configuration
$ git config --list

# Edit the Git configuration file
$ git config -e [--global]

# Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

Add/Delete files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Add specified files to the staging area
$ git add [file1] [file2] ...

# Add the specified directory to the staging area, including subdirectories
$ git add [dir]

# Add all files in the current directory to the staging area
$ git add .

# Confirm each change before adding it
# For multiple changes to the same file, multiple commits can be made
$ git add -p

# Delete a file from the working directory and add the deletion to the staging area
$ git rm [file1] [file2] ...

# Stop tracking a specified file, but keep the file in the working directory
$ git rm --cached [file]

# Rename a file and add the renaming to the staging area
$ git mv [file-original] [file-renamed]

Code submission

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Submit the staging area to the repository
$ git commit -m [message]

# Submit the specified file in the staging area to the repository
$ git commit [file1] [file2] ... -m [message]

# Submit changes in the working directory since the last commit directly to the repository
$ git commit -a

# Display all diff information when submitting
$ git commit -v

# Use a new commit to replace the previous commit
# If there are no new changes, the commit message can be modified
$ git commit --amend -m [message]

# Redo the previous commit and include the new changes to the specified file
$ git commit --amend [file1] [file2] ...

Branch

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
44
45
46
47
48
49
# List all local branches
$ git branch

# List all remote branches
$ git branch -r

# List all local and remote branches
$ git branch -a

# Create a new branch, but stay on the current branch
$ git branch [branch-name]

# Create a new branch and switch to it
$ git checkout -b [branch]

# Create a new branch pointing to the specified commit
$ git branch [branch] [commit]

# Create a new branch and establish a tracking relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]

# Switch to the specified branch and update the working directory
$ git checkout [branch-name]

# Switch to the previous branch
$ git checkout -

# Establish a tracking relationship between the existing branch and the specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

# Merge the specified branch into the current branch
$ git merge [branch]

# Select a commit and merge it into the current branch
$ git cherry-pick [commit]

# Delete a branch
$ git branch -d [branch-name]

# Delete a remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

# There is a local branch named develop_chen, but there is no corresponding remote branch. What should I do?
$ git push origin develop_chen

# This will create a branch on the remote that is the same as the local one.

$ git branch --set-upstream-to=origin/develop develop Establish a tracking relationship between the local branch and the remote branch

Tag

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
# List all tags
$ git tag

# Create a new tag at the current commit
$ git tag [tag]

# Create a new tag at the specified commit
$ git tag [tag] [commit]

# Delete a local tag
$ git tag -d [tag]

# Delete a remote tag
$ git push origin :refs/tags/[tagName]

# View tag information
$ git show [tag]

# Submit the specified tag
$ git push [remote] [tag]

# Submit all tags
$ git push [remote] --tags

# Create a new branch pointing to a specified tag
$ git checkout -b [branch] [tag]

View information

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
# Display files with changes
$ git status

# Display the version history of the current branch
$ git log

# Display the commit history and files changed for each commit
$ git log --stat

# Search the commit history based on keywords
$ git log -S [keyword]

# Display all changes after a certain commit, with each commit on one line
$ git log [tag] HEAD --pretty=format:%s

# Display all changes after a certain commit, where the "commit description" must match the search criteria
$ git log [tag] HEAD --grep feature

# Display the version history of a specified file, including file renaming
$ git log --follow [file]
$ git whatchanged [file]

# Display each diff related to a specified file
$ git log -p [file]

# Display the changes made by you today
$ git diff --shortstat "@{0 day ago}"

# Display the metadata and content changes of a specified commit
$ git show [commit]

# Display the files changed in a specified commit
$ git show --name-only [commit]

# Display the content of a specified file in a specified commit
$ git show [commit]:[filename]

# Display the most recent commits of the current branch
$ git reflog

# Pull code from the local master to update the current branch: branch is generally master
$ git rebase [branch]

Remote synchronization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ git remote update  --update remote repository
# Download all changes from the remote repository
$ git fetch [remote]

# Display all remote repositories
$ git remote -v

# Display information about a specific remote repository
$ git remote show [remote]

# Add a new remote repository and name it
$ git remote add [shortname] [url]

# Retrieve changes from the remote repository and merge them with the local branch
$ git pull [remote] [branch]

# Upload the specified branch from the local repository to the remote repository
$ git push [remote] [branch]

# Force push the current branch to the remote repository, even if there are conflicts
$ git push [remote] --force

# Push all branches to the remote repository
$ git push [remote] --all

Undo

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
# Restore the specified file in the staging area to the working directory
$ git checkout [file]

# Restore the specified file of a certain commit to the staging area and working directory
$ git checkout [commit] [file]

# Restore all files in the staging area to the working directory
$ git checkout .

# Reset the specified file in the staging area to the state of the previous commit, but keep the working directory unchanged
$ git reset [file]

# Reset the staging area and working directory to the state of the previous commit
$ git reset --hard

# Reset the pointer of the current branch to the specified commit, and reset the staging area, but keep the working directory unchanged
$ git reset [commit]

# Reset the HEAD of the current branch to the specified commit, and reset the staging area and working directory to the state of the specified commit
$ git reset --hard [commit]

# Reset the HEAD of the current branch to the specified commit, but keep the staging area and working directory unchanged
$ git reset --keep [commit]

# Create a new commit to undo the specified commit
# All changes made by the latter will be cancelled out by the former and applied to the current branch
$ git revert [commit]

# Temporarily remove uncommitted changes and move them back later
$ git stash
$ git stash pop

Others

  1. Merge the code of the development branch into master
1
2
3
4
5
$ git checkout dev           #Switch to the dev development branch
$ git pull
$ git checkout master
$ git merge dev #Merge the dev branch into master
$ git push origin master #Push the code to master
  1. Synchronize the code of master to the development branch. Merge method: ensure that the main commit line is clean (can be safely rolled back)
1
2
3
4
5
$ git checkout dev           #Switch to the dev development branch
$ git pull
$ git checkout master
$ git merge dev #Merge the dev branch into master
$ git push origin master #Push the code to master
  1. Generate a compressible package for release
1
2
# Generate a compressible package for release
$ git archive