Git common commands

LHT

Common Git Commands

Untitled

  • Workspace: Working directory
  • Index / Stage: Staging area
  • Repository: Local repository
  • Remote: Remote repository

Git Configuration

1
2
3
4
5
6
# Check Git version information
git -v
# View Git-related commands
git -h
# View current Git configuration
git config -l

Set User Signature

πŸ’‘ The purpose of the signature is to distinguish the identity of different operators. The user signature information can be seen in the commit information of each version, confirming who made the commit. You must set up the user signature when installing Git for the first time, otherwise, you cannot commit code.

πŸ“Œ The user signature set here has no relation to the account you will use to log in to GitHub (or other code hosting platforms).

1
2
3
4
5
6
7
8
9
10
11
12
# Query the username used by Git. --global means global, without --global means querying only in the current project.
git config --global user.name
# Query the email used by Git
git config --global user.email

# Globally configure username. --global means global, without --global means configuring only in the current project.
git config --global user.name username
# Globally configure email
git config --global user.email email

# Check if the configured user signature is effective
cat ~/.gitconfig

Basic Operations

Create a New Repository

1
2
3
4
5
# Create a new Git repository in the current directory, generating a .git folder
$ git init

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

Add, Modify, and Delete Files

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
# View the status of the local repository
$ git status

# Add the 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 changes in the working directory to the staging area, including modifications, new files, and deletions
$ git add .
$ git add *
$ git add -A
$ git add --all

# Reset the specified file in the staging area to match the last commit, but the working directory remains unchanged
$ git reset [file]

# Reset the staging area and working directory to match the last commit
$ git reset --hard

# Remove the specified file from tracking, but keep it in the local repository folder
$ git rm --cached [file]

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

# Undo changes to a file in the working directory (use with caution)
$ git checkout -- [file]

Code Commit

1
2
3
4
5
6
7
8
9
10
11
12
# Commit files from the staging area to the local repository and add a description
$ git commit -m "<commit message>"

# Commit specific files from the staging area to the repository
$ git commit [file1] [file2] ... -m [message]

# Replace the previous commit with a new one
# If there are no new changes, use this to rewrite the commit message of the last commit
$ git commit --amend -m [message]

# Redo the last commit and include changes to specified files
$ git commit --amend [file1] [file2] ..

Version Rollback

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
# Print all commit records; use the --oneline option for simplified output
$ git log

# Use the --author option to print commit records by a specific user
$ git log --author=author-name

# Display in a simplified one-line format with concise hash values
$ git log --oneline

# View logs and show versions
$ git log --pretty=oneline

# View all branch operations (including deleted commits and reset actions)
$ git reflog

# Change HEAD to point to a previous commit, but keep files unchanged
$ git reset --soft <commit ID>

# Change HEAD to point to a previous commit, and reset files to the state at that version
$ git reset --hard <commit ID>

# Roll back to the previous commit
$ git reset --hard HEAD^

# Roll back to two commits ago, or further back
$ git reset --hard HEAD~3

# Reset by commit ID, can also move forward
$ git reset --hard <commit ID>

# Create a new commit to undo a previous commit
$ git revert <commit ID>

Branch Operations

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
# List all local branches; the current branch is marked with "*"
$ git branch

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

# List all local branches and show the last commit; the current branch is marked with "*"
$ git branch -v

# Create a new branch based on the latest commit
$ git branch <branch-name>

# Rename a branch; if the original branch name is not specified, the current branch is renamed
$ git branch -m [<old-branch-name>] <new-branch-name>

# Force rename a branch
$ git branch -M [<old-branch-name>] <new-branch-name>

# Delete a specified local branch
$ git branch -d <branch-name>

# Force delete a specified local branch
$ git branch -D <branch-name>

# Delete a remote branch
$ git push origin --delete <branch-name>

# Switch to an existing branch, e.g., `git checkout main`
$ git checkout <branch-name>

# Merge a specified branch into the current branch (e.g., merge `dev` into `main`)
# Before merging, it’s recommended to pull the latest changes with `git pull origin main`
$ git merge dev

# Merge a specific commit into the current branch
$ git cherry-pick <commit ID>

# Create and switch to a specified branch, retaining all commit history
# Equivalent to running `git branch` and `git checkout` consecutively
$ git checkout -b <branch-name>

# Create and switch to a specified branch, but discard all commit history
$ git checkout --orphan <branch-name>

When working with branches, it’s important to follow naming conventions. Common branch names include:

  • main: Main branch
  • dev: Development branch
  • hotfix: For urgent bug fixes to the main branch, typically named by issue number (e.g., hotfix/#1), making it easier to merge with the main branch
  • feature: Named by feature, e.g., feature/add_search
  • release: Used for releases

Untitled

Merge Conflicts

Cause

When merging branches, if there are two completely different changes in the same location of the same file, Git cannot decide which version to keep and requires a manual resolution of the conflict.

Solution

  1. Edit the file with the conflict and remove the special symbols to decide which content to keep.

Special symbols:

1
2
3
4
<<<<<<< HEAD - Code from the current branch
=======
- Code from the merged branch
>>>>>>> - Name of the merged branch
  1. Stage the resolved file.
  2. Commit the changes (Note: Do not include the filename when running the git commit command in this case).

Remote Operations

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
# List all existing remote repositories
$ git remote

# List remote repositories in detail, showing URL after the alias
$ git remote -v

# Add a remote repository, usually aliased as origin
$ git remote add <remote-alias> <remote-URL>

# Rename a remote repository alias
$ git remote rename <old-alias> <new-alias>

# Remove a remote repository alias
$ git remote remove <remote-alias>

# Modify the URL of a remote repository
$ git remote set-url <remote-alias> <new-URL>

# Fetch all branches from the remote repository to the local machine
$ git fetch <remote-alias>

# Fetch a specific branch from the remote repository
$ git fetch <remote-alias> <branch-name>

# Merge the fetched branch into the current branch
$ git merge <branch-name>

# Fetch and merge the latest changes from the remote repository (equal to fetch + merge)
$ git pull <remote-alias> <remote-branch-name>

# Push a local branch to the specified branch on the remote repository
$ git push <remote-alias> <local-branch-name>:<remote-branch-name>

# After using the -u option once, future `git push` commands can be used without specifying the remote alias and local branch.
$ git push -u origin main

# Delete a specific branch from the remote repository
$ git push <remote-alias> :<remote-branch-name>
$ git push <remote-alias> --delete <remote-branch-name>

# Clone a remote repository to the current local directory
$ git clone <remote-URL>

# Clone to a specified local directory
$ git clone <remote-URL> <local-directory>

# Clone a specific branch; the default is the main branch
$ git clone <remote-URL> -b <branch-name> <local-directory>

Tag Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# List all tags
$ git tag

# Add a lightweight tag that references a commit; can specify a previous commit
$ git tag <tag-name> [<commit-ID>]

# Add an annotated tag with a description; can specify a previous commit
$ git tag -a <tag-name> -m <description> [<commit-ID>]

# Switch to a specified tag
$ git checkout <tag-name>

# View information about a tag
$ git show <tag-name>

# Delete a specified tag
$ git tag -d <tag-name>

# Push a tag to the remote repository
$ git push <remote-alias> <tag-name>

# Push all local tags to the remote repository
$ git push <remote-alias> --tags

Using SSH with GitHub

  • Run: ssh-keygen -t rsa -C <GitHub email address>
  • Navigate to the .ssh directory and copy the contents of id_rsa.pub
  • Log in to GitHub: Settings –> SSH and GPG keys –> New SSH Key
  • Add the SSH address as a remote repository in Git: git remote add <alias> <SSH address>

Configuring Git Ignore Rules

Create an ignore rules file (e.g., git.ignore). It is recommended to place this file in your home directory.

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
# Compiled class files
*.class

# Log files
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Virtual machine crash logs
hs_err_pid*

# Eclipse/IDEA project files
.classpath
.project
.settings
target
.idea
*.iml

Reference the ignore configuration file in .gitconfig (located in the home directory on Windows).

1
2
3
4
5
6
[user]
name = Layne
email = [email protected]
[core]
excludesfile = C:/Users/asus/git.ignore
# Note: Use forward slashes ("/") instead of backslashes ("\\")

Common Git Usage Scenarios

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
# First-time Git installation on the server and connecting to GitHub via SSH
$ sudo apt-get install git # For Ubuntu systems
$ git config --global user.name "Your name here"
$ git config --global user.email "[email protected]"
$ ssh-keygen -t rsa -C "[email protected]"
# Open the hidden .ssh/id_rsa.pub file, copy the SSH key
# Add the SSH key to GitHub in Account Settings > Add SSH Key
$ ssh -T [email protected]
# On Aliyun servers, GitHub connections may be slow. Uncomment GSSAPIAuthentication no
vim /etc/ssh/ssh_config

# When the code in the development branch (dev) is ready for production, merge it into the main branch
$ git checkout dev
$ git pull
$ git checkout main
$ git merge dev
$ git push -u origin main

# When the master branch is updated, update the development branch (dev)
$ git checkout main
$ git pull
$ git checkout dev
$ git merge main
$ git push -u origin dev

# Discard local changes and forcefully replace them with remote repository code
$ git fetch --all
$ git reset --hard origin/main
$ git pull

# Implement small feature changes across multiple branches
$ git checkout -b feature/add_search dev # Create two branches: feature and dev
$ git add somefile # Make small changes
$ git commit -m 'msg' # Commit the changes
$ git checkout dev # Switch to the development branch
$ git pull # Fetch remote changes and merge with local dev branch
$ git merge feature/add_search # Merge feature/add_search into the dev branch
$ git push # Push to the remote dev branch
$ git checkout main # Switch to the main branch
$ git merge dev # Merge dev into the main branch
$ git push # Push to the remote main branch
  • Title: Git common commands
  • Author: LHT
  • Created at : 2022-06-01 06:10:00
  • Link: https://blog.327774.xyz/2022/05/31/git/Git common commands/
  • License: This work is licensed under CC BY-NC-SA 4.0.