anjia / blog

博客,积累与沉淀
106 stars 4 forks source link

Git #59

Open anjia opened 5 years ago

anjia commented 5 years ago

最常用

## 回滚
git log hello.tpl   # 日志
git diff xxx:hello.tpl yyy:hello.tpl  # 对比
git checkout xxx hello.tpl  # 回退到特定版本

## merge
git checkout master
git merge hotfix  # merge 完记得 commit, push

## 分支
git checkout -b home_v3  # 相当于 git branch home_v3 和 git checkout home_v3

git push -u origin home_v3  # push 到远程

git branch -d branch_name  # 删除本地
git branch -D branch_name  # 删除本地

git push origin -d home_v3  # 删除远程分支

## 暂存
git stash      # 暂存当前的工作进度
# ...
git stash pop  # 恢复之前的改动

打标签

git tag   # 列出已有的 tag,按字母顺序排列
git tag -l 'v1.*'   # 列出 v1.* 的 tags

git tag -a v1.0 -m 'my version 1.0'  # 新建 tag,含附注标签
git show v1.0   # 查看相应 tag 的版本信息,附带打 tag 时的提交

git push origin v1.0  # 将 tag v1.0 传到远端服务器上
git push origin --tags  # 将本地所有新增的标签一次推送

撤销 commit

git add .
git commit -m 'bugfix:修复重复了'

# 此时想撤销 commit

git reset --hard HEAD^    # 撤回commit
git reset --hard HEAD~1  # 同 HEAD^ 即撤销上一次commit
git reset --hard HEAD~2  # 撤销前2次commit

# --hard  删除工作空间改动代码,撤销 commit,撤销 git add . 
# --soft  不删除工作空间的改动代码,撤销 commit,不撤销 git add.
# --mixed 不删除工作空间改动代码,撤销 commit,也撤销 git add 

https://git-scm.com/

anjia commented 4 years ago

同步 fork

## 进入你的本地项目

# git remote add upstream xxx
# git remote -v
origin  git@xxx_fork.git (fetch)
origin  git@xxx_fork.git (push)
upstream    git@xxx_origin.git (fetch)
upstream    git@xxx_origin.git (push)

git fetch upstream  # 拉取原仓库的提交,会存在本地的 upstream/master 分支上
git checkout master # 切换到你本地的 master 分支上
git merge upstream/master  # 合并

git push origin master  # 提交到自己fork的远程

https://help.github.com/en/articles/syncing-a-fork

anjia commented 4 years ago

git flow

git-flow 用了脚本捆绑了一系列 Git 命令,来完成一些特定的工作流程。

init

初始化。建议用默认值即可。

# git flow init
Which branch should be used for bringing forth production releases?
   - develop
   - master
Branch name for production releases: [master]

Which branch should be used for integration of the "next release"?
   - develop
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Bugfix branches? [bugfix/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
Hooks and filters directory? [/Users/xxx/.git/hooks]

feature

开发新特性

git flow feature start MYFEATURE   # 开始开发,基于 develop 分支
git flow feature finish MYFEATURE  # 完成开发:合并到 develop、删除此分支、回到 develop

# 合作开发
git flow feature publish MYFEATURE     # 将新特性发布到远程
git flow feature track MYFEATURE   # 跟踪在origin上的特性分支
$ git flow feature help
usage: git flow feature [list]
   or: git flow feature start
   or: git flow feature finish
   or: git flow feature publish
   or: git flow feature track
   or: git flow feature diff
   or: git flow feature rebase
   or: git flow feature checkout
   or: git flow feature pull
   or: git flow feature delete

    Manage your feature branches.

    For more specific help type the command followed by --help

release

# develop 已测试完毕&待上线的代码,则执行:
git flow release start 1.1.5 # 基于 develop 分支。命名用版本号
git flow release publish 1.1.5  # 创建 release 之后,可发布。以备其它小伙伴提交代码
git flow release finish 1.1.5
# 1. pull, 确保最新
# 2. release 的内容会被 merged 到 master+develop 分支上
# 3. 打 tag,以 release 的名字命名
# 4. 清理:删除 release 分支,回到 develop 上

git push --tags

hotfix

git flow hotfix start missing-link  # 基于 master 分支
git flow hotfix finish missing-link
# 1. 改动会被 merge 到 master 和 develop 上(确保下次上线的时候,不会把bug再带回来)
# 2. 打 tag,默认是 hotfix 的名字
# 3. 清理:删除 hotfix 分支,切换到 develop 上

git push --tags

附录

命令一览

两个长期分支:master 和 develop 分支

2 git-flow-1-长期分支

三种临时分支:feature/xxx, release/xxx, hotfix/xxx git-flow 会在 finish 它们的时候,自动清理。且会执行其它相关操作,确保工作流程准确无误地推进 2 git-flow-2-临时分支

https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/git-flow https://danielkummer.github.io/git-flow-cheatsheet/index.zh_CN.html

anjia commented 4 years ago

.gitignore

.gitignore文件会递归用在整个工作树中。下面是它支持的 patterns

eg.

*.a     # 忽略所有的 .a 文件
!lib.a  # 但是跟踪 lib.a 文件

/TODO   # 忽略当前目录下的 TODO 文件,但是不包括 subdir/TODO
build/  # 忽略 build 文件夹里的所有文件

doc/*.txt     # 忽略 doc/*.txt,但是不忽略 doc/server/arch.txt
doc/**/*.pdf  # 忽略 doc 文件夹及其子文件夹下的所有 .pdf 文件

https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring

git rm

.gitignore 只会忽略那些原来没有被 track 的文件 如果你在 .gitignore 文件之前,就已经 push 到项目里了,那么即使在 .gitignore 里写入了新规则,这些规则也不会起作用。

解决的话,先把本地缓存删除,将文件改成未 track 状态,然后再提交:

git rm  # 是从索引中删除文件(不会删除工作目录中的文件)
git rm --cached filename.txt # 删除跟踪,但会保留在本地
git rm --f filename.txt      # 删除跟踪,并且删除本地

## 然后再提交(在 Git 中,删除也是一个修改操作)
git commit -m ''
zzz6519003 commented 4 years ago

和svn subv的进步呢?

anjia commented 4 years ago

git config

git config --global --list
git config --global --unset core.editor

ssh

$ ls ~/.ssh
id_rsa    id_rsa.pub    known_hosts
# id_rsa     你的私钥
# id_rsa.pub 你的公钥

$ ssh-keygen -t rsa -C "anjia@xxx.cn"
# -t rsa, RSA 是当前最新最安全的
# -C “”,  给秘钥的一种描述/标签

子模块

$ mkdir xxx 
$ cd xxx

## 添加子模块
$ git submodule add git@git.corp.qihoo.net:haosou-fe-onebox/midpages-cli.git
# 此时,在主仓库下会出现子模块的信息
# ls -a            会有 .gitmodules
# cat .git/config  会有 [submodule xxx] 的信息

## 克隆带子模块的项目
$ git clone  # 仅拉取项目本身
$ git clone xxx --recurse-submodules  # 会拉取本项目及其子仓库
$ git submodule update --init --recursive  # 再补子仓库