Git

提交路径: 工作区(Directory):git add---》暂存区(Stage/Index):git commit---》本地版本库(HEAD):git push---》远程版本库

  • Workplace:工作区
  • Index:暂存区
  • Repository:本地版本库
  • Remote:远端版本库

配置Git

git config --global --list  # 显示全局配置信息
git config --global --edit  # 交互式全局配置
git config --global --unset [value-regex]  # 去除全局配置

git config --global user.name "your_name"  # 配置全局用户名
git config --global user.email "your_email@example.com"  # 配置全局邮件地址
git config --global https.proxy "proxy_server"  # 配置全局https代理
git config --global http.proxy "proxy_server"  # 配置全局htttp代理

git config --list  # 显示当前配置信息
git config --list --show-origin  # 显示当前设置及其来源
git config --local --list  # 显示local repository配置信息(在local repository目录下执行)

注意:git config命令的--global参数表示本机所有的Git仓库都使用这个配置,也可以通过--local参数单独对某个仓库设置不同的用户名和Email地址等信息。

别名

git config --global alias.<alias-name> "<git command>"  # 创建别名
git <alias-name> <more optional arguments>  # 使用别名

本地版本库

git init <folder_name>  # 建立本地Git仓库, 将在<folder_name>下创建.git目录
git remote add <remote_branch_name> <remote_repository_url>  # 将创建的本地仓库与远程仓库连接
git remote -v  # 查看remote repository详细信息,包括推送权限和remote repository的地址
git pull <remote_branch_name>  # 拉取远程仓库最新的改动到本地仓库

git clone <remote_repository_url>  # 克隆remote repository到本地当前目录

添加改动

git add <file>  # 添加指定改动文件到暂存区
git add .    # 添加所有改动文件到暂存区
git add --all    # 添加所有改动文件到暂存区

git rm <file>  # 删除文件,并且将这次删除放入暂存区
git mv <file_name> <file_new_name>  # 重命名文件,并且将改动放入暂存区

显示改动

git status  # 显示当前状态(工作区的改动显示为红色,暂存区的改动显示为绿色)
git diff  # 显示工作区的所有修改内容
git diff <file> # 显示工作区指定文件修改内容
git diff HEAD -- <file>  # 显示工作区和最新版本库的区别

撤销修改

git clean -f 删除未跟踪的文件(Untracked files)
git clean -df # 删除未跟踪的目录和文件
git clean -ndf  # "-n"参数表示预删除,用来确认预计结果,防止重要文件被误删

git checkout -- .  # 撤销工作区的所有修改(只影响track文件)
git checkout -- <file>  # 撤销工作区指定文件的修改(只影响track文件)

git reset .  # 撤销暂存区中所有的修改,放回工作区(只影响track文件)
git reset <file_name>  # 撤销暂存区某个文件的修改,放回工作区(只影响track文件)

git reset --hard  # 放弃本地所有未提交的修改(工作区和暂存区),回退到最新Commit的状态

提交

git commit -m <commit message>  # 提交改动到本地仓库并填写描述
git commit <file> -m <commit message>  # 提交指定文件改动到本地仓库并填写描述
git commit --amend -m <new commit message>   # 修改最后一次的commit message(不能在已推送的commit中使用)
git commit --amend  #  重做上一次commit,包括指定文件的改动
git rebase -i  # 合并本地已进行的多次提交

日志

git log  # 显示提交日志
git log -<number>  # 显示指定数目的提交日志
git log --oneline  # 显示简洁提交日志, 每个提交显示一行
git log --oneline --graph  # 图形化方式显示简洁提交日志, 每个提交显示一行

git reflog  # 显示已执行命令日志
git reflog -<number>  # 显示指定数目的已执行命令日志

版本回退

git log --oneline -<number>  # 查看提交历史,以便确定要回退到哪个版本
git reflog  # 查看命令历史,以便确定要回到未来的哪个版本

git reset --hard <commit_id>  # 将工作区回退到某个commit版本,使用 “--hard” 参数将会丢弃所有当前本地改动
git reset --keep <commit_id>  # 将工作区回退到某个commit版本,使用 “--keep” 参数表示将涉及 “回滚” 的所有改动转换成本地改动,并保留在工作区

git reset --keep HEAD^  # 回退到上一个版本。HEAD表示当前版本,上一个版本就是HEAD^,上两个版本就是HEAD^^,上10个版本可以写成HEAD~10

远程仓库

远程仓库的默认名称是origin,也可以改为成其他名字。

git clone <remote_repository_url>  # 克隆remote repository到本地当前目录

git fetch  # 同步新增的远程分支和tag到本地 (但不会同步已删除的远程分支信息到本地)
git fetch --prune  # 同步已删除的远程分支信息到本地

git pull  # 从取回远程仓库的变化,并与本地分支合并,其实是git fetch和git merge的结合使用
git pull --rebase <remote_repository>  # 是git fetch和git rebase的结合使用,可以理解为“想把修改建立在其他人的修改之上"。

git push -u origin <branch_name>  # 第一次发布本地分支到远程仓库(创建新远程分支并映射本地分支),此后只需执行 git push 命令能提交改动到远程分支
git push origin <branch_name>  # 推送分支的最新修改到remote repository
git push --set-upstream origin <branch_name>  # 设置本地分支和远程分支的映射关系,,此后只需执行 git push 命令能提交改动到远程分支

git remote  # 显示remote repository信息
git remote -v  # 显示所有remote repository详细信息,包括推送权限和remote repository的地址

分支管理

分支模型定义了创建、合并、删除分支的场景与时机。
不同的分支模型对应了不同的分支管理策略。

创建分支时,要先使用git status查看当前分支状态,在确认合适的情况下创建分支。
合并分支时,

  • Fast forward模式,删除分支后,会丢掉分支信息,就看不出来曾经做过合并
  • 禁用Fast forward模式,在merge时生成一个新的commit,从分支历史上就可以看出分支信息
git branch  # 显示本地分支,当前分支有*号标识
git branch -a  # 显示所有分支
git branch -r # 显示所有远程分支
git branch <branch_name>  # 创建分支
git checkout <branch_name>  # 切换分支
git checkout -b <branch_name>  # 创建并切换到新分支
git checkout -b <branch> <tag>  # 新建一个分支,指向某个tag

git cherry-pick <commit>  # 选择一个commit合并到当前分支

git merge <branch_name>  # 合并指定分支到当前分支
git merge --no-ff -m "comments" <branch_name>  # 禁用Fast forward模式合并分支,加上-m参数以便填写一个新的commit描述

git branch -d <branch_name>  # 删除本地分支
git branch -D <branch_name>  # 强行删除没有被合并过的本地分支
git push origin --delete <remote_branch_name>  # 删除远程分支

git log --oneline --graph  # 图形化显示分支合并日志

标签

git tag  # 列出本地所有tag
git ls-remote --tags  # 列出远程tags
git show <tag>  # 查看tag详细信息

git checkout <tag>  # 切换到指定tag

git tag <tag>  # 新建一个tag在当前commit
git tag -a <tag> -m <comments>  # 新建一个tag在当前commit,并添加注解信息
git tag -a <tag> <commit> -m <comments>  # 在指定commit新建一个tag,并添加注解信息

git tag -d <tag>  # 删除本地指定tag
git push origin :refs/tags/<tag>  # 删除远程指定tag

git push origin --tags  # 推送所有标签
git push origin <tag>  # 推送指定版本的标签

空运行(Dry Run)

对命令的实际产生结果进行预先检查

git clean -n/--dry-run
git add -n/--dry-run
git rm -n/--dry-run

git merge 模拟 Dry-Run

git merge --no-commit --no-ff <branch>
git diff --cached
git merge --abort

帮助与教程

  • 显示帮助指南: git help --guide
  • 显示可用命令和一些概念指南: git help -agit help -g
  • 在浏览器中显示总体教程: git help git
  • 在浏览器中显示指定命令或概念的教程: git help <command>git help <concept>
原文地址:https://www.cnblogs.com/anliven/p/6385971.html