跳转至

版本控制 Git 使用笔记

克隆与切换

使用 Git 创建以及切换各种分支。

clone 默认分支仓库

如果您还没有任何代码, 可以通过命令行工具创建一个全新的 Git 仓库并初始化到本项目仓库中。

git clone https://e.coding.net/shumlab/webstack/nav.bioitee.src.git
cd nav.bioitee.src
echo "# nav.bioitee.src" >> README.md
git add README.md
git commit -m "first commit"
git push -u origin master

clone 指定分支仓库

通过 -b 指定对应分支:

git clone -b release_22.05 https://github.com/galaxyproject/galaxy.git

使用命令行推送已存在的仓库

如果您已有一个 Git 仓库, 可以通过命令行工具将其直接推送到本仓库中。

git remote add origin https://e.coding.net/shumlab/webstack/nav.bioitee.src.git
git push -u origin master

本地创建分支

$ git branch v-1.2
$ git branch
* master
  v-1.2

切换本地分支

$ git branch
* master
  v-1.2

$ git checkout v-1.2
Switched to branch 'v-1.2'

$ git branch
  master
* v-1.2

把本地分支提交到远程分支仓库

$ git add --all

$ git commit -m "ASO design for Galaxy version 1.2"

$ git push origin v-1.2
Username for 'https://git.dev.tencent.com': shenweiyan
Password for 'https://shenweiyan@git.dev.tencent.com':
Counting objects: 19, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (18/18), 29.87 KiB | 0 bytes/s, done.
Total 18 (delta 3), reused 0 (delta 0)
To https://git.dev.tencent.com/shenweiyan/aso_design.git
 * [new branch]      v-1.2 -> v-1.2

查看一下远程仓库有几个分支

$ git branch -a
  master
* v-1.2
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/v-1.2

 $ git remote show origin

克隆用户提交的 PR

参考:GitHub clone from pull request? - stackoverflow

git clone https://github.com/shenweiyan/xxxxx.git
cd xxxxx
git fetch origin pull/15/head:pull_15
git checkout pull_15

删除

各种分支、tag 删除处理的一些备忘。

删除本地分支

$ git branch -D v-1.2

删除远程分支和 tag

在 Git v1.7.0  之后,可以使用这种语法删除远程分支(前提是删除的 不是默认的分支):

git push origin --delete <branchName>

删除 tag 这么用:

git push origin --delete tag <tagname>

否则,可以使用这种语法,推送一个空分支到远程分支,其实就相当于删除远程分支:

git push origin :<branchName>

这是删除 tag 的方法,推送一个空 tag 到远程 tag:

git tag -d <tagname>
git push origin :refs/tags/<tagname>

两种语法作用完全相同。

删除 GitHub 的分支

You can have head branches automatically deleted after pull requests are merged in your repository. For more information, see “Managing the automatic deletion of branches.”

  1. On GitHub, navigate to the main page of the repository.
  2. Above the list of files, click  NUMBER branches.

  1. Scroll to the branch that you want to delete, then click delete icon.

参考:https://help.github.com/en/articles/creating-and-deleting-branches-within-your-repository

删除在本地有但在远程库中已经不存在的分支

可以查看远程库的一些信息,及与本地分支的信息。有时候可能遇到如下情况:

$ git remote show origin
* remote origin
  Fetch URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git
  Push  URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git
  HEAD branch: dev
  Remote branches:
    dev                               tracked
    master                            tracked
    refs/remotes/origin/release_17.09 stale (use 'git remote prune' to remove)
    refs/remotes/origin/release_18.05 stale (use 'git remote prune' to remove)
    release_13.01                     tracked
    release_13.02                     tracked
    release_13.04                     tracked

其中 release_17.09, release_18.05 两个分支在远程库已经不存在了(你之前从远程库拉取过,可能之后被其他人删除了,你用 git branch -a  也是不能看出它们是否已被删除的),这时候我们可以删除本地库中这些相比较远程库中已经不存在的分支:

$ git remote prune origin
Pruning origin
URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git
 * [pruned] origin/release_17.09
 * [pruned] origin/release_18.05

回滚

本地回滚到之前某一次的 commit

$ git log
commit 610d4417c04ce2c53cfb7b77a0525ddb7695b869
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Fri Aug 23 09:08:06 2019 +0800

    fix ipad

commit 8938003aa309ab3cd98af43e67ad4f4aaff5e672
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Fri Aug 23 09:00:22 2019 +0800

    add new post at 2019-08-23-09:00:22

commit 31107425b1488b49d7a1a75f183149e40ba81223
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Thu Aug 22 15:50:56 2019 +0800

    add topic page urls

commit bbf9d6803864dffd7e7965036a638b17be0eeda7
Author: shenweiyan <ishenweiyan@qq.com>
Date:   Thu Aug 22 15:47:02 2019 +0800

    add about page images
......

$ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672
HEAD is now at 8938003 add new post at 2019-08-23-09:00:22

远程回滚到之前某一次的 commit

# 方法一,先 git reset 回滚到本地,然后再强制 push 到远程。
$ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672

# 其中,-f:force(谨慎操作,备份预备)
$ git push -u origin master -f

同步 GitHub fork 后新增加的分支

参考:https://segmentfault.com/q/1010000004228020

子模块

如果一个程序依赖于另一个程序,并且需要同时开发的时候,就可以使用 submodule。Git 里面的 submodule,本质是上就是在自己的 repository 里面存放指向另一个 repository 的某个 commit 的引用。所以如果 repository 是多用户共享的,要保证每个 submodule 所对应的 repository 每个用户都能访问到。

克隆一个包含子模块的项目

  • 正常克隆
git clone https://github.com/user/project.git

这个时候进入 clone 出来的目录,submodule 的子目录下面是空的,默认不会把 submodule 一同 checkout 出来。

  • 接着,在 project 的根目录需要执行两条命令:
git submodule init
git submodule update

这个时候 submodule 才会正常 Checkout 出来。我们也可以使用上面两条命令的合并版本:

git submodule update --init --recursive

在已有的仓库中克隆子模块

直接使用 git 的 submodule 命令:

git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git

添加完 submodule,就可以在对应的目录下面找到 checkout 出来的文件。同时也会添加一个 .gitmodules 文件在当前 repository 的根目录里。

[submodule "themes/ICS-Hugo-Theme"]
        path = themes/ICS-Hugo-Theme
        url = https://github.com/shenweiyan/ICS-Hugo-Theme.git

然后,在父仓库中执行 commit,push 就可以了。

一些 submodule 操作记录。

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:29:13 /home/shenweiyan/HugoSource
$ git add --all
warning: adding embedded git repository: themes/ICS-Hugo-Theme
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:   git submodule add <url> themes/ICS-Hugo-Theme
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:   git rm --cached themes/ICS-Hugo-Theme
hint:
hint: See "git help submodule" for more information.

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:29:13 /home/shenweiyan/HugoSource
$ git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git themes/ICS-Hugo-Theme
'themes/ICS-Hugo-Theme' already exists in the index

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:07 /home/shenweiyan/HugoSource
$ git rm --cached themes/ICS-Hugo-Theme
error: the following file has staged content different from both the
file and the HEAD:
    themes/ICS-Hugo-Theme
(use -f to force removal)

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:18 /home/shenweiyan/HugoSource
$ rm -rf themes/

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:31 /home/shenweiyan/HugoSource
$ git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git themes/ICS-Hugo-Theme
'themes/ICS-Hugo-Theme' already exists in the index

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:34 /home/shenweiyan/HugoSource
$ ls
config.toml  content  resources

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:40 /home/shenweiyan/HugoSource
$ git rm --cached themes/ICS-Hugo-Theme -f
rm 'themes/ICS-Hugo-Theme'

shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:40 /home/shenweiyan/HugoSource
$ git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git themes/ICS-Hugo-Theme
Cloning into '/data/HugoSource/themes/ICS-Hugo-Theme'...
remote: Enumerating objects: 8344, done.
remote: Counting objects: 100% (572/572), done.
remote: Compressing objects: 100% (292/292), done.
remote: Total 8344 (delta 307), reused 396 (delta 177), pack-reused 7772
Receiving objects: 100% (8344/8344), 9.45 MiB | 7.62 MiB/s, done.
Resolving deltas: 100% (4358/4358), done.

更新子模块

我们引入了别人的仓库之后,如果该仓库作者进行了更新,我们需要手动进行更新。即进入子模块后,执行:

$ git pull
You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

$ git pull https://github.com/shenweiyan/ICS-Hugo-Theme.git
From https://github.com/shenweiyan/ICS-Hugo-Theme
 * branch            HEAD       -> FETCH_HEAD
Updating 3501a4d..3036221
Fast-forward
 README.md        | 2 +-
 layouts/404.html | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

进行更新。

submodule 指定对应分支

通过 -b 指定对应分支:

git submodule add -b dev [URL to Git repo]