日常使用Git时经常记不住相关操作,故将常用的命令记录以便翻查
基本操作
git init初始化仓库git clone [url]克隆远程仓库到本地git add [file]添加指定文件到缓存区git add .添加所有文件到缓存区git status查看仓库当前状态git diff对比缓存器与工作区的差异git pull [remote] [branch]拉取远程仓库, 并与本地分支合并git push [remote] [branch]推送本地指定分支到远程仓库git push [remote] --force强制推送当前分支到远程仓库git push [remote] --all推送所有分支到远程仓库git commit -m [message]将缓存区内容提交到仓库git reset HEAD还原工作区与缓存区到上一次提交状态git reset [file]还原缓存区中指定文件, 工作区不变git rm删除仓库中指定文件git mv移动/重命名仓库中的文件
分支
git branch列出所有本地分支git branch -r列出所有远程分支git branch -a列出所有本地和远程分支git branch [branch-name]新建分支git checkout -b [branch-name]新建并切换分支git checkout [branch-name]切换分支git merge [branch-name]合并指定分支到当前分支git branch -d [branch-name]删除分支
日志
git log显示当前分支的提交历史git log --stat显示每次提交的变更git log -S [keyword]根据关键词搜索提交历史
远程仓库
git remote add [name] [url]新建远程仓库git remote -v显示所有远程仓库信息git remote show [name]显示指定远程仓库信息
还原
git checkout [file]还原缓存区指定文件到工作区git checkout [commit] [file]还原指定commit的指定文件到缓存区和工作区git checkout .还原缓存区所有文件到工作区git reset --hard还原缓存区和工作区与上一次commit一致git reset [commit]当前分支HEAD变为指定commit, 同时重置缓存区, 但工作区不变git reset --hard [commit]当前分支HEAD变为指定commit, 同时重置缓存区和工作区get reset --keep [commit]重置当前分支HEAD为指定commit, 但缓存区和工作区都保持不变
常用组合命令
新建远程仓库连接
origin,并首次推送1
2git remote add origin git@github:xxx/xxx.git
git push -u origin -all强制pull远程仓库覆盖本地文件
1
2
3git fetch --all
git reset --hard origin/master
git pull