基础
Git基础操作
三种状态
- Working Directory 工作区
- Staging Area 暂存区
- Repository 版本库
配置
SSH Keys
ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-keygen -t ed25519 -C "your_email@example.com"
ssh-keygen -t rsa -C "your.email@example.com" -b 4096
ssh-keygen -t rsa -C "your.email@example.com" -b 4096
配置用户名及邮箱
# 配置用户名及邮箱
git config --global user.name 'wforguo'
git config --global user.email 'wforguo@qq.com'
# 配置镜像
git config --global registry https://registry.npm.org
# 配置用户名及邮箱
git config --global user.name 'wforguo'
git config --global user.email 'wforguo@qq.com'
# 配置镜像
git config --global registry https://registry.npm.org
查看已有配置
git config --global --list
git config --global --list
user.name=wforguo
user.email=wforguo@qq.com
user.name=wforguo
user.email=wforguo@qq.com
仓库
一个项目其实就对应一个仓库,也就是一个版本库
初始化仓库
初始化仓库
git init
git init
添加代码到暂存区
git add .
git add .
提交暂存区代码到远程仓库
git commit -m "feat: git init"
git commit -m "feat: git init"
查看当前项目改动
git status
git status
查看提交记录(所有)
git log
git log
查看单个人的提交记录
git log --author wforguo
git log --author wforguo
推送代码到远程仓库
git remote add origin git@github.com:wforguo/forguo.cn.git
git push -u origin master
git remote add origin git@github.com:wforguo/forguo.cn.git
git push -u origin master
删除远程地址
git remote rm origin
git remote rm origin
文件修改查看
- 单行查看commitId及内容
git log --pretty=oneline readme.md
git log --pretty=oneline readme.md
- 查看具体commit内容,提交人及提交内容
git show a2bab1e7832a55fa2baad2bb338a17a923bc5c52
git show a2bab1e7832a55fa2baad2bb338a17a923bc5c52
- 查看单个文件修改内容
git log -p readme.md
git log -p readme.md
忽略文件
新.gitignore
文件,配置需要忽略的文件或者文件夹,对应的文件就会被git忽略
.DS_Store
node_modules
/node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
/.idea
.vscode
.DS_Store
node_modules
/node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
/.idea
.vscode
文件的操作
添加
git add readme.md
git add readme.md
删除
1、手动删除
手动删除文件,再执行git add .
,添加剩余文件到缓存区
2、代码删除
git rm readme.md
git add .
git rm readme.md
git add .
重命名
手动重命名会先删除原有的,再去新增一个改过之后的;
git mv readme.md Readme.md
git add .
git mv readme.md Readme.md
git add .
移动位置
readme.md 移动到doc文件夹下
git mv readme.md doc
git add .
git mv readme.md doc
git add .
readme.md 移动到doc文件夹下并重命名Readme.md
git mv readme.md doc/Readme.md
git add .
git mv readme.md doc/Readme.md
git add .
代码还原
单个文件
1、未提交至暂存区
还未提交到暂存区的代码,可以撤回
git checkout -- readme.md
git checkout -- readme.md
2、已提交至暂存区
从暂存区撤销,再去撤回,重复上面的操作
git reset HEAD readme.md
git checkout -- readme.md
git reset HEAD readme.md
git checkout -- readme.md
3、已经提交commit
git checkout 154653b -- readme.md
git checkout 154653b -- readme.md
git reset
原理是根据commitId
来恢复版本,也可以通过设置HEAD^
,来回到上一个版本;
可以通过git log
命令查看提交记录或者commitId
,取前七位即可;
使用:
git reset [option] [commitId]
git reset [option] [commitId]
- 上个版本
git reset --hard HEAD^
git reset --hard HEAD^
HEAD^ ,^个数代表回退到前几个版本。
- 某个版本
git reset --hard 154653b
git reset --hard 154653b
option 共有 3 个值,具体含义如下:
- --hard:撤销 commit,撤销 add,删除工作区改动代码
- --mixed:默认参数。撤销 commit,撤销 add,还原工作区改动代码【默认】
- --soft:撤销 commit,不撤销 add,还原工作区改动代码
**--hard**
参数慎用,他将你工作区的代码直接删掉
git reset是真正的回退一个版本,修改的代码都不会有log产生
git push会提示需要先pull
解决办法有两个,
1、git push -f
【也需谨慎】
2、使用git revert
git revert
新增一个提交,并且这个提交是使用上一个提交的代码。
使用:
git revert -n [commitId]
git revert -n [commitId]
Tag
查看
git tag
git tag
添加
git tag v1.0
git tag v1.0
指定commit
git tag v1.0 154653b
git tag v1.0 154653b
删除
git tag -d v1.0
git tag -d v1.0
推送
git push origin v1.0
git push origin v1.0
分支的操作
基本操作
查看分支
git branch
git branch
查看本地仓库与远程仓库的关系
git branch -av
git branch -av
创建分支
git branch newName
git branch newName
切换分支
git checkout newName
git checkout newName
创建并切换
git checkout -b newName
git checkout -b newName
切换分支并与远程分支关联
git checkout -b newName remotes/origin/newName
git checkout -b newName remotes/origin/newName
删除分支
git branch -d newName
git branch -d newName
强制删除分支
比如当前分支commit了,没有push之时
git branch -D newName
git branch -D newName
删除远程分支
git push --delete origin oldName
git push --delete origin oldName
分支重命名
git branch -m oldName newName
git branch -m oldName newName
合并分支
先切换到目标分支
git checkout target
git checkout target
合并到当前target分支
git merge dev
git merge dev
解决冲突
- 忽略其他分支的修改
git merge --abort
git merge --abort
- 手动修改后去提交
解决冲突后,提交commit
git commit -m 解决冲突
git commit -m 解决冲突
查看版本路线
查看当前的版本路线
git log --oneline --graph
git log --oneline --graph
修改分支名称
1. 本地分支重命名(还没有推送到远程)
git branch -m oldName newName
git branch -m oldName newName
2. 远程分支重命名 (已经推送远程-假设本地分支和远程对应分支名称相同)
a. 重命名远程分支对应的本地分支
git branch -m oldName newName
git branch -m oldName newName
b. 删除远程分支
git push --delete origin oldName
git push --delete origin oldName
c. 上传新命名的本地分支
git push origin newName
git push origin newName
d.把修改后的本地分支与远程分支关联
git branch --set-upstream-to origin/newName
git branch --set-upstream-to origin/newName
合并多个commit
假设存在这样的 commit 历史
通过 git log
查看 commit 历史
start fix(*): start
bug1 fix(*): bug1
bug2 fix(*): bug2
bug3 fix(*): bug3
start fix(*): start
bug1 fix(*): bug1
bug2 fix(*): bug2
bug3 fix(*): bug3
我们想要将 bug1
、bug2
和 bug3
合并成一个 commit
首先执行 git rebase
对某一段线性提交历史进行编辑、删除、复制、粘贴
最好先对你本地的 commit 合并,再push, 否则本地落后于远程仓库的提交,需要
git push -f
强制推送,难免会出问题
- 1、合并指定的commit
git rebase -i [startpoint] [endpoint]
git rebase -i [startpoint] [endpoint]
前开后闭的区间
endpoint 可选,不指定则为当前分支最新的 commit
- 2、合并第一次commit之后的所有commit
git rebase -i --root
git rebase -i --root
- 3、合并第一次commit到指定的commit之间的所有commit
git rebase -i --root commitId
git rebase -i --root commitId
- 4、合并最近的两个commit
git rebase -i HEAD~2
git rebase -i HEAD~2
这里,我们执行 git rebase -i start bug3
相当于只合并 bug1
bug2
bug3
进入交互模式
如下,按 i
进入编辑模式
修改 commit id 前面的 pick
,也就是修改对应的指令类型
pick bug1 fix(*): bug1
pick bug2 fix(*): bug2
pick bug3 fix(*): bug3
pick bug1 fix(*): bug1
pick bug2 fix(*): bug2
pick bug3 fix(*): bug3
指令类型:
# 指令类型:#
# Rebase bce8eaf onto bce8eaf (16 commands)
# Commands:
# p, pick <commit> = use commit 保留该commit
# r, reword <commit> = use commit, but edit the commit message 保留该commit,但我需要修改该commit的注释
# e, edit <commit> = use commit, but stop for amending 保留该commit,但我需要停下来修改该commit
# s, squash <commit> = use commit, but meld into previous commit 将该commit和前一个commit合并
# f, fixup <commit> = like "squash", but discard this commit's log message 将该commit和前一个commit合并,但我不要保留该提交的注释信息
# 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>]
# ...
# 指令类型:#
# Rebase bce8eaf onto bce8eaf (16 commands)
# Commands:
# p, pick <commit> = use commit 保留该commit
# r, reword <commit> = use commit, but edit the commit message 保留该commit,但我需要修改该commit的注释
# e, edit <commit> = use commit, but stop for amending 保留该commit,但我需要停下来修改该commit
# s, squash <commit> = use commit, but meld into previous commit 将该commit和前一个commit合并
# f, fixup <commit> = like "squash", but discard this commit's log message 将该commit和前一个commit合并,但我不要保留该提交的注释信息
# 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>]
# ...
这里我们使用 squash
缩写 s
, 也就是合并 commit,然后 esc
,:wq
保存退出即可。
pick bug1 fix(*): bug1
s bug2 fix(*): bug2
s bug3 fix(*): bug3
pick bug1 fix(*): bug1
s bug2 fix(*): bug2
s bug3 fix(*): bug3
修改 commit message
如下,开始对 commit message 进行修改, 可以使用 dd
删除当前行,也可以使用 i
进入编辑模式,修改 commit message, 然后 esc
, :wq
保存退出即可。
# This is a combination of 3 commits.
# This is the 1st commit message:
fix(*): bug1
# This is the commit message #2:
fix(*): bug2
# This is the commit message #3:
fix(*): bug3
# This is a combination of 3 commits.
# This is the 1st commit message:
fix(*): bug1
# This is the commit message #2:
fix(*): bug2
# This is the commit message #3:
fix(*): bug3
Successfully rebased and updated refs/heads/master.
Successfully rebased and updated refs/heads/master.
合并完成