问题描述
疑惑对git分支不了解,今天测试使用git分支,本地创建了一个 new分支,commit后push报错了,且远程并没有看到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 testAdministrator@NSGRZQBMHMHTYQF /E$ cd testAdministrator@NSGRZQBMHMHTYQF /E/test$ touch 'd' > 1.txtAdministrator@NSGRZQBMHMHTYQF /E/test$ ls1.txt dAdministrator@NSGRZQBMHMHTYQF /E/test$ git initInitialized empty Git repository in e:/test/.git/Administrator@NSGRZQBMHMHTYQF /E/test (master)$ git statusOn branch masterInitial commitUntracked files: (use 'git add <file>...' to include in what will be committed)1.txtdnothing added to commit but untracked files present (use 'git add' to track)Administrator@NSGRZQBMHMHTYQF /E/test (master)$ git add --allAdministrator@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 dAdministrator@NSGRZQBMHMHTYQF /E/test (master)$ git remote add origin git@bitbucket.org:myacc/br.gitAdministrator@NSGRZQBMHMHTYQF /E/test (master)$ git push -u origin --all # pushes up the repo and its refs for the first timeCounting 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 -> masterBranch master set up to track remote branch master from origin.Administrator@NSGRZQBMHMHTYQF /E/test (master)$ git branch newAdministrator@NSGRZQBMHMHTYQF /E/test (master)$ git checkout newSwitched to branch ’new’Administrator@NSGRZQBMHMHTYQF /E/test (new)$ git statusOn branch newnothing to commit, working directory cleanAdministrator@NSGRZQBMHMHTYQF /E/test (new)$ touch '2' > 2.txtAdministrator@NSGRZQBMHMHTYQF /E/test (new)$ git statusOn branch newUntracked files: (use 'git add <file>...' to include in what will be committed)22.txtnothing added to commit but untracked files present (use 'git add' to track)Administrator@NSGRZQBMHMHTYQF /E/test (new)$ git add --allAdministrator@NSGRZQBMHMHTYQF /E/test (new)$ git statusOn branch newChanges to be committed: (use 'git reset HEAD <file>...' to unstage)new file: 2new file: 2.txtAdministrator@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.txtAdministrator@NSGRZQBMHMHTYQF /E/test (new)$ git push -u origin:newwarning: push.default is unset; its implicit value is changing inGit 2.0 from ’matching’ to ’simple’. To squelch this messageand maintain the current behavior after the default changes, use: git config --global push.default matchingTo squelch this message and adopt the new behavior now, use: git config --global push.default simpleWhen push.default is set to ’matching’, git will push local branchesto 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 correspondingremote 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 namefatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.Administrator@NSGRZQBMHMHTYQF /E/test (new)$ git statusOn branch newnothing 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两个版本的代码了吗
难以理解……
问题解答
回答1:语法错误。。。明确的报错提示,都不看么
ssh: Could not resolve hostname origin: no address associated with namefatal: 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完成
回答2:Git 在呼唤你好好的看下人家的报错信息啊! 执行完
git config --global push.default matching
就会正常了,而且push 的命令是
git push origin new
啊!什么时候成了origin:new了啊!
回答3:应该是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
回答4:把 newBranch 分支 push 上去的话, 你要在 master 上合并 new 分支: git merge newBranch总体步骤:
git checkout mastergit merge newBranchgit add .git commit -a -m '新分支'git pull origin mastergit push origin master如果你只想单单提交自己的newBranch分支, 那么按照以下方法操作:git checkout -b newBranchgit status# modified a.txtgit add a.txtgit commit a.txt -m '新分支'git push origin newBranch