git创建分支失败,无法提交,难以理解分支的用途
疑惑
对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 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两个版本的代码了吗
难以理解……
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
语法错误。。。明确的报错提示,都不看么
push新分支应该这样写(你在哪看到是加冒号的 =。=)
===========================================
对分支的理解
创建新分支不代表切换到分支,
git branch new_branch
只是从当前分支的当前状态创建一个新的分支,但是此时直接修改并commit 还是在当前分支上的, 只有git checkout new_branch
才会切换到新建的分支git checkout -b new_branch
等效于上面两条命令之和只有
push/pull/fetch
是跟远端交互的,commit/checkout
之类的都是纯本地操作checkout
只是从当前使用的分支切换到本地另外一个分支,换句话说就是把当前工作空间的所有文件内容变为另一个分支的状态"从远程下载new分支到本地,直接自动合并" 是在new分支上
git pull
的效果是的,分支状态是持久化的,所有信息保存在.git目录下
是的
分支的用途
假如开发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完成
Git 在呼唤你好好的看下人家的报错信息啊! 执行完
就会正常了,而且
push
的命令是啊!什么时候成了
origin:new
了啊!应该是
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:
gi
t 默认的分支就是master
分支?A:
git
默认分支是master
分支推荐从头学习一下吧:https://github.com/numbbbbb/progit-zh-pdf-epub-mobi
把 newBranch 分支 push 上去的话, 你要在 master 上合并 new 分支: git merge newBranch
总体步骤: