Git是目前最流行的版本管理系统,github.com也是当前最大的交友论坛,学习并使用Git已经是程序员的基本要求。

安装

命令行版本

1
2
brew install git
brew install tig

图形界面
SourceTree https://www.sourcetreeapp.com/
Github Desktop https://desktop.github.com/

配置使用

修改~/.gitconfig文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[user]
name = ubuntu
email = ubuntu@utunbu.com
signingkey = your-sign-key
[alias]
co = checkout
br = branch
ci = commit -S -m
st = status
last = log -1 HEAD
un = update-index --assume-unchanged
noun= update-index --no-assume-unchanged
ignored = !git ls-files -v | grep "^[[:lower:]]"
[credential]
helper = store
[commit]
template = /ubuntu/.stCommitMsg
gpgsign = true
[url "https://github.com/"]
insteadOf = ssh://git@github.com/

修改~/.gitignore_global

1
2
3
4
5
*~
*.swp
*.swo
*.swn
.DS_Store

常用命令

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
git clone https://git.ubuntu.com/ubuntu.git
git st
git ci -a -m'comment message'

# reset local change, HARD
git reset --hard

# amend into last commit
git commit --amend

# merge some-br into current branch, squash
git merge --squash some-br

# restore file
git restore path/to/some/file

# checkout remote branch
git fetch origin/test
git checkout origin/test
git checkout -b test origin/test

# stash
git stash
git stash pop

# git url redirect
git config --global url."https://$(user):$(token)@github.com/username".insteadOf "https://github.com/username"
git config --global url."https://api:xxx@github.com/".insteadOf "git@github.com:"

rebase

1
git rebase -i HEAD~n

进本交互编辑界面,提示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

修改后边的两个picksquash,可以将这三个commit合为一个提交:

1
2
3
4
5
6
7
8
9
10
11
12
13
pick f7f3f6d changed my name a bit
squash 310154e updated README formatting and added blame
squash a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

netrc auto login

*** AS YOU MAY KNOWN, IT’S VERY DANGEROUS ***

1
2
3
4
5
6
7
8
9
touch ~/.netrc

machine github.com
login github-user-name
password user-token

machine gitlab.com
login gitlab-user-name
password gitlab-user-token

GPG Sign

Download GPG Suite: https://gpgtools.org/

  1. Open GPG Keychain
  2. Click New to and follow instrctions to generate a new GPG key
  3. Select the key you want to export and click Export
  4. Copy the content of exported public key, add to Github or Gitlab, if you prefer to use terminal: gpg --armor --export pub <KEY ID>
  5. Config Git to use your sign key id
    • execute gpg --list-secret-keys --keyid-format=long
    • copy sign key id from sec ed20000/this-is-you-sign-key 2099-01-01 [SC]
    • execute git config --global user.signingkey <KEY ID>
    • force git to sign all commit with git config --global commit.gpgsign true

With Sourcetree GUI:

  • open preferences, under Advenced set GPG Program, with GPG Suite it’s /usr/local/MacGPG2/bin
  • for each repository, open Repository -> Repository Setting, under Security section, enable GPG sign and select the right sign-key

Git托管

参考