git 常用指令

264 阅读3分钟

git 是我们经常会用到的版本工具,为我们的生产提供了良好的助力!
日常工作场景中,很多人会用到git UI工具来工作。 作为一个程序员我个人更喜欢使用指令来进行操作。自己在噼里啪啦敲指令时,总是会莫名的感到些许的成就感。


下面我将我日常使用的 git 指令写总结出来,方便我查阅的同时,希望能够到帮助别的同学!

git clone

指令功能
git clone url拷贝一个Git仓库到本地

git pull

指令功能
git pull拉取远程分支到本地
git pull origin branchName:branchName拉取远程branchName分支到本地branchName;
名称一致可写成 git pull origin branchName

git push

指令功能
git push origin <本地分支名>:<远程分支名>将本地分支推动到远程,没有则会被创建
git push <分支名>将本地推送到远程,前提是本地分支名和远程分支名相同
git push --delete origin <远程分支名>删除远程分支,同时本地分支也会被删除

git checkout

指令功能
git checkout “feat”切换到分支 feat
git checkout -b "feat"创建并切换到分支 feat
git checkout -b feat-xx origin/feat-xx本地创建分支,并与远端分支拉齐

git branch

指令功能
git branch查看本地分支
git branch -a查看本地分支和远程分支
git branch “feat”创建分支 feat
git branch -m new-name重命名分支(本分支)
git branch -m old-name new-name重命名分支(在别的分支上)

git config

指令功能
git config --listgit的config配置项列表
git config --global user.name “zhb”全局添加用户名
git config --global user.email “19584434@qq.com全局添加邮箱
git config credential.helper store保存账号和密码,不需每次输入,输入指令后会需要再一次输入指令和密码
git config --global --unset credential.helper删除某一配置项(credential.helper就是其中一配置项)

git reset

指令功能
git reset --mixed [commitId]重置暂存区的文件与上一次提交保持一致,工作区文件内容保持不变 (--mixed 为默认可省略) 。简写为:git reset [commitId]
git reset --hard [commitId]回退到 commitId 版本,且该版本之后的提交均舍弃 (慎用)

git log

指令功能
git log展示提交日志
git log --author="authorname"按照作者名字提交 (git log --author="xiaoming",名字没有空格可不用使用)
git log --oneline简略展示日志信息

git show

指令功能
git show commitId展示 commitId 修改内容

git merge

指令功能
git merge branchName合并 分支 branchName 到本分支

git stash

指令功能
git stash存储
git stash list存储的内容
git stash drop [number]按照序号删除stash
git stash clear删除全部stash
git stash pop应用stash 0 ,并从stash中删除

其它

指令功能
git remote set-url origin <url>修改远程仓库地址为 url
git remote rm origin; git remote add origin [url]修改远程仓库地址为 url; 先删除后添加
git remote -v查看远程仓库地址
git remote prune origin同步远程的分支到本地,远程已经被删除的分支,本地不会展示
git branch --set-upstream-to=origin/remote_branch  your_branch其中,origin/remote_branch是你本地分支对应的远程分支;your_branch是你当前的本地分支

文章持续更新中...