GIT使用笔记

GIT使用笔记。有些命令tortoisegit无法胜任,则使用命令行。

一般性

github 使用用户名下载经常超时,使用 git 协议下载正常,此时形式如:git@github.com:latelee/Makefile_templet.git
在 jenkins 中指定仓库地址时,要给出后缀.git

分支管理的笔记

创建分支

1
2
在master上创建分支:
git checkout -b develop master

示意图:

image-20210701160735341

本地更新远程仓库所有分支

默认情况下,使用git clone下载的仓库,只有默认的分支(可能是master,可能是其它的,可由远程仓库设置)。在本地更新所有的远程仓库分支:

1
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

只拉取远程仓库所有分支:

1
2
3
git fetch origin   // 注:并未切换分支,如果仓库新建有分支,此操作会更新在本地。

git checkout -b 3-fixup-memory-leak origin/3-fixup-memory-leak

拉取指定分支:

1
2
3
4
git fetch origin dev  // dev为远程仓库分支
git fetch origin release-1.6.4.831-2021.8.31 // dev为远程仓库分支
git checkout -b release-1.6.4.831-2021.8.31 origin/release-1.6.4.831-2021.8.31

子模块

1
2
3
4
5
6
更新子模块(要执行这2句):
git submodule update --init
git submodule update --remote

git submodule update --init --recursive

合并多个提交记录

git bash中操作。
1、合并最新的N个提交。
示例:
git rebase -i HEAD~3
出现VI编辑器,输入i进入编辑模式。第一行的commit不可修改。其它的commit将pick改为s。输入:wq保存并退出,git进行压缩。接着进入另一个VI编辑器,将已有的提交日志删除,重新添加新的提交日志。输入:wq
此时最新的3个提交已被合并。日志为刚才添加的,而时间则是3个提交最早的那一次时间。

注意必须添加日志,否则无法完成操作。提示:

1
2
Aborting commit due to empty commit message.
Could not apply 9cf099d... 7777777777777777

其它:出现异常,使用git rebase --abort放弃操作。

强制重新提交新的仓库

相当于清空原仓库,而不删除。
删除 .git,再新建仓库

1
2
3
4
5
6
7
8
rm -rf .git
git init
git add .
git commit -m "first commit"

// 注:此过程可能需要添加用户信息,如
git config user.name "Late Lee"
git config user.email "li@latelee.org"

添加远程仓库并提交:

1
2
git remote add origin https://latelee@github.com/latelee/foobar.git
git push -u --force origin master

另一种提交方法(初始化同上,仅是提交时用一条命令,ssh方式):

1
git push --force --set-upstream git@github.com:latelee/foobar.git master:master

注:如果提交了不应该提交的内容,可以重新制作本地的 git 仓库,再用此法,则原地址未变,而内容改变,过程的版本不再看到。

标签tag

删除远程仓库tag

标签v1.0已打,但发现问题,重新改,删除该标签,在新的提交上打上v1.0(注:此场景不是另一个新版本的发布)。此时无法推送远程。可删除远程标签,再推送。

1
2
3
4
5
6
7
8
9
git tag -d v1.0
git push origin :v1.0
(注:冒号前有空格)

上传所有的标签:
git push --tags
或指定远程仓库:
git push origin --tags

问题及解决

git checkout时出现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")

Changes to be committed:

new file: archives/2020/12/index.html
new file: archives/2020/index.html
new file: archives/page/3/index.html
new file: page/5/index.html
new file: uncategorized/undefined/index.html

Unmerged paths:
(use "git add <file>..." to mark resolution)

both added: activity/website-update/1.jpg
both added: dl/apt-key.gpg
both added: image/3part/aliyun.jpg
both added: image/3part/docker.jpg
both added: image/3part/exmail.jpg

不需要保留本地修改情况下,执行下面两步:

1
2
git fetch origin
git reset --hard origin/master

再次查看:

1
2
3
4
git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

分支没有任何内容,但创建分支:

1
2
3
4
5
$ git checkout -b develop master
fatal: 'master' is not a commit and a branch 'develop' cannot be created from it

解决:
默认master先加README.md文件

远程仓库没有对应的分支,但本地有,提交方法:

1
git push --set-upstream origin branch-aaa

github的一些笔记

ssh:

先在主机生成ssh:

1
2
$ ssh-keygen.exe -t rsa -C "li@latelee.org"  # 邮箱为注册github的,一路回车
$ cat ~/.ssh/id_rsa.pub # 查看公钥

再在个人设置中添加公钥。再测试连通性:

1
2
$ ssh -T git@github.com
Hi latelee! You've successfully authenticated, but GitHub does not provide shell access.

使用仓库为ssh形式,如下:

1
git clone git@github.com:latelee/hello.git

gitlab的一些笔记

自建的gitlab,可能没有域名,用 IP 地址即可,可能加端口访问。支持 http 和 git,注意,git 方式不用加端口。git 方式和 github 设置类似。

1
2
3
git clone http://172.18.18.18:8888/latelee/git_test.git

git clone git@172.18.18.18:latelee/git_test.git

一说加端口的是这样:

1
git clone ssh://git@10.10.10.10:8022/group/project.git

参考:

http://www.ruanyifeng.com/blog/2012/07/git.html