跳转到内容

Git 常用命令

Initialize a new git repository

Terminal window
git init

Set configuration values for your username and email

Terminal window
git config --global user.name <your-name>
git config --global user.email <your-email>

Clone a repository

Terminal window
git clone <repository-url>

Add a file to the staging area

Terminal window
git add <file>

Add all files changes to the staging area

Terminal window
git add

Check the unstaged changes

Terminal window
git diff

Commit the staged changes

Terminal window
git commit -m "message"

Reset staging area to the last commit

Terminal window
git reset

Check the state of the working directory and the staging area

Terminal window
git status

Remove a file from the index and working directory

Terminal window
git rm ‹file>

List the commit history

Terminal window
git log

Check the metadata and content changes of the commit

Terminal window
git show <commit-hash>

Lists all local branches

Terminal window
git branch

Create a new branch

Terminal window
git branch <branch-name>

Rename the current branch

Terminal window
git branch -m <new-branch-name>

Delete a branch

Terminal window
git branch -d <branch-name>

Switch to another branch

Terminal window
git checkout <branch-name>

Merge specified branch into the current branch

Terminal window
git merge <branch-name>

Create a new connection to a remote repository

Terminal window
git remote add <name> <repository-url>

Remove connection to a remote repository

Terminal window
git remote remove <name>

Update connection to a remote repository

Terminal window
git remote set-url <name> <repository-url>

View connection to a remote repository

Terminal window
git remote get-url <name>

Push the committed changes to a remote repository

Terminal window
git push <remote> <branch>

Download the content from a remote repository

Terminal window
git pull <remote>

Cleanup unnecessary files and optimize the local repository

Terminal window
git gc

Temporarily remove uncommitted changes and save them for later use

Terminal window
git stash

Reapply previously stashed changes

Terminal window
git stash apply