Common Git Commands
Note
All content within square brackets ([]) in the following text should be customized by the user according to the actual situation, and should not be directly copied from the examples.
Initial Configuration
When using Git for the first time, you need to configure your username and email to associate with commit
operations:
Set username:
git config --global user.name "[Your Name]"
Set email:
git config --global user.email "[Your Email Address]"
Other common configurations:
Set default editor:
git config --global core.editor "[Default Editor]"
Enable color command line output:
git config --global color.ui auto
Change default branch name:
git config init.defaultBranch [New Name]
View all configurations:
git config --list
Configure SSH Key
SSH key is a secure and efficient authentication method, which implements password-free remote connection when using Git through encryption technology. Configuring SSH key can effectively protect your Git account and code security, the specific steps are as follows:
Generate SSH key
Press
Win+R
keys to open the “Run” window.Enter
cmd
and press Enter to open the command prompt window.Enter the command
ssh-keygen -t rsa -C '[Your Email Address]'
in the command prompt window and press Enter.Press Enter three more times to complete the generation of the SSH public and private key pair.
Find the generated SSH public key
Open the user directory:
C:\Users\[Username]\.ssh
, where the.pub
file is the SSH public key.Right-click the
.pub
file, select “Open with” and use “Notepad” or “VS Code” and other file editors to view.Alternatively, you can enter the command:
notepad %USERPROFILE%\.ssh\id_rsa.pub
in the command prompt window to view the file.Configure the public key to Github
Log in to Github, click on the avatar on the right and select
Settings
>SSH and GPG keys
>New SSH key
.Copy and paste the text content from the previous step into the
Key
text box.Set a name for this SSH key in the
Title
box.Click
Add SSH key
to complete the configuration of the SSH public key.Check if the key pairing is successful
Enter the command:
ssh -T git@github.com
in the command prompt window.If the window displays
Hi your-username! You've successfully authenticated.
, it means the SSH key configuration is successful.
Note
When generating the SSH key, make sure that the email address after
-C
is consistent with the email bound to the Github account.The security of the public and private keys is very important, ensure to properly keep your private key and do not expose or share it.
Create Repository
Before the project starts, you need to create a new repository locally and then push it to Github. Or use the clone
command to copy an existing remote repository to the local and automatically set up remote association.
Initialize new repository:
git init
Clone remote repository:
git clone [url]
Get the URL of the repository To clone a remote repository, you first need to obtain the URL of the repository. The specific steps are as follows:
Log in to Github and go to the page of the repository you want to obtain. 2. On the right side of the repository page, click the green
Code
button, a box will pop up displaying the URL of the repository. 3. Choose the URL type:HTTPS:
https://github.com/username/repository.git
SSH:
git@github.com:username/repository.git
(Use when SSH key is configured)
Click the copy button or manually copy the selected URL.
Adding Changes to the Staging Area
The add
command is used to stage modified files to Git’s staging area, marking them for inclusion in the next commit. This enables precise control and orderly management of version changes.
Add a specific file to the staging area:
git add [File]
Add all changes to the staging area:
git add .
Note
When using git add [File]
, ensure your current directory contains the target file, or provide the correct path relative to the current directory.
Committing Changes
The commit
command records staged changes into the local repository, creating a “snapshot” that documents the modifications and description.
Commit with a message:
git commit -m "[Description]"
Note
You must first stage changes using the add
command before committing with commit
.
Viewing Status
The status
command displays the current state of the working directory and staging area. It shows the current branch, modified/deleted files, staged files, and any untracked files.
View detailed status:
git status
View short status:
git status -s
Viewing Commit History
The log
command displays the Git repository’s commit history, including author, date, message, and commit hash. It is essential for tracking project changes.
Show full history:
git log
Show one-line summaries:
git log --oneline
Show graphical branch and merge relationships:
git log --graph
Show commits by a specific author:
git log --author="[Author]"
Show commits since a specific time:
git log --since="[Time]"
Show changes introduced by each commit:
git log -p
Show files changed and line statistics:
git log --stat
Show the most recent n commits:
git log -n [Number]
Show history for a specific file:
git log [File Name]
Viewing Differences
The diff
command compares changes across Git stages (working directory, staging area, commits). It does not modify content but shows detailed differences.
Compare working directory with staging area:
git diff
Compare staging area with last commit:
git diff --cached
orgit diff --staged
Compare working directory with last commit (all changes):
git diff HEAD
Compare two commits:
git diff [commit1] [commit2]
Compare two branches:
git diff [branch1] [branch2]
View diff for a file (default: working dir vs staging):
git diff [File Name]
Ignoring Files
In Git, ignored files are excluded from version control. These include temporary files, logs, build artifacts, and system caches. Create a .gitignore
file in the project root and list the paths or patterns to ignore.
Ignore all log files:
*.log
Ignore all temp files:
temp/
Ignore all
.exe
files:*.exe
Ignore VS Code workspace settings:
.vscode/
Ignore a specific file:
[File Name]
Branches
In Git, branches provide a mechanism for parallel development. A branch is an independent copy of the project history, allowing new features or fixes without affecting the main branch. The branch
command is used to manage branches: viewing, creating, and deleting.
Viewing Branches
List all local branches:
git branch
Creating/Switching Branches
Create a new branch without switching:
git branch [Branch Name]
Switch to an existing branch:
git checkout [Branch Name]
orgit switch [Branch Name]
Create and switch to a new branch:
git checkout -b [Branch Name]
orgit switch -c [Branch Name]
Deleting Branches
Delete a merged branch:
git branch -d [Branch Name]
Force delete an unmerged branch:
git branch -D [Branch Name]
Merging Branches
Merge a specified branch into the current branch:
git merge [Branch Name]
Merge and retain a merge commit:
git merge --no-ff [Branch Name]
Abort a merge:
git merge --abort
Remote Repository Collaboration
Remote repository collaboration enables distributed development by multiple developers using a shared Git repository.
View remote repositories:
git remote -v
Link local repo to remote origin:
git remote add origin [url]
Fetch and merge changes from remote:
git pull origin [Branch Name]
Fetch changes only (no merge):
git fetch origin
Push local commits to remote:
git push origin [Branch Name]