git 常用命令

Git 常用命令速查

从服务器上将代码给拉下来

git clone https://github.com/zhdxmw/wy-music

Git 几个专用名词的译名如下。

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库

远程仓库相关命令

检出仓库:$ git clone git://github.com/jquery/jquery.git
查看远程仓库:$ git remote -v
添加远程仓库:$ git remote add [name] [url]
删除远程仓库:$ git remote rm [name]
修改远程仓库:$ git remote set-url --push [name] [newUrl]
拉取远程仓库:$ git pull [remoteName] [localBranchName]
推送远程仓库:$ git push [remoteName] [localBranchName]

注: git commit -m "用户权限管理" --no-verify

如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,如下:

git push origin test:master // 提交 本地test 分支作为远程的master分支
git push origin test:test // 提交 本地test 分支作为远程的test分支

分支(branch)操作相关命令

git branch --set-upstream dev origin/next
// 手动建立追踪关系。dev分支追踪origin/next分支
git branch 列出所有本地分支
git branch -r 列出所有远程分支
git branch -a 列出所有本地分支和远程分支
git branch [branch-name] 新建一个分支,但依然停留在当前分支
git checkout -b [branch-name] 新建一个分支,并切换到该分支
git branch --track [branch][remote-branch]

新建一个分支,与指定的远程分支建立追踪关系

创建版本:$ git tag [name]
删除版本:$ git tag -d [name]
查看远程版本:$ git tag -r
创建远程版本(本地版本push到远程):$ git push origin [name]
删除远程版本:$ git push origin :refs/tags/[name]
合并远程仓库的tag到本地:$ git pull origin --tags
上传本地tag到远程仓库:$ git push origin --tags
创建带注释的tag:$ git tag -a [name] -m 'yourMessage'

合并分支
比如,如果要将开发中的分支(develop),合并到稳定分支(master)

首先切换的master分支:git checkout master。
然后执行合并操作:git merge develop。
如果有冲突,会提示你,调用git status查看冲突文件。
解决冲突,然后调用git add .
所有冲突解决后,git commit 提交更改。

拉取分支
获取最新版本有两种 拉取和 获取 pull 和 fetch

git pull 相当于git fetch 和 git merge
git pull 从远程拉取最新版本到本地自动合并 merge
git pull origin master
git fetch 从远程获取最新版本到本地不会自动合并 merge
git fetch origin master
git log -p master..origin/master
git merge origin/master

以上命令的含义:
首先从远程的origin的master主分支下载最新的版本到origin/master分支上
然后比较本地的master分支和origin/master分支的差别
最后进行合并
实际使用中使用git fetch 更安全在merge之前可以看清楚 更新情况 再决定是否合并

当前的分支20171208,获取20171214分支最新代码,然后再推送到远程远程20171214

git fetch
git checkout 20171214
git merge 20171208
git push

推送分支

  • 本地分支与远程分支可以不是追踪关系
    如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的test分支,或者作为另外一个名叫test的分支,如下:
    本地先开好分支然后推送到远程
git checkout -b test //创建并切换到分支test
git push origin test:test // 推送本地的test(冒号前面的)分支到

远程origin的test (冒号后面的)分支(没有会自动创建)

  • 本地分支与远程分支是追踪关系
    如果想把本地的某个分支dev提交到远程仓库,并作为远程仓库的dev分支,或者作为另外一个名叫dev的分支,如下:
本地先开好分支然后推送到远程
git checkout -b dev //创建并切换到分支dev
或者当前的分支就是dev
git push origin dev
git push origin payght // 将本地dev 分支推送到远程 payght 分支

分支操作

列出所有本地分支
git branch
列出所有远程分支
git branch -r
列出所有本地分支和远程分支
git branch -a
新建一个分支,但依然停留在当前分支
git branch [branch-name]
新建一个分支,并切换到该分支
git checkout -b [branch]
新建一个分支,指向指定commit
git branch [branch] [commit]
新建一个分支,与指定的远程分支建立追踪关系
git branch --track [branch] [remote-branch]
切换到指定分支,并更新工作区
git checkout [branch-name]
切换到上一个分支
git checkout -
建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]
合并指定分支到当前分支
git merge [branch]
选择一个commit,合并进当前分支
git cherry-pick [commit]
删除分支
git branch -d [branch-name]
删除远程分支
git push origin --delete [branch-name]
git branch -dr [remote/branch]

查看信息

显示有变更的文件
git status
显示当前分支的版本历史
git log
显示commit历史,以及每次commit发生变更的文件
git log --stat
搜索提交历史,根据关键词
git log -S [keyword]
显示某个commit之后的所有变动,每个commit占据一行
git log [tag] HEAD --pretty=format:%s
显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
git log [tag] HEAD --grep feature
显示某个文件的版本历史,包括文件改名
git log --follow [file]
git whatchanged [file]
显示指定文件相关的每一次diff
git log -p [file]
显示过去5次提交
git log -5 --pretty --oneline
显示所有提交过的用户,按提交次数排序
git shortlog -sn
显示指定文件是什么人在什么时间修改过
git blame [file]
显示暂存区和工作区的代码差异
git diff
显示暂存区和上一个commit的差异
git diff --cached [file]
显示工作区与当前分支最新commit之间的差异
git diff HEAD
显示两次提交之间的差异
git diff [first-branch]...[second-branch]
显示今天你写了多少行代码
git diff --shortstat "@{0 day ago}"
显示某次提交的元数据和内容变化
git show [commit]
显示某次提交发生变化的文件
git show --name-only [commit]
显示某次提交时,某个文件的内容
git show [commit]:[filename]
显示当前分支的最近几次提交
git reflog
从本地master拉取代码更新当前分支:branch 一般为master
git rebase [branch]

撤销

恢复暂存区的指定文件到工作区
git checkout [file]
恢复某个commit的指定文件到暂存区和工作区
git checkout [commit] [file]
恢复暂存区的所有文件到工作区
git checkout .
重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset [file]
重置暂存区与工作区,与上一次commit保持一致
git reset --hard
重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
git reset [commit]
重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
git reset --hard [commit]
重置当前HEAD为指定commit,但保持暂存区和工作区不变
git reset --keep [commit]
新建一个commit,用来撤销指定commit
后者的所有变化都将被前者抵消,并且应用到当前分支
git revert [commit]
暂时将未提交的变化移除,稍后再移入
git stash
git stash pop

Git 大全

posted @   镜子-眼泪  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
欢迎阅读『git 常用命令』

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示