Git 操作小结

Git alias

修改~/.gitconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[color]
ui = true
[core]
editor = vim
excludesfile = ~/.gitignore
deltaBaseCacheLimit = 1G
[alias]
co = checkout
ci = commit
ca = commit --amend
st = status
fr = !git fetch && git rebase
sp = !git stash && git pull && git stash pop
br = branch
dc = diff --cached
sst = status -s
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
chist = log --oneline --graph --decorate --branches
review = push origin HEAD:refs/for/master
[push]
default = simple

新建分支

wip Work in Progress

1
git co -b wip-xxx-xx

切换分支

查看有哪些分支

git branch -a

1
2
3
4
5
6
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/nnvm
remotes/origin/piiswrong-patch-1
remotes/origin/v0.9rc1

git checkout -b 本地分支名 origin/远程分支名 (推荐使用):原因:可以直接跳转到分支

1
2
3
git checkout -b v0.9rc1 origin/v0.9rc1
Branch v0.9rc1 set up to track remote branch v0.9rc1 from origin.
Switched to a new branch 'v0.9rc1'

#已经切换到v0.9rc1分支了

Push代码

1
2
git ci -m "some word"
git push -u origin wip-xxx-xx

推送本地新分支

先确保当前已在local_branch分支,然后push

1
2
3
git checkout local_branch

git push origin local_branch:remote_branch

合并多个commits

设置起点

  • 以master 为起点
    当然 origin/master 也可以改为 对应起点的commit hash值
    1
    git rebase -i origin/master
  • 从HEAD版本开始往过去数3个版本
1
git rebase -i HEAD~3
  • 指名要合并的版本之前的版本号
    1
    git rebase -i 3a4226b
    3a4226b 不会参与合并

选择要合并的提交

执行rebase之后,会弹出一个窗口

1
2
3
pick 3ca6ec3   '注释**********'
pick 1b40566 '注释*********'
pick 53f244a '注释**********'

一共有三种状态

  • pick
    保留commit状态,不合并该次提交

  • s | squash

    use commit, but meld into previous commit
    使用此次Commit,但会被合并到前一个Commit

  • f | fixup

    like “squash”, but discard this commit’s log message
    跟squash一样,唯一的不同时丢弃掉Commit message。
    就是 git commit -m "Commit message" ,里边说的话丢弃掉。

改成这个样子

1
2
3
pick e7ba81d Commit-1
s 5756e15 Commit-2
s b1b8189 Commit-3

修改完成后 :wq 或者 shift + z + z 退出,进入下一个界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# This is a combination of 3 commits.
# The first commit's message is:
Commit-1

# This is the 2nd commit message:

Commit-2

# This is the 3rd commit message:

Commit-3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue Jan 5 23:27:22 2016 +0800
#
# rebase in progress; onto 5d39ff2
# You are currently editing a commit while rebasing branch 'master' on '5d39ff2'.
#
# Changes to be committed:
# modified: a

这里是一个编写 Commit Message 的界面, 带 # 的行会被忽略掉,其余的行就会作为新的Commit Message。将这个页面略作修改为:

1
2
3
4
5
6
7
8
9
10
11
12
Commit-1

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Tue Jan 5 23:27:22 2016 +0800
#
# rebase in progress; onto 5d39ff2
# You are currently editing a commit while rebasing branch 'master' on '5d39ff2'.
#
# Changes to be committed:
# modified: a

保存之后,查看log:
1
2
* 2d7b687 - (HEAD -> master) Commit-1
* 5d39ff2 - Commit-0

最后使用 git push -f 提交

命令行指令

Git 全局设置

1
2
git config --global user.name "mmmwhy"
git config --global user.email "mmmwhy@qq.com"

创建新版本库

1
2
3
4
5
6
git clone https://iii.run/mmmwhy/ssql.git
cd seq-annotation
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

已存在的文件夹或 Git 仓库

1
2
3
4
5
6
cd existing_folder
git init
git remote add origin https://iii.run/mmmwhy/sql.git
git add .
git commit -m "first commit"
git push -u origin master

添加远程

git remote add 命令使用两个参数:

远程命令,如 origin
远程 URL,如 https://github.com/user/repo.git
例如:

1
2
3
4
5
6
7
8
9
$ git remote add iii https://github.com/iii/repo.git
# Set a new remote

$ git remote -v
# Verify new remote
> origin https://github.com/user/repo.git (fetch)
> origin https://github.com/user/repo.git (push)
> iii https://github.com/iii/repo.git (fetch)
> iii https://github.com/iii/repo.git (push)

拉取远程分支

1
git fetch iii 

这样就可以看到远程分支的内容了