• Git——常用命令总结


    前言

    记录日常中常用的Git命令

    安装

    Git官网 | Git官网

    Git下载 | Git下载

    配置

    配置user.nameuser.email

    git config --global user.name 'your_name'
    git cofig --global user.email 'your_email@domain.com'
    
    # 将默认编辑器设置为vim
    git config --global core.editor vim
    

    作用域

    # 某个仓库有效(默认)
    git config --local
    
    # 全局有效(当前用户所有仓库有效)
    git config --global
    
    # 系统所有登陆用户有效
    git config --system
    

    显示config配置

    # 查看本地配置
    git config --list --local
    
    # 查看全局配置
    git config --list --global
    
    # 查看系统配置
    git config --list --system
    

    创建仓库

    cd your_project 
    git init 
    git config --global user.name 'your_name'
    git cofig --global user.email 'your_email@domain.com'
    git remote add origin <git addr>
    git add -A
    git commit -m "add file"
    git push
    

    目录结构

    .git
    ├─ COMMIT_EDITMSG
    ├─ FETCH_HEAD
    ├─ HEAD #该文件表示当前本地签出的分支,例如存储的值:ref: refs/heads/master
    ├─ ORIG_HEAD
    ├─ config
    ├─ description
    ├─ hooks
    │    ├─ applypatch-msg.sample
    │    ├─ commit-msg.sample
    │    ├─ fsmonitor-watchman.sample
    │    ├─ post-update.sample
    │    ├─ pre-applypatch.sample
    │    ├─ pre-commit.sample
    │    ├─ pre-push.sample
    │    ├─ pre-rebase.sample
    │    ├─ pre-receive.sample
    │    ├─ prepare-commit-msg.sample
    │    └─ update.sample
    ├─ index #存储缓冲区(GitExtensions中的stage)的内容,内容包括它指向的文件的时间戳、文件名、sha1值等;(git三大区域:工作区,暂存区,历史记录区)
    ├─ info
    │    ├─ exclude
    │    └─ refs
    ├─ logs
    │    ├─ HEAD
    │    └─ refs
    │           ├─ heads
    │           ├─ remotes
    │           └─ stash
    ├─ objects #存储对象的目录,本地仓库,git中对象分为三种:commit对象,tree对象(多叉树),blob对象
    │    ├─ 06
    │    │    └─ abbb19400a205bdbd1dc0a28b986323fc8b808
    │    ├─ 0e
    │    │    └─ 5d9122f24db7db88b7688759bf457be74e8643
    │    ├─ 16
    │    │    └─ 89c24264c1fde08df4773db3cfbf6316d7e2b3
    │    ├─ 2c
    │    │    └─ 936a4ca60dd5c8f4ceeb9fede83a2c2f2c69b5
    │    ├─ 48
    │    │    └─ 0be438bab34a5361d8949f690a346973b8eb79
    │    ├─ 72
    │    │    └─ 7c5abd67dfeb49ee214b6a112445fbb550d3c5
    │    ├─ bd
    │    │    └─ 3441fa796371e5f796a1d522f8052b763ad533
    │    ├─ f1
    │    │    └─ 894b05a9021dded510f71fef2be2cfb9864db6
    │    ├─ info
    │    │    └─ packs
    │    └─ pack
    │           ├─ pack-76de1ecd79b2e31879a027b482c07377610a8bd6.idx
    │           └─ pack-76de1ecd79b2e31879a027b482c07377610a8bd6.pack
    ├─ packed-refs
    └─ refs #存储指向branch的最近一次commit对象的指针,也就是commit对象的sha-1值(就是hash值,sha-1是一种散列算法),refs的目录下包括以下目录(git init后并没有remotes和stash,需要有从remote地址中pull code等交互性操作才会出现remotes目录,stash文件则是做了stash操作才会出现):
           ├─ heads
           │    ├─ dev
           │    ├─ master
           │    └─ tb-customer
           ├─ remotes
           │    └─ origin
           └─ stash
    

    命令详解

    add

    # 将工作空间修改和删除的文件添加到暂存区
    git add -u 
    
    # 将工作空间新增和被修改的文件添加的暂存区
    git add .
    
    # 将工作空间新增、修改和删除的文件添加到暂存区
    git add -A
    

    mv

    # 修改文件名称
    git mv <old_name> <new_name>
    

    rm

    # 删除文件
    git rm <file_name>
    

    log

    # 查看单行的简洁日志
    git log --oneline
    
    # 查看最近2条简洁日志
    git log --oneline -n4
    git log --oneline -4
    
    # 查看所有分支的历史日志
    git log --all
    
    # 以图形化展示历史日志
    git log --all --graph
    
    # 以图形化展示所有分支的最近4条简洁日志
    git log --all --oneline -4 --graph
    
    # 在网页端查看git log命令文档
    git help --web log
    
    # 在GUI界面查看
    gitk --all
    

    commit

    # 修改上一次提交commit信息
    git commit --amend
    
    # 修改指定commit信息
    git rebase -i <commit_id>
    r  <修改commit信息>
    p  <保留commit信息>
    
    # 把连续多个commit合并成一个
    git rebase -i <last_commit_id>
    p  <需保留的cmmit信息>
    s  <需合并到上一个commit信息>
    
    # 把间隔的commit合并成一个
    git rebase -i <last_commit_id>
    p  <需保留的commit信息>
    s  <把需要合并的commit的移动到需要保留的commit下面并选择squash策略>
    
    

    rebase策略

    # Commands:
    # p, pick <commit> = use commit
    # r, reword <commit> = use commit, but edit the commit message
    # e, edit <commit> = use commit, but stop for amending
    # s, squash <commit> = use commit, but meld into previous commit
    # f, fixup <commit> = like "squash", but discard this commit's log message
    # x, exec <command> = run command (the rest of the line) using shell
    # b, break = stop here (continue rebase later with 'git rebase --continue')
    # d, drop <commit> = remove commit
    # l, label <label> = label current HEAD with a name
    # t, reset <label> = reset HEAD to a label
    # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
    # .       create a merge commit using the original merge commit's
    # .       message (or the oneline, if no original merge commit was
    # .       specified). Use -c <commit> to reword the commit message.
    

    diff

    # 比较暂存区和HEAD所含文件差异
    git diff --cached
    git diff --staged
    
    # 比较工作区和暂存区所含文件差异
    git diff 
    
    # 比较工作去和暂存区指定文件差异(可比较多个文件)
    git diff -- <file_name_1> <file_name_2>
    
    # 比较两个commit之间的差别
    git diff <commit_id_1> <commit_id_2>
    
    # 比较两个commit之间的指定文件差别
    git diff <commit_id_1> <commit_id_2> -- <file_name>
    

    reset

    # 将暂存区恢复成和HEAD一样
    git reset HEAD
    
    # 将暂存区指定文件恢复成和HEAD一样(可多个文件)
    git reset HEAD -- <file_name_1> <file_name_2>
    
    # 将暂存区恢复到指定的commit
    git reset --hard <commit_id>
    
    # 将指定文件的工作区恢复成和暂存区一样
    git checkout -- <file_name>
    git restore -- <file_name>
    
    

    branch

    # 查看本地所有分支
    git branch -v
    
    # 删除指定分支
    git branch -d <branch_name>
    
    # 强制删除指定分支
    git branch -D <branch_name>
    
    
    
    

    checkout

    # 创建新的分支
    git checkout -b <branch_name>
    
    # 切换分支
    git checkout <branch_name>
    git switch <branch_name>
    

    stash

    # 存储当前修改
    git stash
    
    # 查看stash列表
    git stash list
    
    # 恢复之前存储内容(保留stash信息)
    git stash apply
    
    # 恢复之前存储内容(删除stash信息)
    git stash pop
    

    remote

    # 添加远程分支(一般使用智能协议[http/https|ssh])
    git remote add <local_barnch_name> <remote_address>
    
    # 查看本地分支
    git remote -v
    
    # 查看所有分支
    git remote -va
    
    

    fetch

    # 拉取远程所有分支
    git fetch --all
    
    # 拉取指定分支
    git fetch <local_branch_name> <remote_branch_name>
    
    #
    

    merge

    # 合并和远程不相关的分支
    git merge --allow-unrelated-histories <remote_branch_name>
    
    # 图形化合并界面
    git mergetool
    
  • 相关阅读:
    10.18
    10.16~10.17笔记
    JS
    10.8~10.11
    9.28~9.29
    9.27 代码笔记
    代码复习(9.26)
    9.19 链家
    9.18笔记
    9.17 定位
  • 原文地址:https://www.cnblogs.com/wangyang0210/p/13379529.html
Copyright © 2020-2023  润新知