创建与切换

使用命令行创建仓库

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

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

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

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

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

本地创建分支

  1. $ git branch v-1.2
  2. $ git branch
  3. * master
  4. v-1.2

切换本地分支

  1. $ git branch
  2. * master
  3. v-1.2
  4. $ git checkout v-1.2
  5. Switched to branch 'v-1.2'
  6. $ git branch
  7. master
  8. * v-1.2

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

  1. $ git add --all
  2. $ git commit -m "ASO design for Galaxy version 1.2"
  3. $ git push origin v-1.2
  4. Username for 'https://git.dev.tencent.com': shenweiyan
  5. Password for 'https://shenweiyan@git.dev.tencent.com':
  6. Counting objects: 19, done.
  7. Delta compression using up to 2 threads.
  8. Compressing objects: 100% (17/17), done.
  9. Writing objects: 100% (18/18), 29.87 KiB | 0 bytes/s, done.
  10. Total 18 (delta 3), reused 0 (delta 0)
  11. To https://git.dev.tencent.com/shenweiyan/aso_design.git
  12. * [new branch] v-1.2 -> v-1.2

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

  1. $ git branch -a
  2. master
  3. * v-1.2
  4. remotes/origin/HEAD -> origin/master
  5. remotes/origin/master
  6. remotes/origin/v-1.2
  7. $ git remote show origin

删除

删除本地分支

  1. $ git branch -D v-1.2

删除远程分支和 tag

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

  1. git push origin --delete <branchName>

删除 tag 这么用:

  1. git push origin --delete tag <tagname>

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

  1. git push origin :<branchName>

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

  1. git tag -d <tagname>
  2. 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.

Git 的一些常用用法笔记 - 图1

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

Git 的一些常用用法笔记 - 图2

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

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

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

  1. $ git remote show origin
  2. * remote origin
  3. Fetch URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git
  4. Push URL: https://shenweiyan:git-sz-0201@github.com/shenweiyan/galaxy.git
  5. HEAD branch: dev
  6. Remote branches:
  7. dev tracked
  8. master tracked
  9. refs/remotes/origin/release_17.09 stale (use 'git remote prune' to remove)
  10. refs/remotes/origin/release_18.05 stale (use 'git remote prune' to remove)
  11. release_13.01 tracked
  12. release_13.02 tracked
  13. release_13.04 tracked

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

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

回滚

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

  1. $ git log
  2. commit 610d4417c04ce2c53cfb7b77a0525ddb7695b869
  3. Author: shenweiyan <ishenweiyan@qq.com>
  4. Date: Fri Aug 23 09:08:06 2019 +0800
  5. fix ipad
  6. commit 8938003aa309ab3cd98af43e67ad4f4aaff5e672
  7. Author: shenweiyan <ishenweiyan@qq.com>
  8. Date: Fri Aug 23 09:00:22 2019 +0800
  9. add new post at 2019-08-23-09:00:22
  10. commit 31107425b1488b49d7a1a75f183149e40ba81223
  11. Author: shenweiyan <ishenweiyan@qq.com>
  12. Date: Thu Aug 22 15:50:56 2019 +0800
  13. add topic page urls
  14. commit bbf9d6803864dffd7e7965036a638b17be0eeda7
  15. Author: shenweiyan <ishenweiyan@qq.com>
  16. Date: Thu Aug 22 15:47:02 2019 +0800
  17. add about page images
  18. ......
  19. $ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672
  20. HEAD is now at 8938003 add new post at 2019-08-23-09:00:22

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

  1. # 方法一,先 git reset 回滚到本地,然后再强制 push 到远程。
  2. $ git reset --hard 8938003aa309ab3cd98af43e67ad4f4aaff5e672
  3. # 其中,-f:force(谨慎操作,备份预备)
  4. $ git push -u origin master -f

同步 GitHub fork 后新增加的分支

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

子模块

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

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

  • 正常克隆

    1. git clone https://github.com/user/project.git

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

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

    1. git submodule init
    2. git submodule update

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

    1. git submodule update --init --recursive

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

直接使用 git 的 submodule 命令:

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

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

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

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

一些 submodule 操作记录。

  1. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:29:13 /home/shenweiyan/HugoSource
  2. $ git add --all
  3. warning: adding embedded git repository: themes/ICS-Hugo-Theme
  4. hint: You've added another git repository inside your current repository.
  5. hint: Clones of the outer repository will not contain the contents of
  6. hint: the embedded repository and will not know how to obtain it.
  7. hint: If you meant to add a submodule, use:
  8. hint:
  9. hint: git submodule add <url> themes/ICS-Hugo-Theme
  10. hint:
  11. hint: If you added this path by mistake, you can remove it from the
  12. hint: index with:
  13. hint:
  14. hint: git rm --cached themes/ICS-Hugo-Theme
  15. hint:
  16. hint: See "git help submodule" for more information.
  17. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:29:13 /home/shenweiyan/HugoSource
  18. $ git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git themes/ICS-Hugo-Theme
  19. 'themes/ICS-Hugo-Theme' already exists in the index
  20. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:07 /home/shenweiyan/HugoSource
  21. $ git rm --cached themes/ICS-Hugo-Theme
  22. error: the following file has staged content different from both the
  23. file and the HEAD:
  24. themes/ICS-Hugo-Theme
  25. (use -f to force removal)
  26. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:18 /home/shenweiyan/HugoSource
  27. $ rm -rf themes/
  28. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:31 /home/shenweiyan/HugoSource
  29. $ git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git themes/ICS-Hugo-Theme
  30. 'themes/ICS-Hugo-Theme' already exists in the index
  31. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:34 /home/shenweiyan/HugoSource
  32. $ ls
  33. config.toml content resources
  34. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:40 /home/shenweiyan/HugoSource
  35. $ git rm --cached themes/ICS-Hugo-Theme -f
  36. rm 'themes/ICS-Hugo-Theme'
  37. shenweiyan@iZ7xv4bbjwm8qgx8m72z68Z 16:30:40 /home/shenweiyan/HugoSource
  38. $ git submodule add https://github.com/shenweiyan/ICS-Hugo-Theme.git themes/ICS-Hugo-Theme
  39. Cloning into '/data/HugoSource/themes/ICS-Hugo-Theme'...
  40. remote: Enumerating objects: 8344, done.
  41. remote: Counting objects: 100% (572/572), done.
  42. remote: Compressing objects: 100% (292/292), done.
  43. remote: Total 8344 (delta 307), reused 396 (delta 177), pack-reused 7772
  44. Receiving objects: 100% (8344/8344), 9.45 MiB | 7.62 MiB/s, done.
  45. Resolving deltas: 100% (4358/4358), done.

更新子模块

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

  1. $ git pull
  2. You are not currently on a branch.
  3. Please specify which branch you want to merge with.
  4. See git-pull(1) for details.
  5. git pull <remote> <branch>
  6. $ git pull https://github.com/shenweiyan/ICS-Hugo-Theme.git
  7. From https://github.com/shenweiyan/ICS-Hugo-Theme
  8. * branch HEAD -> FETCH_HEAD
  9. Updating 3501a4d..3036221
  10. Fast-forward
  11. README.md | 2 +-
  12. layouts/404.html | 4 ++--
  13. 2 files changed, 3 insertions(+), 3 deletions(-)

进行更新。