ESJIAN 以下是常用 Git 命令清单,根据功能类别整理,便于理解和记忆: 一、新建代码库 git init:在当前目录新建一个 Git 代码库。 git init [project-name]:新建一个目录,并将其初始化为 Git 代码库。 git clone [url]:下载一个项目及其整个代码历史。 git clone [url] [path]:克隆项目内容到本地指定目录(不会克隆出一个名为项目名的文件夹) 二、配置 git config --list:显示当前的 Git 配置。 git config -e --global:编辑 Git 配置文件。 git config --global user.name "name":设置提交代码时的用户名。 git config --global user.email "email address":设置提交代码时的用户邮箱。 三、增加/删除文件 git add [file1] [file2] ...:添加指定文件到暂存区。 git add [dir]:添加指定目录到暂存区,包括子目录。 git add .:添加当前目录的所有文件到暂存区。 git add -p:添加每个变化前都要求确认。 git rm [file1] [file2] ...:删除工作区文件,并将删除放入暂存区。 git rm --cached [file]:停止追踪指定文件,但文件保留在工作区。 git mv [file-original] [file-renamed]:改名文件,并将改名放入暂存区。 四、代码提交 git commit -m "message":提交暂存区到仓库区,并且附上提交消息,若未附上,会自动进入 GUN 编辑器进行消息编辑。 git commit --allow-empty -m "message":允许不记录文件提交信息。 git commit [file1] [file2] ... -m "message":提交暂存区的指定文件到仓库区。 git commit -a:提交工作区自上次 commit 之后的变化,直接到仓库区。 git commit -v:提交时显示所有 diff 信息。 git commit --amend -m "message":使用一次新的 commit,替代上一次提交。 git commit -m [message]:提交暂存区到仓库区,并且附上提交消息,若未附上,会自动进入 GUN 编辑器进行消息编辑。 git commit [file1] [file2] ... -m [message]:提交暂存区的指定文件到仓库区。 git commit -a:提交工作区自上次 commit 之后的变化,直接到仓库区。 git commit -v:提交时显示所有 diff 信息。 git commit --amend -m [message]:使用一次新的 commit,替代上一次提交。 git commit --amend [file1] [file2] ...:重做上一次 commit,并包括指定文件的新变化。 五、分支 git branch:列出所有本地分支。 git branch -r:列出所有远程分支。 git branch -a:列出所有本地分支和远程分支。 git branch -v:查看本地分支的关联关系 git branch [branch-name]:新建一个分支,但依然停留在当前分支。 git checkout -b [branch]:新建一个分支,并切换到该分支。 git checkout [branch-name]:切换到指定分支,并更新工作区。 git branch -d [branch-name]:删除分支。 git push origin --delete [branch-name]:删除远程分支。 git merge [branch]:合并指定分支到当前分支。 git cherry-pick [commit]:选择一个 commit,合并进当前分支。 git branch -m [name]:给本地当前所在分支重命名 git branch --set-upstream-to=<远程仓库>/<远程分支> <本地分支>:给本地分支重新关联远程分支 六、标签 git tag:列出所有 tag。 git tag [tag]:新建一个 tag 在当前 commit。 git tag [tag] [commit]:新建一个 tag 在指定 commit。 git tag -d [tag]:删除本地 tag。 git push origin :refs/tags/[tagName]:删除远程 tag。 git show [tag]:查看 tag 信息。 git push [remote] [tag]:提交指定 tag。 git push [remote] --tags:提交所有 tag。 七、查看信息 git status:显示有变更的文件。 git log:显示当前分支的版本历史。 git log --stat:显示 commit 历史及每次 commit 发生变更的文件。 git log -S [keyword]:搜索提交历史,根据关键词。 git diff:显示暂存区和工作区的差异。 git diff --cached [file]:显示暂存区和上一个 commit 的差异。 八、远程同步 git remote:查看本地分支关联了几个远程仓库。 git remote add [name] [https地址]:将当前分支关联到相应的远程分支,并为其在本地命名为 name。 git remote remove [name]:取消当前本地分支关联的远程仓库 git fetch [remote]:下载远程仓库的所有变动。 git pull [remote] [branch]:取回远程仓库的变化,并与本地分支合并。 git push [remote] [branch]:上传本地指定分支到远程仓库。 git push [仓库地址]:不关联仓库直接推送到地址所指仓库 git push [remote] --force:强行推送当前分支到远程仓库,即使有冲突。 git push [remote] --all:推送所有分支到远程仓库。 git push --set-upstream [remote] [master]:关联本地与远程分支。 九、撤销 git checkout [file]:恢复暂存区的指定文件到工作区。 git checkout [commit] [file]:恢复某个 commit 的指定文件到暂存区和工作区。 git reset [file]:重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变。 git reset --hard [commit]:重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致。 十、其他 git archive:生成一个可供发布的压缩包。
Lyneon 关于《二、配置》中的命令,其中的--global选项表示修改的是全局的默认配置 假如你有这样的需求:加入了一个团队项目,需要更改提交名并换用团队分配邮箱,但又不想影响到其他的个人项目。你可以先切换到目标仓库,然后执行命令时将--global替换为--local,这样就可以设定此仓库的独立配置