版本控制 - git创建分支失败,无法提交,难以理解分支的用途

 840126054贝尔 发布于 2022-10-25 17:59

疑惑

对git分支不了解,今天测试使用git分支,本地创建了一个 new分支,commitpush报错了,且远程并没有看到new分支。

截图

远程截图bitbucket.org,只看到默认的master分支,看不到new分支

命令

Welcome to Git (version 1.9.5-preview20141217)


Run 'git help git' to display the help index.
Run 'git help ' to display help for specific commands.

Administrator@NSGRZQBMHMHTYQF /E
$ mkdir test

Administrator@NSGRZQBMHMHTYQF /E
$ cd test

Administrator@NSGRZQBMHMHTYQF /E/test
$ touch "d" > 1.txt

Administrator@NSGRZQBMHMHTYQF /E/test
$ ls
1.txt  d

Administrator@NSGRZQBMHMHTYQF /E/test
$ git init
Initialized empty Git repository in e:/test/.git/

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add ..." to include in what will be committed)

        1.txt
        d

nothing added to commit but untracked files present (use "git add" to track)

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git add --all

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git commit -m "head version"
[master (root-commit) 22a9e38] head version
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1.txt
 create mode 100644 d

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git remote add origin git@bitbucket.org:myacc/br.git

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git push -u origin --all # pushes up the repo and its refs for the first time
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 214 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@bitbucket.org:myacc/br.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git branch new

Administrator@NSGRZQBMHMHTYQF /E/test (master)
$ git checkout new
Switched to branch 'new'

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
nothing to commit, working directory clean

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ touch "2" > 2.txt

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
Untracked files:
  (use "git add ..." to include in what will be committed)

        2
        2.txt

nothing added to commit but untracked files present (use "git add" to track)

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git add --all

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

        new file:   2
        new file:   2.txt


Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git commit -m "branch new head version"
[new 4429a97] branch new head version
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2
 create mode 100644 2.txt

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git push -u origin:new
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

ssh: Could not resolve hostname origin: no address associated with name
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Administrator@NSGRZQBMHMHTYQF /E/test (new)
$ git status
On branch new
nothing to commit, working directory clean

对分支的理解

1.创建了分支之后,再 git add . 就添加了,然后git commit -m ""就提交到了当前的分支?
2.如果运行了 git checkout new,那么是不是会从远程下载new分支到本地,直接自动合并?
3.如果运行了 git checkout new,然后关闭git bash再打开git bash,默认的就是new分支了
4.git 默认的分支就是master分支?

分支的用途

假如开发ubuntu,A程序员在开发LTS12.04分支,然后B程序员却继续开发 V13 版本?
然后某一次B程序员需要去帮A程序员开发LTS12.04,那B应该先将项目V13的所有文件git push V13分支,再 git checkout LTS12.04,再git pull拉取所有的LTS12.04分支。修改后,再git push LTS12.04分支,再checkout V13分支,再 git pull V13所有程序,才返回到了V13的分支?
是这样吗?但git pull不是合并了B项目文件夹下的V13和LTS12.04两个版本的代码了吗

难以理解……

4 个回答
  • 把 newBranch 分支 push 上去的话, 你要在 master 上合并 new 分支: git merge newBranch
    总体步骤:

    git checkout master
    git merge newBranch
    git add .
    git commit -a -m "新分支"
    git pull origin master
    git push origin master
    
    如果你只想单单提交自己的newBranch分支, 那么按照以下方法操作:
    git checkout -b newBranch
    git status
    # modified a.txt
    git add a.txt
    git commit a.txt -m "新分支"
    git push origin newBranch
    
    2022-10-27 00:29 回答
  • 应该是push这个过程出现了问题:git push origin new

    Q: 创建了分支之后,再 git add . 就添加了,然后git commit -m ""就提交到了当前的分支?

    A: 提交代码到本地

    Q: 如果运行了 git checkout new,那么是不是会从远程下载new分支到本地,直接自动合并?

    A: 不会从远程下载new分支到本地,除非显示的调用git pull origin

    Q: 如果运行了 git checkout new,然后关闭git bash再打开git bash,默认的就是new分支了

    A: 对的

    Q: git 默认的分支就是master分支?

    A: git 默认分支是master分支

    推荐从头学习一下吧:https://github.com/numbbbbb/progit-zh-pdf-epub-mobi

    2022-10-27 00:29 回答
  • Git 在呼唤你好好的看下人家的报错信息啊! 执行完

    git config --global push.default matching
    

    就会正常了,而且push 的命令是

    git push origin new
    

    啊!什么时候成了origin:new了啊!

    2022-10-27 00:29 回答
  • 语法错误。。。明确的报错提示,都不看么

    ssh: Could not resolve hostname origin: no address associated with name
    fatal: Could not read from remote repository.
    

    push新分支应该这样写(你在哪看到是加冒号的 =。=)

    git push -u 远端仓库名称 远端仓库分支名称
    git push -u origin new
    

    ===========================================

    对分支的理解

    1.创建了分支之后,再 git add . 就添加了,然后git commit -m ""就提交到了当前的分支?

    创建新分支不代表切换到分支, git branch new_branch只是从当前分支的当前状态创建一个新的分支,但是此时直接修改并commit 还是在当前分支上的, 只有git checkout new_branch 才会切换到新建的分支

    git checkout -b new_branch 等效于上面两条命令之和

    2.如果运行了 git checkout new,那么是不是会从远程下载new分支到本地,直接自动合并?

    只有push/pull/fetch是跟远端交互的,commit/checkout之类的都是纯本地操作
    checkout只是从当前使用的分支切换到本地另外一个分支,换句话说就是把当前工作空间的所有文件内容变为另一个分支的状态

    "从远程下载new分支到本地,直接自动合并" 是在new分支上git pull 的效果

    3.如果运行了 git checkout new,然后关闭git bash再打开git bash,默认的就是new分支了

    是的,分支状态是持久化的,所有信息保存在.git目录下

    4.git 默认的分支就是master分支?

    是的

    分支的用途

    假如开发ubuntu,A程序员在开发LTS12.04分支,然后B程序员却继续开发 V13 版本?
    然后某一次B程序员需要去帮A程序员开发LTS12.04,那B应该先将项目V13的所有文件git push V13分支不需要push,但必须commit/stash 否则修改内容会丢失,再 git checkout LTS12.04,再git pull拉取所有的LTS12.04分支这里应该描述为拉取1204分支上所有的远端变更。修改后,先git commit再git push LTS12.04分支,再checkout V13分支,再 git pull V13所有程序同样不需要pull,才返回到了V13的分支?
    是这样吗?但git pull不是合并了B项目文件夹下的V13和LTS12.04两个版本的代码了吗

    pull只会从远端拉取其他人push上去的变更,合并到本地“对应”的分支,跨分支的合并只能在本地通过git merge完成

    2022-10-27 00:29 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有