Git常用命令及操作

Git配置

  • Git全局配置

    1
    2
    3
    4
    git config --global user.name "frankieluo"
    git config --global user.email "frankieluo@sina.com"
    git config --global credential.helper store
    git config --global push.default simple

Git基本命令

  • Clone项目

    $ git clone http://gitlab.3bayslife.com:7777/app/alttest.git

  • Status

    $ git status

  • Pull合并项目

    $ git pull

  • Add新的或者修改后的文件

    git add .

  • 提交到本地仓库

    git commit -m "Initial commit"

  • Push到服务器

    git push -u origin master

Git repository 基本操作

  • Create a new repository

    1
    2
    3
    4
    5
    6
    git clone http://gitlab.3bayslife.com:7777/app/alttest.git
    cd alttest
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
  • Existing folder

    1
    2
    3
    4
    5
    6
    cd existing_folder
    git init
    git remote add origin http://gitlab.3bayslife.com:7777/app/alttest.git
    git add .
    git commit -m "Initial commit"
    git push -u origin master
  • Existing Git repository

    1
    2
    3
    4
    5
    cd existing_repo
    git remote rename origin old-origin
    git remote add origin http://gitlab.3bayslife.com:7777/app/alttest.git
    git push -u origin --all
    git push -u origin --tags

Git进阶操作

  • 查看远程分支

    $ git branch -a

  • 切换分支

    $ git checkout -b dev origin/dev

    切换回master分支

    $ git checkout master

    再次切回dev分支

    $ git checkout dev

    切换commit分支

    $ git checkout 988d45bae9ceefc9a31b5aadc701f6f115ecd895

  • 服务器代码合并本地代码

    1
    2
    3
    $ git stash     //暂存当前正在进行的工作。
    $ git pull origin master //拉取服务器的代码
    $ git stash pop //合并暂存的代码
  • 回滚本地代码

    $ git reset --hard  //回滚到上一个版本

    $ git checkout -- .

    $ git checkout -- readme.md

  • git rm 与 git rm –cached 的区别
    git rm : 同时从工作区和索引中删除文件。即本地的文件也被删除了。
    git rm –cached : 从索引中删除文件。但是本地文件还存在, 只是不希望这个文件被版本控制。

  • zsh反应慢
    You can add this to your git config and zsh won’t check the status anymore

    git config --add oh-my-zsh.hide-status 1

    git config --add oh-my-zsh.hide-dirty 1

  • git删除未跟踪文件

    删除 untracked files:

    git clean -f

    连 untracked 的目录也一起删掉:

    git clean -fd

    连 gitignore 的untrack 文件/目录也一起删掉 (慎用,一般这个是用来删掉编译出来的 .o之类的文件用的):

    git clean -xfd

    在用上述 git clean 前,墙裂建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删:

    1
    2
    3
    git clean -nxfd
    git clean -nf
    git clean -nfd

git创建新分支并提交到远程

  1. 创建一个本地分支
    git checkout -b 名称

  2. 查看现在的分支状态
    git branch (星号代表当前的分支)

  3. 把新建的分支push到远程,相当于创建一个远程分支
    git push origin 名称:名称

  4. 删除远程分支

    1
    2
    3
    git push origin  :名称
    或者
    git push origin --delete 名称
  5. 删除本地分支(不能在此分支上)
    git branch -d 名称

git log操作

  • How to have git log show filenames like svn log -v:

    For full path names of changed files:

    git log --name-only

    or

    git log --name-only --oneline

  • 查看日期范围内的记录信息

    1
    2
    3
    4
    5
    6
    7
    git log --after="2019-3-31" --before="2019-5-1"

    git log --author="frankieluo" --after="2019-7-30" --before="2019-9-1" > ../usefull/work_JFF2_Aug.log

    git log --author="frankieluo" --after="2019-7-30" --before="2019-9-1" > ../release/work_hardware_test_Aug.log

    git log --author="frankieluo" --after="2019-7-30" --before="2019-9-1" > ../release/alt_test_Aug.log
  • For full path names and status of changed files:
    git log --name-status

  • For abbreviated pathnames and a diffstat of changed files:
    git log --stat

    There’s a lot more options, check out the docs https://git-scm.com/docs/git-log .

  • From the git-diff manpage:

    git log -- vendor/nxp-opensource/uboot-imx/drivers/usb/gadget/fastboot_lock_unlock.h

  • 使用git log查看单个文件改动记录

    git log -- device/fsl/ax2130/overlay/frameworks/base/core/res/res/values/config.xml

    使用git show命令查看commit hashID的详细情况

    git show b61f128ac5015a8c2b14b28c67837ebf1924a5e1

git format-patch

  • 某一笔提交的patch

    git format-patch b61f128ac5015a8c2b14b28c67837ebf1924a5e1 -1

  • 某笔提交(包含)之前的几次提交

    git format-patch commitID -n
    git format-patch b61f128ac5015a8c2b14b28c67837ebf1924a5e1 -2

  • 某两笔(包后不包前)提交之间的所有提交

    git format-patch 1ac240c4561fab6ce400943edb38e46c792b949a..255867fabcf79246d948fadfebe307f8a848bff4

  • 某笔提交(不包含)以后所有提交

    git format-patch 1ac240c4561fab6ce400943edb38e46c792b949a

git merge 解决合并的冲突

把issue2分支和issue3分支的修改合并到master。

  • 切换master分支后,与issue2分支合并。
1
2
3
4
5
6
7
$ git checkout master
Switched to branch 'master'
$ git merge issue2
Updating b2b23c4..8f7aa27
Fast-forward
myfile.txt | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
  • 接着合并issue3分支。
1
2
3
4
$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
  • 接着合并issue3分支。
1
2
3
4
$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
  • 在发生冲突的地方,Git生成了内容的差异。请做以下修改:
1
2
3
add 把变更录入到索引中
commit 记录索引的状态
pull 取得远端数据库的内容
  • 修改冲突的部分,重新提交
1
2
3
4
$ git add myfile.txt
$ git commit -m "合并issue3分支"
# On branch master
nothing to commit (working directory clean)

Git 合并特定 commits 到另一个分支

git cherry-pick 1ac240

合并某个分支上的一系列commits

在一些特性情况下,合并单个 commit 并不够,你需要合并一系列相连的 commits 。这种情况下就不要选择 cherry-pick 了,rebase 更适合。还以上例为例,假设你需要合并 feature 分支的 commit 76cada ~ 62ecb3 到 dev 分支。

  • 首先需要基于 feature 创建一个新的分支,并指明新分支的最后一个 commit :

git checkout -b newbranch 62ecb3

  • 然后,rebase 这个新分支的 commit 到 dev(–onto dev)。 76cada^ 指明你想从哪个特定的 commit 开始。

git rebase --onto dev 76cada^

得到的结果就是 feature 分支的 commit 76cada ~62ecb3 都被合并到了 dev 分支。

git 删除文件后如何恢复

1
2
3
4
5
6
7
[xxx@xxx static_files]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: abbr_data
#
  • git reset HEAD abbr_data
  • git checkout -- abbr_data

git diff [–options] [–] […]

  • For instance, to see the difference for a file “main.c” between now and two commits back, here are three equivalent commands:

    1
    2
    3
    $ git diff HEAD^^ HEAD main.c
    $ git diff HEAD^^..HEAD -- main.c
    $ git diff HEAD~2 HEAD -- main.c
  • 某两笔(包前包后)提交之间的所有提交

    git diff commitID1 commitID2 > patch.diff

git apply

  • 检查patch/diff是否能正常打入

    git apply –check file.patch文件
    git apply –check file.diff文件

  • 打入patch/diff

    git apply file.patch文件
    git apply file.diff文件

  • 解决冲突

如果在合入patch的过程中报错了,可以使用下面命令:

git apply –reject file.patch

这个命令会自动合入不冲突的代码,然后保留冲突的部分,同时会生成后缀为.rej的文件,用于保存没有合并进去的部分,可以参考这个进行冲突解决。

解决完冲突后,删除后缀为.rej文件,并执行git add . 添加改动到暂存区

最后执行git am –resolved或者git am –continue

git tag

  • 查看本地分支标签

git tag -l

  • 查看远程所有标签

git ls-remote --tags

  • 给当前分支打标签

git tag v1.1.0

  • 给特定的某个commit版本打标签,比如现在某次提交的id为 039bf8b
1
2
3
4
5
6
7
8
9
git tag v1.0.0 039bf8b

或者可以添加注释

git tag v1.0.0 -m "add tags information" 039bf8b

或者

git tag v1.0.0 039bf8b -m "add tags information"
  • 删除本地某个标签
1
2
3
4
5
6
7
8
9
git tag --delete v1.0.0

或者

git tag -d v1.0.0

或者

git tag --d v1.0.0
  • 删除远程的某个标签
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git push -d origin v1.0.0

或者

git push --delete origin v1.0.0

或者

git push origin -d v1.0.0

或者

git push origin --delete v1.0.0

或者

git push origin :v1.0.0
  • 将本地标签一次性推送到远程
1
2
3
4
5
6
7
8
9
10
11
12
13
git push origin --tags

或者

git push origin --tag

或者

git push --tags

或者

git push --tag
  • 将本地某个特定标签推送到远程

git push origin v1.0.0

  • 查看某一个标签的提交信息

git show v1.0.0

  • 根据某个commit创建本地分支

例如: 当前分支的某个commit id = 12345678,我们可以基于这个id创建本地分支
git checkout 12345678 -b newBranch

git submodules

  • With version 2.13 of Git and later, –recurse-submodules can be used instead of –recursive:
    git clone --recurse-submodules -j8 git://github.com/foo/bar.git

  • With version 1.9 of Git up until version 2.12 (-j flag only available in version 2.8+):
    git clone --recursive -j8 git://github.com/foo/bar.git

  • With version 1.6.5 of Git and later, you can use:
    git clone --recursive git://github.com/foo/bar.git

  • For already cloned repos, or older Git versions, use:

    1
    2
    3
    git clone git://github.com/foo/bar.git
    cd bar
    git submodule update --init --recursive

git配置代理

  • 添加代理,后面端口号去系统设置 -> 网络和Internet -> 代理
    git config --global https.proxy http://127.0.0.1:10809

  • 去除代理
    git config --global https.proxy ""

  • 查看配置信息

1
2
3
4
5
6
7
git config --global  --list
core.editor="C:\Program Files\Sublime Text 3\subl.exe" -w
user.name=frankieluo
user.email=frankieluo@sina.com
http.sslverify=false
https.sslverify=false
https.proxy=

git进阶用法

  • git status -s | awk '{if ($1 == "M") {print $2} }' | xargs git rm --cached

git常见错误

  • 错误 -fatal: remote origin already exists.

    1、先删除远程 Git 仓库

    $ git remote rm origin

    2、再添加远程 Git 仓库

    $ git remote add origin http://act4.myqnapcloud.com:10080/app/cappec-weather-ios.git

  • 错误 Git refusing to merge unrelated histories

    git pull origin master --allow-unrelated-histories