常用Git命令集合

182 阅读2分钟

新建相关

//新建一个Git代码库 project_name为当前目录
git init [project name]  

//下载项目和他整个代码历史
git clone [url]

提交相关

//添加当前目录所有文件到暂存区
git add .

//提交暂存区文件到本地仓库
git commit -m [message]
git commit [file1] [file2] ... -m [message]

//提交时显示所有的diff信息
git commit -v

//取回远程仓库变化,更新本地仓库
git pull [remote] [branch]

//上传本地仓库代码到远程仓库
git push [remote] [branch]

//重置暂存区和工作区,与上一次commit保持一致
git reset --hard //慎用 会回退,恢复起来不好整,一般使用下面一条
git revert  //撤销某次提交,但会保存上次提交。

分支相关

//列出所有本地分支
git branch

//列出所有远程分支
git branch -r

//列出所有本地分支和远程分支
git branch -a

//新建一个本地分支并停留在当前分支
git branch [branch-name]

//新建一个本地分支并切换到新建分支
git checkout -b [branch-name]

//本地分支推送到远程仓库(远程仓库无该分支)
git push origin [branch-name]  // 强制推送
git push --set-upstream origin [branch-name]  //关联远程分支,每次就不需要强制推送

//新建一个分支指向指定commit
git branch [branch] [commit]

//合并指定分支到当前分支
git merge [branch]

//选择一个commit,合并进当前分支
git cherry-pick [commit]

//删除本地分支
git branch -d [branch-name]

//删除远程分支
git branch origin --delete [branch-name]

查看信息

//显示有变更的文件
git status

//显示当前分支的版本历史
git log

//显示暂存区和工作区的代码差异
git diff

//显示某次提交的数据和内容变化
git show [commit]

实际开发中用到的比较多的

//新建分支并将远程指定分支代码下拉
git checkou -b [branch-name] [remote-branch-name]

//将暂存区修改保存,push不会提交这些修改,可以加save来描述修改
git stash
git stash save 

//将保存的内容返回的暂存区,到需要修改的分支,不然会覆盖。
git stash pop