git

《Pro Git》, git-scm


目录

  • git init
  • git add : 1.开始跟踪文件; 2.或者把已经跟踪的文件放到暂存区(stage); 3.merge时把冲突的文件标记为已经解决…
  • git status -s
  • git remote
  • git branch
  • git tag
  • git merge
  • git clone
  • git rm
  • git mv
  • git reset
  • git log & git show
  • git rebase
  • git remote update origin -p/–prune : 更新远程分支列表
  • git update-index –assume-unchanged FILE_NAME

0. git remote

git remote : Manage set of tracked repositories

一个本地仓库可以对应多个远程仓库, 例如可以同时对应Github和Gitlab上面的仓库

1
2
3
4
5
6
7
8
9
# 列出所有远程仓库, 可以不止一个
$ git remote -v
zhang_lab git@gitlab.com:zhang/Golang.git (fetch)
zhang_lab git@gitlab.com:zhang/Golang.git (push)
origin https://github.com/24Jay/Golang.git (fetch)
origin https://github.com/24Jay/Golang.git (push)

# add a remote named repoName for the repository at <url>
$ git remote add repoName git@github.com/24Jay/Golang.git


1. git branch

1.1 查询分支

1
2
3
$ git branch --all

$ git branch -l

1.2 创建本地分支

1
2
3
4
5
6
7
8
9
10
11
#创建
$ git branch newBranch

#切换
$ git checkout newBranch

#创建并且切换
$ git checkout -b newBranch

#创建远程分支的本地分支, 并且关联
$ git checkout -b localName origin/remoteName

1.3 创建远程分支

1
2
3
4
5
6
7
$ git push origin newBranch

# 把本地新建的分支推送到远程, 并且关联本地和远程的这两个分支
$ git push -u origin localBranch

#delete remote branch推送空分支到远程
$ git push origin :newBranch

1.4 分支比较

1
2
3
4
5
6
7
#比较当前分支和master分支的不同,只列出文件列表
$ git diff master --stat

$ git diff feature/branch master --stat

#比较某个文件在当前分支和master分支上有何不同
$ git diff master src/main/resources/application.properties

1.5 删除分支

1
2
3
4
5
6
7
8
9
10
11
12
13
#删除本地分支
$ git branch -d branchName
$ git branch --delete branchName

#强制删除
$ git branch -D branchName
$ git branch --delete --force branchName

#删除远程分支
#delete remote branch
$ git push --delete origin remoteBranch
$ git push -d origin remoteBranch
$ git push -D origin remoteBranch

1.6 本地分支和远程分支的关联

1
2
3
4
5
6
7
8
#查看本地分支对应的远程分支
$ git branch -va

#If you wish to set tracking information for this branch you can do so with:
#git branch --set-upstream-to=origin/<branch> feature/localbranch

# 或者使用
$ git push -u origin feature/localbranch

2. git tag

2.1 查询tag

1
2
3
4
5
6
7
8
#列出所有tag, git tag或者
$ git tag -l

#列出所有2开头的tag
$ git tag -l 2*

#查看以前的commit
$ git tag --oneline

2.2 创建tag

1
2
3
4
5
#创建轻量级tag
$ git tag version 1.0 或者 git tag 1.0

#创建带有注释的tag
$ git tag -a version1.0 -m "first version"

2.3 删除tag

1
2
#删除tag
$ git tag -d 1.0

2.4 共享tag

在执行git push的时候, 本地tag是不会上传到远程服务器的

1
$ git push origin --tags

2.5 获取origin的tag, 本地切换到对应版本

  1. 比如在某次开发的时候, 本地找不到tag 2.8.1对应的版本, 但是远程仓库是有的.

    1
    2
    3
    4
    5
    #本地查看如果找不到远程的tag, 可以使用git fetch获取
    $ git fetch

    # 然后使用git tag查看
    $ git tag -l 2.8.*
  2. 接下来在本地创建一个branch, 对于tag(tag可以认为是一个只读分支)

    1
    2
    #得到的分支tag_281便和远程的tag 2.8.1一样了
    $ git checkout -b tag_281 2.8.1
  3. 直接读取远程的tag分支到本地

    1
    $ git checkout tagName

3 git merge

3.1 比较两个分支

1
2
3
4
5
6
7
8
#本分支和其他分支对比
$ git diff otherBranch

#只列出不同的文件名
$ git diff otherBranch --stat

#本地分支和远程分支对比, 注意是origin/branch, 而不能是origin branch
$ git diff origin/branch --stat

3.1 把其他分支合并到本分支

1
$ git merge otherBranch

3.2 用其他分支某个文件替代本分支某个文件(解决冲突)

必须是在两个本地分支之间

1
$ git checkout otherBranch -- src/main/java/path/file.java

3.3 将其他分支上的某个文件(本分支没有)merge到本分支

1
2
3
$ git checkout -p otherFeatureName  file/path/for/copied/file 
.....
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]? y

3.4 放弃本次merge操作: git merge –abort

1
$ git merge --abort

4. git clone

4.1 clone指定分支或者tag

1
2
3
4
5
6
7
8
9
10
11
#获取指定分支
$ git clone -b aBranch https://github.com/.....

#获取指定tag
$ git clone -b aBranch https://github.com/...

#或者使用
$ git clone --branch ....

#clone到指定文件夹
$ git clone -b aBranch https://github.com/... newDirectory

5. git rm

5.1 取消跟踪某文件

1
2
3
4
5
# 取消跟踪但是不删除文件
$ git rm --cached fileName

# 取消跟踪并且删除文件
$ git rm -f fileName

6. git reset和revert

6.1 git log查看历史commits

1
2
3
4
5
$ git log
commit f8db65744552e26c430c1cbcf784848301b4acc3
...
commit b8efdfe53bd557896f6ed0e37672c52e169f4c1b
...

6.2 git reset

git reset有5种模式

  • git reset –mixed : 默认模式,只保留源码, 回退commit和index信息
  • git reset –soft : 只回退commit,不会恢复到index file一级
  • git reset –hard : 彻底回退, 本地代码也会修改
  • git reset –merge
  • git reset –kepp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#1. 提交代码时, 多add了某一个文件, 想回退取消:
$ git reset HEAD <file>...

#2. 回退所有内容到上一个版本, 本地源代码不变
$ git reset HEAD^

#3. 回退某个文件到上一个版本
$ git reset HEAD^ <file>

#4. 回退到某个commit
$ git reset b8efdfe53bd557896f6ed0e37672c52e169f4c1b

#5. 将本地状态回退到跟远程一样
$ git reset --hard origin/master

#6. 将本地回退后的同步到远程
$ git push -f

6.3 git revert

  • reset是让HEAD后退到之前的某个commit, 而revert是让HEAD前进,用一个新的commit删除某个原来的提交.
  • reset: 删除某个commit之后的所有commits.
  • revert: 仅删除某个commit, 不影响它前后的commits.
  • 因此公共分支上一般不能使用git reset, 推荐使用git revert.

7. git config

1
2
3
$ git config --list

$ git help config

8. git log & git show

查看历史提交记录

8.1 git log

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
#1. 查看历史提交
$ git log
commit 258efb2fa280a171278febbf53247bc46f4982ec
Author: zhang <zhang@xx.com>
Date: Wed Oct 31 17:39:40 2018 +0800

newhome rec/related service lgo

commit 29906de4c19a3fc4fcb5da1a6866f8f65d2f426c
Author: zhang <zhang@xx.com>
Date: Wed Oct 31 15:33:27 2018 +0800

add log for news

#2. 查看某个人的提交
$ git log --author="Jay" --stat

$ git log --name-only

$ git log --name-status

$ git log --graph

#不分页,直接显示所有提交
$ git --no-pager log

#找出某个工程my-project里面,zhansan和lisi修改过的所有文件
$ git --no-pager log --author="zhansan\|lisi" --name-only | grep 'my-project'

8.2 查看某个提交具体修改内容

1
$ git show 29906de4c19a3fc4fcb5da1a6866f8f65d2f426c --stat