Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git: Tips #48

Open
xvno opened this issue Jul 5, 2019 · 16 comments
Open

Git: Tips #48

xvno opened this issue Jul 5, 2019 · 16 comments

Comments

@xvno
Copy link
Owner

xvno commented Jul 5, 2019

Refs

Tips

  • -- double-dash:

The double dash "--" means "end of command line flags" i.e. it tells the preceding command not to try to parse what comes after command line options.

Suppose there is a file called main (or master, it is a branch-ish name, easy to mixed with the git branch)
If it has been modified and then we wanna undo that.
Checkout this file from the last commit:

git checkout main # this will lead to checking out the branch of main
git checkout -f -- main # this will do

Also, another example: when -f is the stupid file name

git checkout -f  # Oops....
git checkout -- -f

change branch name and push to github

git branch -m master main
git fetch origin
git branch -u origin/main main

if other one changed branch name, also

git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
@xvno
Copy link
Owner Author

xvno commented Jul 5, 2019

粒度划分

细粒度

以"完成"以下任意一项

  1. 某一项功能 或 代码段
  2. 添加必要文件/更改目录结构 + 更新必要配置
  3. 修改所有需要改正的字段/话述

独立

  1. 每个提交不应该包含跟它的comment无关的变更, 即描述要覆盖所有变更(细粒度的变更更容易描述, 但<细粒度.3>要满足)
  2. 不依赖"第三方分支": merge/rebase操作, 不应对除master, release, dev(develop), raw 之外的第三方分支使用. 项目成员分支间的依赖不应该存在, 即在合并到raw(develop)上之前是不能相互merge/rebase的

commit/comment

[<jira-id>: ]<动作类型>: [<模块/文件/功能等逗号分隔>:]+ <简要说明>

动作类型: Add, Rmv, Mdf, Dbg, Opt
模块/文件/功能
简要说明

# 实例
# ": "是英文标点加空格
# 尽量使用原有的词汇, 不要直接翻译成中文

Add: login: logo文件
Mdf: login: footer 配色 和 文案
Rmv: user: 无用的大尺寸图片
SHARP-1008: Dbg: login, user: style: sass文件除虫
SHARP-1009: Opt: profile: 

@xvno
Copy link
Owner Author

xvno commented Jul 5, 2019

命令

  • init
  • add
  • commit
  • pull
  • push
  • rebase
  • merge
  • checkout
  • cherry-pick
  • reset
  • stash
  • remote
  • branch

@xvno
Copy link
Owner Author

xvno commented Jul 5, 2019

git init

此命令自不必说, 在本"目录"[pwd]初始化一个代码仓库(repo), 最好第一个提交仅仅包含.gitignore README等初始化文件.

e.g. 如果远程有一个空仓, 比如在github上创建了一个repo2, 要把新创建的本地仓库推送到远程仓库:

  • 就得设置remote, 即 git remote add <repo2-alias> <repo2-uri>
  • 并且还要设置up-stream, 即 git push -u <repo2-alias> <repo2-branch-name>

@xvno xvno changed the title 约定: Git: 工作流约定 Git: Tips & 工作流约定 Aug 6, 2021
@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

git add

该命令两种功能:

  1. 追踪新的文件
  2. 把新的变更放入暂存区

配合 git commit命令使用

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

git commit -m '<comment>'

该命令用于提交代码: 将暂存区中的新文件和新的变更提交到本地仓库中
-m 参数用于引导添加"提交说明(commit comment)" 已经在上文commit/comment中详细描述

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

拉取代码git pull, git pull --rebase

该命令用于从远程仓库拉取代码到本地, 默认当前分支, 还可以加参数git pull <repo2-alias> <repo2-branch-name>来拉取对应分支

git pull --rebase在拉取远程分支的时候还顺便处理rebase, 而非merge

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

推送代码 git push, git push <repo2-alias> <repo2-branch-name>

推送分支到远程服务器

  • 可以指定远程仓库名和分支 git push <repo2-alias> <repo2-branch-name>
  • 慎重! 慎重! 慎重! 可以加参数-f: 前提是这个一定不是master/release/raw/develop, 而是个人的分支或临时分支

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

合并 git merge

不多说了, 废柴操作~

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

git checkout

功能

  1. 切换当前分支为: git checkout <branch-name>, 另外可以用 -b 参数来检出制定提交为新分支git checkout <commit-id> -b <new-branch-name>
  2. 移动HEAD到对应的提交点 git checkout <commit-id>
  3. 用以还原文件: 从上一次的提交点里恢复文件, 覆盖当前的修改
git checkout -f <fileName>
git checkout -p <pathOfFolder>

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

git reset -- <file-names>

Reset-Demystified

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

遴选 git cherry-pick <commit id>

这个功能相当有意思, 能把某个指定的提交点作为补丁添加到当前分支的最前端.
使用场景很单调: 把合格的提交点摘取到重要的分支上.
e.g. 我把自己的3个提交在我自己的分支 xvno 上rebase-squash成了一个提交点 mixed, 切换到develop分支上后, 直接git cherry-pick <mixed: commit-id>

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

合并/摘选 指定提交点, 变基到指定分支/提交点 git rebase -i <commit id>, git rebase --continue

一般用于合并提交点
git的神器之一 rebase, 无出其右者, 加上-i之后, 就可以修改提交comment + 合并提交点 + 丢掉提交点...
添加--continue之后, 能让暂停的rebase过程继续

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

藏私房代码的地方 git stash push/pop/list/clear/apply

stash: 藏匿处, 暗格;

git stash 可以把已经 git add 的变更移动到暗格里. 应用场景:

  • A. 当我们正在做一个功能点的开发,
  • B. 突然接到一个紧急的新需求. 此时我们需要新开个分支来完成, 但当前代码还是比较弱智(即不满足粒度划分要求)而不能作为一个提交点,
  • C. 这种情况下可以先把当前的变更 git add . + git stash 然后处理新需求;
  • D. 当新需求已经处理完了, 我们需要回到 A点, 用 git stash list 查看曾经私藏了哪些代码, 并用 git stash pop [sn] 来取回, 或者用 git stash apply [sn]来复制一份代码到当前工作环境, 这样就可以继续中断的工作了,

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

远程仓库 git remote

做什么用?

  1. 本地仓库关联了哪些远程仓库, git remote -v
  2. 添加远程仓库, git remote add <repo-name> <repo-uri>

有了remote, 就可以光明正大的 git push <repo-name> <branch-name>

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

分支管理 branch

git branch

参数 r 表示 remote-tracking

  1. 检出新分支
  2. -avr, 查看分支
  3. -dDr, 删除分支

真正删除远程分支的方式 git push <repo-name> :<branch-name>

git checkout -b 就当前提交点新建一个分支

git checkout -b newbie

@xvno
Copy link
Owner Author

xvno commented Aug 6, 2021

实例演示

用1个实例演示日常工作流程

已经推送新建的本地仓库到github

  1. 调研需求, 并切换到目标分支, 拉取最新代码
  2. 从当前提交点检出新的分支, 并在该分支上开发
  3. 紧急任务来了, 我要优先处理
  4. 我是谁, 我从哪里来, 我要干什么?
  5. 我做完了, 得合并细小提交
  6. 我还得把开发结整合到master上, 并删掉当前分支
  7. todo列表上的下一个项目来了
  8. 这是一个循环

@xvno xvno changed the title Git: Tips & 工作流约定 Git: Tips Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant