盒子
盒子
文章目录
  1. 新建 GitLab 仓库时官方给出的命令
    1. Git global setup
    2. Create a new repository
    3. Existing folder
    4. Existing Git repository
  2. 新建 GitHab 仓库时官方给出的命令
    1. …or create a new repository on the command line
    2. …or push an existing repository from the command line
    3. …or import code from another repository
  3. Git 免密码
    1. Git HTTPS 方式传输文件免密码
      1. cache
      2. store
    2. Git SSH 方式传输文件免密码
  4. 分支管理
    1. 远程服务器(远程仓库)
      1. 远程服务器(远程仓库)是什么
      2. 多个远程服务器
    2. 远程分支
      1. 删除远程分支
    3. 本地分支
  5. 多人协同完整流程
  6. REF

git 使用小结

新建 GitLab 仓库时官方给出的命令

Git global setup

git config --global user.name "YourName"
git config --global user.email "YourEmail"

Create a new repository

git clone https://gitlab.com/holmeyoung/num_recog.git
cd num_recog
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Existing folder

cd existing_folder
git init
git remote add origin https://gitlab.com/holmeyoung/num_recog.git
git add .
git commit -m "Initial commit"
git push -u origin master

Existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/holmeyoung/num_recog.git
git push -u origin --all
git push -u origin --tags

新建 GitHab 仓库时官方给出的命令

…or create a new repository on the command line

echo "# tmp" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:Holmeyoung/tmp.git
git push -u origin master

…or push an existing repository from the command line

git remote add origin git@github.com:Holmeyoung/tmp.git
git push -u origin master

…or import code from another repository

You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

Git 免密码

Git HTTPS 方式传输文件免密码

cache

git config --global credential.helper cache

或直接修改 ~/.gitconfig

> [credential]
> helper = cache --timeout=3600
>

采用 cache 形式,密码会临时保存在 ~/git-credential-cache 文件夹下

store

git config --global credential.helper store

或直接修改 ~/.gitconfig

> [credential]
> helper = store
>

采用 store 形式,密码会永久保存在 ~/.git-credentials 文件中

Git SSH 方式传输文件免密码

配置密钥即可

分支管理

远程服务器(远程仓库)

远程服务器(远程仓库)是什么

有命令

git push origin master:master

git pull origin master:master
git fetch origin

这里的 origin 究竟是什么

有命令

git remote add origin git@code.ziroom.com:shuly/wiki.git

所以

origin --> git@code.ziroom.com:shuly/wiki.git

origin 即为指向远程服务器 `git@code.ziroom.com:shuly/wiki.git` 的远程服务器名

多个远程服务器

假设还有另一个服务器 `git@github.com。可以用第二章中提到的git remote add命令把它加为当前项目的远程分支之一。我们把它命名为origin_dev` ,以便代替完整的 Git URL 以方便使用。

执行命令

git remote add origin_dev git@github.com:Holmeyoung/wiki.git

现在你可以用 git fetch origin_dev 来获取另一台服务器上你还没有的数据了。执行该命令会创建一个名为 origin_dev/master 的远程分支,指向 origin_dev 服务器上 master 分支所在的提交对象

远程分支

删除远程分支

如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放稳定代码的分支),可以用这个非常无厘头的语法来删除它:git push [远程名] :[分支名] 。如果想在服务器上删除 develop 分支,运行下面的命令:

git push origin :develop

本地分支

  • 创建一个叫做 feature_x 的分支,并切换过去:

    git checkout -b feature_x
  • 切换回主分支:

    git checkout master
  • 再把新建的分支删掉:

    git branch -d feature_x
  • 除非你将分支推送到远端仓库,不然该分支就是 不为他人所见的:

    git push origin <branch>

多人协同完整流程

  • GitLab 新建远程分支 develop

这一步的意义在于防止直接操作 master 分支造成无法挽回的后果

  • 下载新的远程分支

    git fetch origin

    值得注意的是,在 fetch 操作下载好新的远程分支之后,你仍然无法在本地编辑该远程仓库中的分支。换句话说,在本例中,你不会有一个新的 develop 分支,有的只是一个你无法移动的 origin/develop 指针

  • 操作下载的远程分支

    • 如果要把该远程分支的内容合并到当前分支,可以运行

      git merge origin/develop
    • 如果想要一份自己的 serverfix 来开发,可以在远程分支的基础上分化出一个新的分支来

      git checkout -b develop origin/develop

      这会切换到新建的 develop 本地分支,其内容同远程分支 origin/develop 一致,这样你就可以在里面继续开发了。

    推荐

  • 新建本地用户分支并开发

    git checkout -b holmeyoung

    holmeyoung 分支开发完毕后

    • 提交到缓存区

      git add --all
    • 提交到 HEAD

      git commit -m "holmeyoung change"
  • 合并改动到 develop 分支

    • 跳到 develop 分支

      git checkout develop
    • holmeyoung 分支的改动 merge 到 develop 分支

      git merge holmeyoung
    • 推送本地 develop 分支到远程 develop 分支

      git push origin develop:develop
  • 合并改动到 master 分支

    • 跳到 master 分支

      git checkout master
    • develop 分支的改动 merge 到 master 分支

      git merge develop
    • 推送本地 master 分支到远程 master 分支

      git push origin master:master

REF

git - 简易指南

Git远程操作详解

Git Book

支持一下
万一真的就有人扫了呢
  • 微信扫一扫
  • 支付宝扫一扫