初始化
git init # 初始化仓库
配置
git config --global user.name "Your Name" # 设置用户名 git config --global user.email "Your Email" # 设置邮箱 git config --global core.editor "Your Editor" # 设置编辑器 git config --list # 查看当前配置
添加
git add file # 添加文件到暂存区 git add . # 添加当前目录下所有文件到暂存区 git add -u # 添加已跟踪文件到暂存区 git add -A # 添加所有文件到暂存区
提交
git commit -m "message" # 提交暂存区文件到仓库,并添加注释 git commit -am "message" # 添加文件到暂存区并提交到仓库
查看
git status # 查看仓库状态 git log # 查看仓库提交日志 git show commit_id # 查看某次提交的信息 git diff # 查看暂存区和工作区的差异 git diff --cached # 查看暂存区和仓库的差异 git diff commit_id1 commit_id2 # 查看两次提交的差异
分支
git branch # 查看分支 git branch branch_name # 创建分支 git checkout branch_name # 切换分支 git merge branch_name # 合并分支 git branch -d branch_name # 删除分支
标签
git tag # 查看标签 git tag tag_name commit_id # 添加标签 git tag -d tag_name # 删除标签
远程仓库
git remote # 查看远程仓库 git remote add remote_name remote_url # 添加远程仓库 git push remote_name branch_name # 推送到远程仓库 git pull remote_name branch_name # 从远程仓库拉取 git clone remote_url # 从远程仓库克隆
撤销
git reset HEAD file # 撤销工作区文件 git checkout -- file # 撤销暂存区文件 git reset commit_id # 撤销某次提交 git revert commit_id # 撤销某次提交(保留提交历史)
其他
git stash # 储藏 git stash pop # 恢复 git clean -df # 清理 git reflog # 查看操作历史 git gc --prune=now # 清理垃圾