search

Home  >  Q&A  >  body text

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

疑惑

对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 <command>' 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 <file>..." 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 <file>..." 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 <file>..." 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两个版本的代码了吗

难以理解……

高洛峰高洛峰2796 days ago898

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-28 09:07:49

    Grammar error. . . Don’t you even read the clear error message?

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

    Push a new branch should be written like this (where did you see the colon =.=)

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

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

    Understanding of branches

    1. After creating the branch, git add . will add it, and then git commit -m "" will commit it to the current branch?

    Creating a new branch does not mean switching to the branch,

    will switch to the newly created branchgit branch new_branch只是从当前分支的当前状态创建一个新的分支,但是此时直接修改并commit 还是在当前分支上的, 只有git checkout new_branch

    Equivalent to the sum of the above two commandsgit checkout -b new_branch

    2. If you run git checkout new, will the new branch be downloaded from the remote to the local and merged directly and automatically?

    Only

    just switches from the currently used branch to another local branch. In other words, it changes all the file contents of the current workspace to the state of another branchpush/pull/fetch是跟远端交互的,commit/checkout之类的都是纯本地操作
    checkout

    "Download the new branch from the remote to the local and directly merge it automatically" is the effect of

    on the new branchgit pull

    3. If you run git checkout new, then close git bash and then open git bash, the default branch will be new

    Yes, branch status is persisted and all information is saved in the .git directory

    4. The default branch of git is the master branch?

    Yes

    Purpose of branch

    Suppose when developing Ubuntu, programmer A is developing the LTS12.04 branch, and then programmer B continues to develop the V13 version?

    Then one time, programmer B needs to help programmer A develop LTS12.04. Then B should first git push all the files of project V13 to the V13 branch
    , and then return to the V13 branch? 不需要push,但必须commit/stash 否则修改内容会丢失,再 git checkout LTS12.04,再git pull拉取所有的LTS12.04分支这里应该描述为拉取1204分支上所有的远端变更。修改后,先git commit再git push LTS12.04分支,再checkout V13分支,再 git pull V13所有程序同样不需要pull is that so? But didn’t git pull merge the codes of the V13 and LTS12.04 versions in the B project folder?

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-04-28 09:07:49

    Git is calling you to take a good look at other people’s error messages! Executed

    git config --global push.default matching
    

    will be normal, and the command of push is

    git push origin new
    

    Ah! When did it become origin:new?

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-04-28 09:07:49

    should be push这个过程出现了问题:git push origin new

    Q: After creating a branch, git add . adds it, and then git commit -m "" commits it to the current branch?

    A: Submit the code locally

    Q: If you run git checkout new, will the new branch be downloaded from the remote to the local and merged directly and automatically?

    A: Will not download from remote new分支到本地,除非显示的调用git pull origin

    Q: If you run git checkout new,然后关闭git bash再打开git bash, the default is the new branch

    A: Right

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

    A: git 默认分支是masterbranch

    It is recommended to learn it from the beginning: https://github.com/numbbbbb/progit-zh-pdf-epub-mobi

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-04-28 09:07:49

    If you push the newBranch branch, you need to merge the new branch on the master: git merge newBranch
    Overall steps:

    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
    

    reply
    0
  • Cancelreply