0
点赞
收藏
分享

微信扫一扫

产品前端架构——版本管理 [网易前端笔记]

初始化开发环境


安装git后需要做的第一件事情

git config --global user.name 'exmaple: Github Account'// 配置用户名
git config --global user.email 'example: GitHub Email Address' //配置用户email

使用github

  1. 生成ssh key
# Step 1. 
在终端中输入 ssh-keygen -t rsa -C "YOUR EMAIL"
一直按回车
# Step 2.
cd ~/.ssh
cat id_rsa.pub
拷贝其中内容
  • 将上一步得到的内容设置到github中

使用别名设置自定义log

命令格式:git config alias.shorename <fullcommand>

//Example. 配置格式化的log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

git 基本操作(常用命令)


查看git帮助文档

git  help <command>
git <command> -h
git <command>  --help

git config 配置git

  1. 用户配置
git config --global user.name "Demo User"
git config --global user.email "example@test.com"

这些配置会在提交中被展现


git init 初始化仓库

git init [path]
git init [path] --bare

初始化后将会在被初始化的目录下出现一个.git目录, 结构如下:

workdir
|__.git
   |——HEAD
   |——branches
   |——config
   |——description
   |——hooks
   |——info
   |——objects
   |__refs

git status

# git status 主要用于在下面3对关系之间找出他们的关系变化
- 未跟踪    <——> 跟踪
- 工作目录  <——> 暂存区
- 暂存区    <——> 最新提交

对文件状态的追踪

内容状态: 工作区,暂存区,提交区
文件跟踪状态:已跟踪,未跟踪


  1. touch newFile, 将在工作目录创建一个newFile的文件,此时文件处于未跟踪状态
  • git add newFile or git add .,文件被添加到暂存区,同时文件被跟踪

.gitignore 忽略文件

  • 在添加时忽略匹配文件
  • 仅作用于未追踪文件
    相当于在使用git add命令时,将.gitignore文件中所列的文件类型都进行了删除

git-rm 从暂存区删除文件

git rm --cached // 仅从暂存区删除

git diff 显示不同版本之间的差异

  • 显示工作目录与暂存区之间的差异, 命令:git diff
  • 暂存区与某次提交差异, 默认为HEAD,命令:git diff --cached [<reference>]
  • 工作目录与某次提交的差异,命令:git diff <reference>
  • 两次提交之间的差异, 命令: git diff <commitId1 commitId2>

撤销修改

  1. 将文件内容从暂存区复制到工作目录,命令: git checkout --<file>
  • 将文件内容从上次提交复制到暂存区,命令: git reset HEAD <file>

git分支操作


分支的增删查改 git branch

创建分支
git branch <branchName>
删除分支
git branch -d <branchName>
查看所有分支信息
git branch -v

git checkout 通过移动HEAD检出版本,可用于分支切换

git checkout <branchName> // 切换分支
git checkout -b <branchName> // 新建并切换到一个分支
git checkout -   // 恢复到上一个分支
git checkout <commit> // 切换到某次提交, head处于分离状态,此时避免做write操作

git reset 将当前分支回退到历史某个版本

git reset --mixed <commit> (默认) // 会将当前内容复制到暂存区
git reset --soft <commit> // 会暂存区和工作目录的状态,不会有任何变化
git reset --hard <commit> // 会将当前内容复制到暂存区和工作目录

使用捷径

  • A^ : A上的父提交
  • A~n: 在A之前的第n次提交

reset 与 checkout的区别(commit操作&file操作)

reset & checkout

命令 移动 范例(HEAD/branch) 说明
git reset 【commit】 git reset HEAD^ --soft 是/是 完全回退到某提交
git reset 【file】 git reset README.me 否/否 恢复暂存区到某提交状态
git checkout 【commit】 git checkout master 是/否 移动当前指针HEAD到某提交
git checkout 【file】 git checkout --README.md git checkout HEAD --xx.log 否/否 恢复工作目录到某状态

git stash

保存目前的工作目录和暂存区状态并返回到干净的工作空间

  1. 保存当前工作目录和暂存区的文件
git stash save 'push to stash area'
  • 查看暂存区内容
git stash list
// stash@{0}: On master: push to stash area
  • 取回暂存区内容到工作目录
git stash apply stash@{0}
  • 丢弃暂存区对应的stash
git stash drop stash@{0}
  • 取回暂存区栈顶一条内容到工作目录并丢弃暂存区栈顶内容
git stash pop stash@{0}
// stash pop = stash apply + stash drop

分支合并 git merge

  1. 解决merge冲突
  • 不要使用fast-forward
    穿入参数--no-ff
eg: git merge next --no-ff
  • git rebase
    修剪提交历史基线,俗称“变基”
eg.  git rebase master // 将会进行分支重演

git rebase --onto master commit


## git tag 对某个提交设置一个不变的别名

1. 为某次commit设置别名(tag)

git tag v1.0 commit
or
git tag -a 'v1.0' -m 'commit msg'
git push origin --tags


# git 远程操作

git 支持本地协议, 所以我们可以初始化一个本地的远程服务器

git init ~/git-server --bare

> bare 参数的意思是将当前仓库初始化为一个裸仓库,裸仓库没有工作目录
> 其文件结构为:
git-server
  |— HEAD
|—branches
|— config
|— description
|— hooks
|— info
|— objects
|— refs

## git push 用于提交本地历史到远程

## git remote 远程仓库的相关配置
1. 配置远程映射

git remote add origin ~/git-server // 添加远程仓库别名(origin 就是远程仓库git-server的别名,信息会放在.git/config文件下)
git remote -v // 查看远程仓库配置


## push 冲突
> 其他用户先于你push时, 会产生冲突

1. 使用git fetch+merge来解决这一问题

step 1

git fetch origin master

step 2

git merge origin/master

step 3

将这次提交推送到服务器

* 1中的3个步骤也可以使用
举报

相关推荐

0 条评论