创建与切换
使用命令行创建仓库
如果您还没有任何代码, 可以通过命令行工具创建一个全新的 Git 仓库并初始化到本项目仓库中。
git clone https://e.coding.net/shumlab/webstack/nav.bioitee.src.gitcd nav.bioitee.srcecho "# nav.bioitee.src" >> README.mdgit add README.mdgit commit -m "first commit"git push -u origin master
使用命令行推送已存在的仓库
如果您已有一个 Git 仓库, 可以通过命令行工具将其直接推送到本仓库中。
git remote add origin https://e.coding.net/shumlab/webstack/nav.bioitee.src.gitgit push -u origin master
本地创建分支
$ git branch v-1.2$ git branch* masterv-1.2
切换本地分支
$ git branch* masterv-1.2$ git checkout v-1.2Switched to branch 'v-1.2'$ git branchmaster* v-1.2
把本地分支提交到远程分支仓库
$ git add --all$ git commit -m "ASO design for Galaxy version 1.2"$ git push origin v-1.2Username for 'https://git.dev.tencent.com': shenweiyanPassword 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 -amaster* v-1.2remotes/origin/HEAD -> origin/masterremotes/origin/masterremotes/origin/v-1.2$ git remote show origin
删除
删除本地分支
$ 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.”
- On GitHub, navigate to the main page of the repository.
- Above the list of files, click NUMBER branches.

- 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 originFetch URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.gitPush URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.gitHEAD branch: devRemote branches:dev trackedmaster trackedrefs/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 trackedrelease_13.02 trackedrelease_13.04 tracked
其中 release_17.09, release_18.05 两个分支在远程库已经不存在了(你之前从远程库拉取过,可能之后被其他人删除了,你用 git branch -a 也是不能看出它们是否已被删除的),这时候我们可以删除本地库中这些相比较远程库中已经不存在的分支:
$ git remote prune originPruning originURL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git* [pruned] origin/release_17.09* [pruned] origin/release_18.05
回滚
本地回滚到之前某一次的 commit
$ git logcommit 610d4417c04ce2c53cfb7b77a0525ddb7695b869Author: shenweiyan <ishenweiyan@qq.com>Date: Fri Aug 23 09:08:06 2019 +0800fix ipadcommit 8938003aa309ab3cd98af43e67ad4f4aaff5e672Author: shenweiyan <ishenweiyan@qq.com>Date: Fri Aug 23 09:00:22 2019 +0800add new post at 2019-08-23-09:00:22commit 31107425b1488b49d7a1a75f183149e40ba81223Author: shenweiyan <ishenweiyan@qq.com>Date: Thu Aug 22 15:50:56 2019 +0800add topic page urlscommit bbf9d6803864dffd7e7965036a638b17be0eeda7Author: shenweiyan <ishenweiyan@qq.com>Date: Thu Aug 22 15:47:02 2019 +0800add about page images......$ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672HEAD 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 initgit 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-Themeurl = https://github.com/shenweiyan/ICS-Hugo-Theme.git
然后,在父仓库中执行 commit,push 就可以了。
一些 submodule 操作记录。
shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:29:13 /home/shenweiyan/HugoSource$ git add --allwarning: adding embedded git repository: themes/ICS-Hugo-Themehint: You've added another git repository inside your current repository.hint: Clones of the outer repository will not contain the contents ofhint: 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-Themehint:hint: If you added this path by mistake, you can remove it from thehint: index with:hint:hint: git rm --cached themes/ICS-Hugo-Themehint: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 indexshenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:07 /home/shenweiyan/HugoSource$ git rm --cached themes/ICS-Hugo-Themeerror: the following file has staged content different from both thefile 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 indexshenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:34 /home/shenweiyan/HugoSource$ lsconfig.toml content resourcesshenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:40 /home/shenweiyan/HugoSource$ git rm --cached themes/ICS-Hugo-Theme -frm '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-ThemeCloning 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 7772Receiving objects: 100% (8344/8344), 9.45 MiB | 7.62 MiB/s, done.Resolving deltas: 100% (4358/4358), done.
更新子模块
我们引入了别人的仓库之后,如果该仓库作者进行了更新,我们需要手动进行更新。即进入子模块后,执行:
$ git pullYou 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.gitFrom https://github.com/shenweiyan/ICS-Hugo-Theme* branch HEAD -> FETCH_HEADUpdating 3501a4d..3036221Fast-forwardREADME.md | 2 +-layouts/404.html | 4 ++--2 files changed, 3 insertions(+), 3 deletions(-)
进行更新。
