Git is an indispensable version control tool in modern software development. It helps developers keep track of all changes to a project and collaborate efficiently with team members. In this article, we will introduce some common Git commands in the project practice to help you better manage the code.
1. Initialization and configuration
Initialize Warehouse
Initialize the Git repository in a new project directory:
git init
Configuring user information
Before committing your code, you need to configure user information that will be recorded in each commit:
git config --global "Your Name" git config --global "@"
2. Basic operations
Cloning Remote Warehouse
Clone a project locally from a remote repository:
git clone <repository_url>
View Warehouse Status
Checks the status of the current working directory, including modified, untracked files:
git status
Adding files to the staging area
Add the file to the staging area in preparation for the next commit:
git add <file_name> # add a single file gitadd . # Add all changed files
Submit changes
Submit the file in the staging area and add the commit message:
git commit -m "Submit information"
View Submission History
View a project's commit history:
git log
3. Branch management
Creating and switching branches
Create a new branch and switch to it:
git checkout -b <new_branch>
Switch to an existing branch:
git checkout <branch_name>
View Branches
Lists all branches and marks the branch you are currently on:
git branch
merge branch
Merges changes from the specified branch into the current branch:
git merge <branch_name>
Deleting Branches
Delete branches that are no longer needed:
git branch -d <branch_name>
4. Remote operation
View Remote Warehouse
View the currently configured remote repository:
git remote -v
Adding a Remote Repository
Adds a remote repository to the local repository:
git remote add origin <repository_url>
Push to Remote Warehouse
Push a local branch to a remote repository:
git push origin <branch_name>
Pulling Remote Updates
Pull updates from remote repositories and merge them with local branches:
git pull origin <branch_name>
Cloning Remote Warehouse
If you don't already have a local repository, you can clone the remote repository locally with the following command:
git clone <repository_url>
5. Advanced operations
Undo changes
Restore the working directory to the state of the last commit (use with caution):
git checkout -- <file_name>
Cancel Suspension
Removes the file from the staging area, but keeps the changes to the working directory:
git reset <file_name>
Modify the last commit
This can be used if you need to modify the commit message of the last commit or include new changes:
git commit --amend
Succinct format for viewing submission history
Use one line to display each commit record:
git log --oneline
View Changes
View the differences with the staging area in the working catalog:
git diff
View the differences between the staging area and the last commit:
git diff --cached
6. Practical tips
Interactive staging
Use the interactive mode to select the part of the file to be staged:
git add -p
label management
Create a label for the project:
git tag <tag_name>
Push tags to remote repositories:
git push origin <tag_name>
Cleaning up untracked files
Clean up untracked files and directories in the working directory (use with caution):
git clean -f -d