git 使用分享

git 使用分享

  • git 命令行必备
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    # 设置 alias 别名
    git config --global alias.st "status"
    git config --global alias.ci "commit"
    git config --global alias.co "checkout"
    git config --global alias.br "branch"
    git config --global alias.ps "push"
    git config --global alias.pl "pull"
    git config --global alias.bra "branch -a"
    git config --global alias.cob "checkout -b"
    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 config --global user.name "your name"
    git config --global user.email "ericrao@welove-inc.com"

    # 查看 git 配置
    $ cat .gitconfig
    [alias]
    co = checkout
    ci = commit
    br = branch
    st = status
    [user]
    name = Your Name
    email = your@email.com
  • 配置 credential,避免每次都输入用户名和密码

    • git config --global credential.helper store <file path>
  • 忽略无用文件:

  • 常用命令 git mergegit fecth / pull 的区别
  • 巧用保存进度 git stashgit stash pop / apply
  • 每次发版需要打标签:
    • git tag 1.0.0 -m "tag message"
    • 查看 TAG: git tag --listgit show 1.0.0

问题合集

  • 问题 1:如何使用 submodule
    • 代码演示
  • 问题 2 :如何使用 subtree
    • 比 submodule 更建议使用,具体使用方法待更新
  • 问题3 :避免分支合并错误

To use this hook:

  • add the prepare-commit-msg file at .git/hooks/prepare-commit-msg and edit as needed
  • make it executable: chmod +x .git/hooks/prepare-commit-msg
  • disable fast-forward merges: git config branch.master.mergeoptions "--no-ff"
  • that’s it!

NOTE: after a failed merge from a forbidden branch, the working tree will still be in a MERGING state. To discard the local working copy state, run: git reset --merge

prepare-commit-msg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env ruby
# This git hook will prevent merging specific branches into master
# Put this file in your local repo, in the .git/hooks folder
# and make sure it is executable.
# The name of the file *must* be "prepare-commit-msg" for Git to pick it up.

FORBIDDEN_BRANCHES = ["test"]

def merge?
ARGV[1] == "merge"
end

def into_master?
current_branch = `git branch | grep '*' | sed 's/* //'`.chop
current_branch == "master"
end

def merge_msg
@msg ||= `cat .git/MERGE_MSG`
end

def from_branch
@from_branch = merge_msg.match(/Merge branch '(.*?)'/)[1]
end

def from_forbidden_branch?
FORBIDDEN_BRANCHES.include?(from_branch)
end

if merge? && into_master? && from_forbidden_branch?
out = `git reset --merge`
puts
puts " STOP THE PRESSES!"
puts " You are trying to merge #{from_branch} into the *master* branch."
puts " Surely you don't mean that?"
puts
puts " run the following command now to discard your working tree changes:"
puts
puts " git reset --merge"
puts
exit 1
end
  • 问题4 :

我本来打算把代码提交到branch1,但是我提交的时候忘了切过去, 提交到branch2了( 未推送或者已经推送 ), 这时候怎么回到我提交前的状态, 切个分支就可以再提交到branch1

​ 代码演示

  • iOS 项目分支出的问题