版本管理工具Git

144 阅读3分钟

推荐使用软件

  • sourceTree

配置

# 查看配置信息
git config --list
git config user.name

# 配置用户名和邮箱 去掉--global只对当前仓库有效
git config --global user.name "myName"
git config --global user.email myEmail@qq.com

基本操作

# 初始化仓库
git init

# clone并重新定义项目目录名称
git clone git://github.com/schacon/grit.git mygrit

# 查看仓库状态,-s获得简短输出结果:AM表示添加到缓存后又修改了
git status -s

# 比较文件不同(更喜欢用VSCode查看不同)
git diff

# 恢复工作区指定文件
git checkout [file]
git checkout . 

# 将多个文件、指定目录下、所有添加到暂存区
git add [file1] [file2]
git add [dir]
git add .
# 取消指定文件的add
git reset HEAD A.php

# 提交暂存区指定文件到本地仓库 
git commit [file1] [file2] -m "提交说明"
# Linux系统commit使用单引号,Windows系统需要使用双引号
git commit -m "提交说明"
# -a参数设置修改后不用add,直接提交
git commit -am "提交说明"
# 提交不校验
git commit --no-verify -m "提交说明"
# 提交后修改注释
git commit --amend -m "提交说明2"
# 撤回提交
git reset --soft HEAD^

# 回退版本 --soft会保留暂存区和工作区 --minxed默认,会保存工作区 --hard暂存区和工作区都不保存
git reset [--soft | --mixed | --hard] [HEAD]
# 回退所有内容到上一版本
git reset HEAD^
git reset HEAD~1
# 回退指定文件到指定版本
git reset 052e A.php

# 删除文件,手工删除运行status会提示Changes not staged for commit
# 从暂存区和工作区中删除
git rm [file]
# 删除之前修改过并放到暂存区的,需要使用-f强制删除
git rm -f [file]
# 仅从跟踪清单中删除,文件依然存在,使用--cached
git rm --cached [file]

# 移动或重命名文件、目录或软连接
# 添加一个文件并重命名
git add README
git mv README README.md

# 提交日志
# 使用--oneline查看历史记录的简洁版本
git log --oneline
# 使用--graph查看历史中的分支、合并,开启拓扑图选项
# 使用--reverse逆向显示
# 使用--author查看指定用户的提交

# 查看指定文件的修改历史
git blame [file]

# 对远程仓库操作
# 查看本地仓库对应的远程地址
git remote -v
# 查看某个远程仓库信息
git remote show https://github.com/tianqixin/runoob-git-test
# 删除远程仓库
git remote rm name
# 修改仓库名
git remote rename oldName newName

# 从远程获取代码到本地的远程仓库(本地保存两个版本的仓库分为本地仓库和远程仓库)
# fetch + merge === pull
git fetch origin
git merge origin/master

# 从远程获取代码并合并本地的版本
git pull origin

# 将本地分支推送到远程并合并
git push origin master

分支管理

# 创建一个新分支
git branch myBranch
git checkout -b myBranch

# 删除分支
git branch -d branchName

# 合并B分支到当前分支
git merge Bbranch
# 撤销merge
git reset --hard [merge前commitId]

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

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

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

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

标签

# 创建带注解的标签,-a意思是创建一个带注解的标签
git tag -a v1.0

# 给提交追加标签
git tag -a v2.0 85fc7e7

# 查看所有标签
git tag

# 删除标签
git tag -d v1.0

# 查看该版本修改的内容
git show v2.0

# 指定标签信息
git tag -a tagName -m "xxx标签"

# PGP签名标签
git tag -s tagName -m "xxx标签"

# 提交标签
git push origin v1.0