美文网首页
git 命令列表

git 命令列表

作者: 零一间 | 来源:发表于2021-05-17 23:21 被阅读0次

常用 Git 命令清单

一般来说,日常使用只要记住下图6个命令,就可以了。但是熟练使用,恐怕要记住60~100个命令。

img

一、新建代码库

# 在当前目录新建一个Git代码库
$ git init

# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]

# 下载一个项目和它的整个代码历史
$ git clone [url]

二、配置

Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。


#配置使用git仓库的人员姓名  
git config --global user.name "Your Name Comes Here"  
  
#配置使用git仓库的人员email  
git config --global user.email you@yourdomain.example.com  
  
#配置到缓存 默认15分钟  
git config --global credential.helper cache   
  
#修改缓存时间  
git config --global credential.helper 'cache --timeout=3600'    
  
git config --global color.ui true  
git config --global alias.co checkout  
git config --global alias.ci commit  
git config --global alias.st status  
git config --global alias.br branch  
git config --global core.editor "mate -w"    # 设置Editor使用textmate  
git config -l #列举所有配置  
  
#用户的git配置文件~/.gitconfig  

三、增加/删除文件

# 添加指定文件到暂存区
$ git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
$ git add [dir]

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

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
$ git add -p

# 删除工作区文件,并且将这次删除放入暂存区
$ git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
$ git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
$ git mv [file-original] [file-renamed]

四、代码提交

# 提交暂存区到仓库区
$ git commit -m [message]

# 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区
$ git commit -a

# 提交时显示所有diff信息
$ git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
$ git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
$ git commit --amend [file1] [file2] ...

五、分支

# 列出所有本地分支
$ 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]

六、标签

# 列出所有tag
$ git tag

# 新建一个tag在当前commit
$ git tag [tag]

# 新建一个tag在指定commit
$ git tag [tag] [commit]

# 删除本地tag
$ git tag -d [tag]

# 删除远程tag
$ git push origin :refs/tags/[tagName]

# 查看tag信息
$ git show [tag]

# 提交指定tag
$ git push [remote] [tag]

# 提交所有tag
$ git push [remote] --tags

# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

七、查看信息

# 显示有变更的文件
$ 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

八、远程同步

# 下载远程仓库的所有变动
$ git fetch [remote]

# 显示所有远程仓库
$ git remote -v

# 显示某个远程仓库的信息
$ git remote show [remote]

# 增加一个新的远程仓库,并命名
$ git remote add [shortname] [url]

# 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]

# 上传本地指定分支到远程仓库
$ git push [remote] [branch]

# 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force

# 推送所有分支到远程仓库
$ git push [remote] --all

九、撤销

# 恢复暂存区的指定文件到工作区
$ 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

十、其他

# 生成一个可供发布的zip压缩包
$ git archive -v --format=zip v1.9.1 > v1.9.1.zip
# 生成一个可供发布的zip压缩包,添加路径前缀v1.9.1
$ git archive -v --format=zip --prefix=v1.9.1/ v1.9.1 > v1.9.1.zip
# 生成一个可供发布的tar.gz压缩包
$ git archive -v --format=tar.gz --prefix=v1.9.1/ v1.9.1 > v1.9.1.tar.gz

#展示关联远程分支名
$ git branch -vv 
#,关联远程分支
$ git branch -u origin/mybranch
$ git push origin/mybranch -u

#远程删除,对应的本地分支也删除
$ git remote prune origin

#从远程分支创建并切换到本地分支
$ git checkout -b <branch-name> origin/<branch-name>

#删除远程分支
$ git push origin --delete <remote-branchname>
$ git push origin :<remote-branchname>

#重命名本地分支
$ git branch -m <new-branch-name>

#推送标签到远程仓库
$ git push origin <local-version-number>
#一次性推送所有标签
$ git push origin --tags

# 以新增一个 commit 的方式还原某一个 commit 的修改
$ git revert <commit-id>



# 修改上一个 commit 的描述
$ git commit --amend

# 把 A 分支的某一个 commit,放到 B 分支上
$ git checkout <branch-name> && git cherry-pick <commit-id>

# 存储当前的修改
$ git stash
$ git stash list
$ git stash pop
$ git stash clear
$ git checkout <stash@{n}> -- <file-path>


#展示简化的 commit 历史
$ git log --pretty=oneline --graph --decorate --all


命令缩写

$ git branch -v
$ git config --global alias.st status
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch

撤销commit

回到某个 commit 的状态,并删除后面的 commit和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit

--mixed
意思是:不删除工作空间改动代码,撤销commit,并且撤销git add . 操作
这个为默认参数,git reset --mixed HEAD^git reset HEAD^ 效果是一样的。

--soft
不删除工作空间改动代码,撤销commit,不撤销git add .

--hard
删除工作空间改动代码,撤销commit,撤销git add .

注意完成这个操作后,就恢复到了上一次的commit状态。

#默认就是-mixed参数。
$ git reset <commit-id>  

#回退至上个版本,它将重置HEAD到另外一个commit,并且重置暂存区以便和HEAD相匹配,但是也到此为止。工作区不会被更改。
$ git reset --mixed HEAD^  

#回退至三个版本之前,只回退了commit的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接commit即可  
$ git reset --soft HEAD~3 

#彻底回退到指定commit-id的状态,暂存区和工作区也会变为指定commit-id版本的内容
$ git reset --hard <commit-id>  

切换到指定的版本号

$ git checkout 012013ae73f7fc9aed82c7b816885a86c1bfeec4 //切换到这个commit下
$ git checkout -b feature-xxx-0714-2.0 012013ae73f7fc9aed82c7b816885a86c1bfeec4 //在本地新建一个dev_2.0分支
$ git branch //查看分支
$ git push --set-upstream origin dev_2.0 //将本地dev_2.0分支推到远程

常用分支或者前缀

  • master 分支(主分支) 稳定版本

  • develop 分支(开发分支) 最新版本

  • release 分支(发布分支) 发布新版本

  • hotfix 分支(热修复分支) 修复线上Bug

  • feature 分支(特性分支) 实现新特性


git工作流

  • 主干分支。主干开发,主干。
  • 开发分支。团队根据自己的情况可以创建dev,beta,prod等分支。 开发人员基于稳定分支创建新的分支用来开发新的功能。如果三个分支的历史变更比较一致,可以采用merge方式合并到不同环境上进行测试。 如果各个分支的变更记录差别较大,可以采用cherry-pick方式处理。 最终发布依然是主干分支发布。
  • 预发布分支。即可以采用功能分支发布,主干分支开发。 也可以采用功能分支开发,最终合入主干发布。
  • 修补bug分支。基于生产创建,完成各个环节测试后,直接合入主干。则为主干发布。

常用 Git 命令清单 http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

Git的奇技淫巧 https://github.com/521xueweihan/git-tips#%E5%B1%95%E7%A4%BA%E5%B8%AE%E5%8A%A9%E4%BF%A1%E6%81%AF

相关文章

  • git简单使用命令集合

    查询类 git help (git --help) 查询所有命令列表 git help -a 展示所有命令列表 g...

  • Git 和 Github

    首先安装一个git sudo apt install git git命令列表查看 请在命令行里输入git,如果出现...

  • GIT

    git 闪退 git 放弃本地修改,强制拉取更新 git git命令: 查看分支列表: `git branch ...

  • git命令列表

    一、分支相关命令 git branch //查看本地分支 git branch -a ...

  • git 命令列表

    废话:Git,是分布式版本控制系统,在多人开发的模式下,相信很多人都在用高大上的git,git是目前版本控制系统中...

  • git 命令列表

    常用 Git 命令清单 一般来说,日常使用只要记住下图6个命令,就可以了。但是熟练使用,恐怕要记住60~100个命...

  • git & github

    git:代码管理工具 git命令详解 pwd 当前目录 cd 返回层级 和 .、..连用 ls 列表 ls -a ...

  • 简单快捷SVN2Gitlab

    1.安装git-svn 命令:yum install -y git-svn 2.获取作者名字列表 svn co...

  • Git 使用

    1 切换远程分支 先用fetch命令更新remote索引 -> git fetch 列出远程分支列表 -- ...

  • git cherry 命令使用

    git cherry 命令使用 git cherry 找到本地提交列表中,尚未推送到远程的提交 1. 没有参数的情...

网友评论

      本文标题:git 命令列表

      本文链接:https://www.haomeiwen.com/subject/vcknrltx.html