全局配置
git config --global user.name <name>设置用户名git config --global user.email <email>设置邮箱git config --global core.autocrlf <option>input提交代码时自动将行尾转换为LF,检出代码时不转换true在检出代码时自动将行尾转换为CRLF,在提交代码时自动将行尾转换为LFfalse在检出和提交代码时都不自动转换行尾
git config --global core.eol <option>lfUNIX 行结束符,即\ncrlfWindows 行结束符,即\r\n
创建仓库
git init初始化仓库git clone克隆仓库git clone -b <branch_name> --single-branch <url>克隆指定的分支
提交和修改
git addgit add <file>添加一个或多个文件到暂存区git add <dir>添加指定目录到暂存区,包括子目录git add .添加当前目录下的所有文件到暂存区
git commitgit commit -m <message>提交暂存区到本地仓库中,<message>可以是一些备注信息git commit <file> -m <message>提交暂存区的指定文件到仓库区git commit -a直接提交,不需要执行git add命令
git status查看仓库的状态,显示有变更的文件git diffgit diff <file>查看指定文件暂存区和工作区的差异
git reset回退版本git rmgit rm <file>将文件从暂存区和工作区中删除git rm -rf <dir>递归删除指定目录下的所有内容
git mvgit mv <file> <new_file>移动或重命名工作区文件
git checkoutgit checkout -- <file>将指定文件恢复到最新的提交状态,丢弃所有未提交的更改,常用于撤销文件的修改
git restoregit restore <file>撤销指定文件的修改git restore .撤销所有文件的修改
日志
git log查看历史提交记录git log --oneline精简提交历史
git blame <file>以列表形式查看指定文件的历史修改记录
远程操作
git remote列出当前仓库中已配置的远程仓库git remote -v列出当前仓库中已配置的远程仓库,并显示它们的 URLgit remote add <remote_name> <remote_url>添加一个新的远程仓库git remote rename <old_name> <new_name>将已配置的远程仓库重命名git remote remove <remote_name>从当前仓库中删除指定的远程仓库git remote set-url <remote_name> <new_url>修改指定远程仓库的 URLgit remote show <remote_name>显示指定远程仓库的详细信息git remote prune origin清理无效的远程分支信息
get fetch用于从远程仓库拉取代码git merge <branch_name>将指定分支合并到当前分支
git pull从远程仓库拉取代码并合并本地的版本git pushgit push <remote_name> <branch_name>推送本地分支到远程仓库git push <remote_name> -d <branch_name>删除远程仓库的指定分支
分支管理
git branch列出所有本地分支git branch -r查看远程分支git branch -a查看所有分支git branch <branch_name>创建本地分支git branch -d <branch_name>删除本地分支git branch -D <branch_name>强制删除本地分支git branch -m <old_branch_name> <new_branch_name>重命名本地分支
git checkoutgit checkout -切换到前一个分支git checkout <branch_name>切换到指定分支git checkout --orphan <branch_name>创建一个空的分支git checkout -b <branch_name>基于当前分支创建并切换到指定分支git checkout -b <branch_name> <local_branch_name>基于本地分支创建并切换到指定分支git checkout -b <branch_name> <remote_name>/<branch_name>基于远程分支创建并切换到指定分支git checkout tags/<tag_name>切换到标签git checkout <commit_hash>切换到指定的提交状态
git stash暂存当前修改git stash save "message"暂存当前修改,并添加备注git stash list列出所有暂存git stash pop恢复最近一次暂存git stash apply恢复最近一次暂存git stash drop删除最近一次暂存git stash clear删除所有暂存
git taggit tag <tag_name>基于当前分支创建一个标签git tag <tag_name> <commit_id>基于指定提交创建一个标签git tag -a <tag_name> -m <message>基于当前分支创建一个带注释的标签git tag -a <tag_name> <commit_id> -m <message>基于指定提交创建一个带注释的标签git tag -d <tag-name>删除指定标签git push <remote_name> <tag_name>推送一个本地标签git push <remote_name> :refs/tags/<tag_name>删除一个远程标签git push <remote_name> --tags推送所有本地标签
git rebase变基git rebase <base-branch>将当前分支变基到指定分支git rebase -i <base-branch>将当前分支变基到指定分支,并进入交互模式git rebase --abort终止变基git rebase --continue继续变基
本地拉取PR进行预览
格式为git fetch origin pull/PRId/head:LocalBranchName
其中,PRId为该 Pull Request 的序号,LocalBranchName为拉取到本地后的分支名称
shell
git fetch origin pull/123/head:pr123