git -命令

372 阅读6分钟

1.基础配置与命名

1.1单个账户登陆

最新git建议使用git ssh方式鉴权

  1. 在github仓库获取代码时候,选择ssh的url

  2. 本地生成公私钥,mac ssh-keygen -t rsa -b 4096 -C "test@xx.com"

  3. 在/Users/xxx.xx/.ssh/ 下生成 id_rsa 和 id_rsa.pub

  4. 复制id_rsa.pub的公钥内容到 github -> setting -> SSH and GPG keys -> New SSH key

  5. 下载 git clone git@github.com:mjsong07/directus_demo.git

1.2同一个电脑连两个github账户登陆

ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_second
cat ~/.ssh/id_rsa_second.pub
touch ~/.ssh/config # 没有就创建一个

# 输入下面内容
# Default GitHub account
Host github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa

# Second GitHub account
Host github-second
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_second

# 测试
ssh -T git@github.com
ssh -T git@github-second # 注意第二个域名已经改变 记得要用`github-second`
# 测试下载
git clone git@github-second:xxxxxx/repo.git

1.3Git 分支类型

  • master 分支(主分支) 稳定版本
  • develop 分支(开发分支) 最新版本
  • release 分支(发布分支) 发布新版本
  • hotfix 分支(热修复分支) 修复线上Bug
  • feature 分支(特性分支) 实现新特性
#设置全局账号信息
git config --global user.email "xxxx@xxx.cn"
git config --global user.name "Your Name"
git config --global user.password "Your password"

#创建git项目
git init
#时刻检查状态
git status

#状态:
untracked 未跟踪 (新创建的文件filename.xx)
tracked 已跟踪 (通过 git add 添加过的)


2.缓存区 staged 操作

# 添加指定文件到 缓存区
git add filename.xx
# 添加所有到 缓存区
git add .

# 显示缓存区文件列表
git ls-files

#取消缓存文件 -f强行执行force 
git rm --cached filename.xx
# -r递归文件夹 清空所有缓存
git rm -r --cached .

#添加 忽略文件.gitignore
touch .gitignore
echo 'filename.xx' > .gitignore


# 总是有文件已经忽略但是还是提示要提交
# 先修改 .gitignore 添加要忽略的文件夹 或文件
git rm -r --cached . # 先清空
git status # 查看提示删除的
git add . # 添加本地缓存
git status # 再次查看

#恢复指定缓存文件 (只处理add 的文件,新文件不算)
# 注意如果 checkout 后面是 分支名字 会当切换分支处理
git checkout filename.xx #等价于 git checkout -- filename.xx
#恢复所有缓存文件 
git checkout .

#取消当前编辑
git restore filename.xx
#取消当前所有编辑
git restore .

3.版本库 repository 操作

#缓存文件提交到版本库
git commit -m '描述'
#添加缓存 + 提交版本库 (用于处理已经add过的文件,新建文件只能先执行git add ,再git commit)
git commit -am '描述'

# 保存现场到工作栈
git stash 
# 从工作栈还原
git stash pop
#放弃-版本库-指定文件修改,回退到缓存
git restore --staged filename.xx
#放弃-版本库-所有修改,回退到缓存
git restore --staged .

# 取消当前已经commit的内容,状态为待push状态,回滚到上一次
git log 查看提交日志,获取需要回退的版本号 ID
git reset –-soft <版本号>   # 本地改的代码不变 

# 回退上一个版本,工作区代码不修改
git reset HEAD^ 

# 回退上一个版本,工作区代码一起修改
git reset --hard HEAD^  # 加了 --hard 本地代码一起回滚到之前


# 回滚之后 要同步服务器代码,这里要用-f 强制推送,如果是master分支 还要在后台去掉 保护分支的设置
git push -f 

#查看提交记录
git log
#查看提交记录-简约版
git log --oneline
#查看提交记录-样式版本
git log --pretty=oneline
#查看提交记录-完整的包括引用记录
git reflog

4.版本库 分支

#>>>>  	版本库 分支   >>>>
#创建分支
git checkout -b 'newBranch'
#查看当前所有分支
git branch 
#合并当前分支,假设当前是master上
#删除当前分支
git branch -d newBranch


# merge 合并分支 
git checkout master # 先签出被合并的分支master
(master) git merge newBranch #注意: merge逻辑: 当前活动分支master是被合并的分支, newBranch 则是参考的分支 #合并分支,如果不冲突自动add commit
#合并分支 同时把newBranch所有的log都合并成一个
git merge --squash newBranch所有的log都合并成一个,没冲突也要自己commit
#合并不使用快速合并,保留原来节点
git merge --no-ff newBranch

# rebase 变基 
# 原理: 其实就是把分支的所有commit 都一个个,同步到master 的最前面,然后newBranch分支自己就不存在了,跟master在同一个分支上
git checkout newBranch # 先签出要变基的分支
(newBranch) git rebase master # 以master为基础,再追加到当前活动分支newBranch上面的节点信息,并强制覆盖为newBranch完整的所有信息。 也可以使用  git rebase -i xxxxxid 指定具体的commit位置
:q # 提示要操作的所有commit信息 ,直接退出
# 一般会存在冲突,需要每哥重新commit的节点都解决一下冲突 然后使用
git status # 查看提示
git add . # 解决冲突后执行
git rebase --continue # 进行下一个
# 最终完成后,会提示要pull ,千万不要pull ,直接执行git push -f ,强制推送最新的newBranch分支到master上
git push origin newBranch -f  # 强推的时候建议再次指定远程仓库 origin newBranch 防止覆盖的master

(newBranch) git checkout master # 切换为master
(master) git merge newBranch # 把最新调整的newBranch 同步回master

#merge 冲突会产生下面标签,导致不能提交 
#<<<<<<< HEAD
#add a 
#=======
#add b 
#>>>>>>> newBranch2
#需要手动修复 删除多余标签
#修复后,文件在工作区,这时候需要正常的 add 和 commit才能同步到版本库



# cherry-pick
# 合并指定的历史记录到当前分支 
git checkout Feature 
git cherry-pick f 
# 回滚到之前
git cherry-pick --abort
# 退出 Cherry pick,但是不回到操作前的样子
git cherry-pick --quit
 
  a - b - c - d   Feature
         \
           e - f - g Feature1 
# 变成
   a - b - c - d - f   Feature
         \
           e - f - g Feature1 
#cherry冲突 
git add . # 先保存文件
git cherry-pick --conntiue # 继续执行

# 最后还是记得要 -f 强推 
git push origin Feature -f  # 强推的时候建议再次指定远程仓库 origin Feature 防止覆盖的master

5.标签 tag

# tag : 里程碑意义  指向某个commit的指针,tag也是独立的信息,需要单独push到远程

#最后一次commit 添加一个标签,-a会自动添加作者和日期
git tag -a v1.0
#指定的commit 添加tag
git tag v1.0  4ab025
#查看所有tag
git tag
#查看与某个标签之间的差异
git show v1.0
#删除tag
git tag -d v.1.0

#tag 同步推送到远程
git push origin --tags
#tag 远程获取
git fetch origin tag V1.2

6.远程库 remote

#添加远程库关联
git remote add origin git@github.com:mjsong07/hello-git.git
#查看当前远程的状态
git remote -v
#本地库推送到远程
git push #等价于 git push origin master
#拉取代码
git fetch 
#拉取代码+ merge
git pull

#>>>>  	fork   >>>>
# fork 相当于复制了原作者一份代码->到自己的git上
# 本地新加⼀个新的远程库upstream
git remote add upstream https://github.com/vuejs/vue-next
#拉取代码 从作者库
git fetch upstream 
#拉取代码 从作者库 + 合并
git pull upstream 
#合并最新代码
git checkout master 
#合并最新的原作者库 到自己的项目里
git merge upstream/master

git 上游仓库upstream 和 自己远程库origin 关系

image.png

其他

# 强制修改最新的commit的信息 
git commit --amend -m "新的 commit message"