跳转到内容

Git 常用命令

Terminal window
git init

Set configuration values for your username and email

Section titled “Set configuration values for your username and email”
Terminal window
git config --global user.name <your-name>
git config --global user.email <your-email>
Terminal window
git clone <repository-url>
Terminal window
git add <file>
Terminal window
git add
Terminal window
git diff
Terminal window
git commit -m "message"
Terminal window
git reset

Check the state of the working directory and the staging area

Section titled “Check the state of the working directory and the staging area”
Terminal window
git status

Remove a file from the index and working directory

Section titled “Remove a file from the index and working directory”
Terminal window
git rm ‹file>
Terminal window
git log

Check the metadata and content changes of the commit

Section titled “Check the metadata and content changes of the commit”
Terminal window
git show <commit-hash>
Terminal window
git branch
Terminal window
git branch <branch-name>
Terminal window
git branch -m <new-branch-name>
Terminal window
git branch -d <branch-name>
Terminal window
git checkout <branch-name>

Merge specified branch into the current branch

Section titled “Merge specified branch into the current branch”
Terminal window
git merge <branch-name>

Create a new connection to a remote repository

Section titled “Create a new connection to a remote repository”
Terminal window
git remote add <name> <repository-url>
Terminal window
git remote remove <name>
Terminal window
git remote set-url <name> <repository-url>
Terminal window
git remote get-url <name>

Push the committed changes to a remote repository

Section titled “Push the committed changes to a remote repository”
Terminal window
git push <remote> <branch>

Download the content from a remote repository

Section titled “Download the content from a remote repository”
Terminal window
git pull <remote>

Cleanup unnecessary files and optimize the local repository

Section titled “Cleanup unnecessary files and optimize the local repository”
Terminal window
git gc

Temporarily remove uncommitted changes and save them for later use

Section titled “Temporarily remove uncommitted changes and save them for later use”
Terminal window
git stash
Terminal window
git stash apply