zoukankan      html  css  js  c++  java
  • git学习 branch log rebase merge fetch remote add push pull

    16

    我创建了一个项目,然后通过下面的命令 push 到了 GitHub 上。如何再将这个项目 push 到其他远程仓库呢?

    git remote add github https://github.com/zxbetter/test.git
    git push -u github master

    方法一: 使用 git remote add 命令

    1.1# 如下命令查看远程仓库的情况,可以看到只有一个叫 github 的远程仓库。

    git remote
    github
    
    git remote -v
    github  https://github.com/zxbetter/test.git (fetch)
    github  https://github.com/zxbetter/test.git (push)

    1.2# 使用如下命令再添加一个远程仓库(这里以码云为例)

    git remote add oschina https://git.oschina.net/zxbetter/test.git

    1.3# 再次查看远程仓库的情况,可以看到已经有两个远程仓库了。然后再使用相应的命令 push 到对应的仓库就行了。这种方法的缺点是每次要 push 两次。

    git remote
    github
    oschina
    
    git remote -v
    github  https://github.com/zxbetter/test.git (fetch)
    github  https://github.com/zxbetter/test.git (push)
    oschina https://git.oschina.net/zxbetter/test.git (fetch)
    oschina https://git.oschina.net/zxbetter/test.git (push)

    方法二: 使用 git remote set-url 命令

    2.1# 删除方法一的 oschina 远程仓库。

    git remote rm oschina

    2.2# 使用如下命令添加远程仓库。

    git remote set-url --add github https://git.oschina.net/zxbetter/test.git

    2.3# 查看远程仓库情况。可以看到 github 远程仓库有两个 push 地址。这种方法的好处是每次只需要 push 一次就行了。

    git remote -v
    github  https://github.com/zxbetter/test.git (fetch)
    github  https://github.com/zxbetter/test.git (push)
    github  https://git.oschina.net/zxbetter/test.git (push)

    方法三: 修改配置文件

    打开 .git/config 找到 [remote "github"],添加对应的 url 即可,效果如下。这种方法其实和方法二是一样的。

    [remote "github"]
        url = https://github.com/zxbetter/test.git
        fetch = +refs/heads/*:refs/remotes/github/*
        url = https://git.oschina.net/zxbetter/test.git

    关于 git pull

    方法二和三在 push 的时候比较方便。但是在 pull 的时候只能从方法三中的第一个 url 地址拉取代码。而方法一则不存在这种问题(可能要解决冲突)。
    所以,如果只进行 push 操作,推荐方法二和三,如果也要进行 pull 操作,推荐方法一。

     

    1、git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令:

     git fetch orgin master //将远程仓库的master分支下载到本地当前branch中
    
     git log -p master  ..origin/master //比较本地的master分支和origin/master分支的差别
    
     git merge origin/master //进行合并

    也可以用以下指令:

    git fetch origin master:tmp //从远程仓库master分支获取最新,在本地建立tmp分支
    
    git diff tmp //將當前分支和tmp進行對比
    
    git merge tmp //合并tmp分支到当前分支

    2. git pull:相当于是从远程获取最新版本并merge到本地

    git pull origin master

    git pull 相当于从远程获取最新版本并merge到本地

    在实际使用中,git fetch更安全一些

    https://www.liaoxuefeng.com/wiki/896043488029600/900003767775424

    https://blog.csdn.net/hudashi/article/details/7664631/

  • 相关阅读:
    安卓9.0内测的背后,是上万App开发者半年来的适配优化
    错误记录:vue跟vue编译器版本不一致
    jspdf简单使用
    vue input添加回车触发
    vue watch bug记录
    SecureCRT通过拷贝配置文件登陆
    仿射变换
    opencv图像的旋转
    图像旋转的原理
    CvScalar
  • 原文地址:https://www.cnblogs.com/CreatorKou/p/11393059.html
Copyright © 2011-2022 走看看