美文网首页
Git  常用命令总结(一)

Git  常用命令总结(一)

作者: 梦幻的心爱 | 来源:发表于2019-01-09 15:00 被阅读0次

   我们在工作中经常会使用到Git,可是有时候命令太多记不住,为此我整理了一份文档,方便大家记忆和参考!

下面几个是专用名词,作为参考。

Workspace: 工作区

Index/Stage:暂存区

Repository:仓库区(或本地仓库)

Remote:远程仓库

一、配置操作

配置操作总览-01

 #指令集

 $ git  help -a

# 显示当前git配置

 $  git config --list  【简写 :git config -l】

# 更改配置文件

$ git config -e    ##单个文件配置

$ git config -e --global      ##全局配置

# 配置邮箱&&用户名

$ git config user.email "邮箱"

$ git config user.name "用户名" 

$ git config --global user.email "邮箱"          ##全局##

$ git config --global user.name  "用户名"     ##全局##

# 命令配置别名

$ git config alias.别名 原指令名称  如:git config  alias.st status     ##单个项目配置##

$ git config --global alias.别名 原指令名 如:git config --global alias.st status  ##全局配置##

二、仓库操作

仓库操作总览-02

# 初始化仓库

$ git init     ##空仓库

$ git init 仓库路径  ## 已存在

# 复制远程仓库

$ git clone  仓库路径   

# 添加文件到暂存区  

$ git add .     ## 所有文件

$ git add file1  file2 ...    ## 指定文件

# 删除工作区文件,并且将这次删除放入暂存区

$ git rm file1 file2 ...  

# 停止追踪指定文件,但该文件会保留在工作区

$ git rm --cached file  

# 提交

$ git commit -m "修改内容"    ## 提交暂存区到仓库区

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

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

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

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

# 推送

$ git push origin master    ## 提交更改到远程仓库

# 拉取

$ git pull origin master    ## 拉取远程更改到本地仓库默认自动合并

三、分支操作

分支操作总览-03

# 列出分支【查】

$ git branch -a    ## 列出本地和远程分支

$ git branch    ## 列出所有本地分支

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

# 新建 【增】

$ git branch [branch-name]    ## 新建一个分支,但依然停留在当前分支

$ git checkout -b [branch]     ## 新建一个分支,并切换到该分支

$ git branch [branch] [commit]     ## 新建一个分支,指向指定commit

$ git branch --track [branch] [remote-branch]     ## 新建一个分支,与指定的远程分支建立追踪关系

# 切换 【改】

$ git checkout [branch-name]    ## 切换到指定分支,并更新工作区

$ git checkout -    ## 切换到上一个分支

$ git branch -m brancholdname branchnewname    ## 重命名分支

# 合并【合】

$ git branch --set-upstream [branch] [remote-branch]    ## 建立追踪关系,在现有分支与指定的远程分支之间

$ git merge [branch]    ## 合并指定分支到当前分支,如果有冲突需要手动合并冲突(就是手动编辑文件保存咯),然后add,commit再提交

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

# 删除【删】

$ git branch -d [branch-name]    ## 删除分支

$ git push origin --delete [branch-name]    ## 删除远程分支

$ git branch -dr [remote/branch]

四、标签操作

标签操作总览-04

# 列出所有tag

$ git tag

# 新建一个tag在当前commit

$ git tag [tag]

# 删除本地tag

$ git tag -d [tag]

# 删除远程tag

$ git push origin :refs/tags/[tagName]

# 查看tag信息

$ git show [tag]

# 提交指定tag

$ git push [remote] --tags

# 提交所有tag

$ git push --tags

# 新建一个分支,指向某个tag

$ git checkout -b [branch] [tag]

五、撤销操作

撤销操作总览-05

# 恢复暂存区的指定文件到工作区

$ git checkout [file]

# 恢复某个commit的指定文件到暂存区和工作区

$ git checkout [commit] [file]

# 恢复暂存区的所有文件到工作区

$ git checkout .

# 回退到上一个版本,在Git中,用HEAD表示当前版本

$ git reset --hard HEAD^

# 重置暂存区的指定文件,与上一次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]

# 暂时将未提交的变化移除,稍后再移入

$ git stash

$ git stash pop

六、文件信息

文件信息总览-06

#  显示当前分支的版本历史

$ git log

# 搜索提交历史,根据关键词

$ 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]

七、组件化常用整理

# 组件快速创建

$ pod lib create [组件名]

# 本地验证

$ pod lib lint

$ pod lib lint --allow-warnings        ## 本地验证  忽略警告

# 远程验证

$ pod spec lint

$ pod spec lint --allow-warnings    ## 远程验证 忽略警告

# 查看本地索引库

$ pod repo

# 向本地端提交索引库 也就是把.spec 提交到本地创建的索引库中

$ pod  repo  push [本地索引库名称] xxxx.spec

$ pod  repo  push [本地索引库名称] xxxx.spec --allow-warnings    ##忽略警告

# 移除本地索引库

$ pod repo remove [本地索引库名称]

# 移除cocopod 索引文件

$ rm ~/Library/Caches/CocoaPods/search_index.json

八、其他扩展

# 显示当前分支的最近几次提交

$ git reflog

# 记录某个文件的更改历史和更改人

$ git blame filepath

# 覆盖本地内容

$ git fetch --all   

$ git reset --hard origin/master  

$ git pull

相关文章

  • git 命令

    Git常用命令总结Git常用命令总结 git init 在本地新建一个repo,进入一个项目目录,执行git ...

  • Git 常用指令

    Git常用命令总结 Git常用命令总结 git init 在本地新建一个repo,进入一个项目目录,执行git i...

  • git相关教程汇总

    1. git常用命令 git常用命令总结git常用命令讲解 2. git教程相关网站 廖雪峰的git教程猴子都能懂...

  • Git与Github的使用总结 - day 03

    git常用命令总结 git配置(config): git仓库(repository): git分支(branch)...

  • Git常用命令

    Git 常用命令总结 git的一些基础命令 Git常用命令 一般配置 登录git 创建一个文件夹 初始化git仓库...

  • Git 常用命令

    Git更多详细介绍 查看git-book git教程 - 廖雪峰 Git 常用命令 总结 $ git init 通...

  • git常用命令

    对git常用命令做个总结,以便以后使用:

  • GIT 常用命令总结

    GIT 常用命令总结 GIT 初始化命令 命令描述git init初始化本地 git 仓库git config -...

  • Git常用命令总结

    Git常用命令总结 1,初始化:git init 创建git仓库 2,配置用户:git config --glo...

  • Git常用命令总结

    Git常用命令总结 0.Git init 初始化一个本地仓库 1.Git status 是查看Git的状态(目前在...

网友评论

      本文标题:Git  常用命令总结(一)

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