GitHub 创建分支 (branch) 并提交分支

本文详细介绍了如何在GitHub上创建分支、提交更改并推送到指定分支的全过程。从克隆仓库开始,通过实例演示了如何使用git命令进行分支创建、切换、添加文件、提交修改及解决推送错误的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Branch: master

git clone https://github.com/ForeverStrongCheng/dbscan_clustering_algorithm

strong@foreverstrong:~/darknet_work$ git clone https://github.com/ForeverStrongCheng/dbscan_clustering_algorithm
Cloning into 'dbscan_clustering_algorithm'...
Username for 'https://github.com': chengyq116@163.com
Password for 'https://chengyq116@163.com@github.com': 
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 33 (delta 6), reused 30 (delta 6), pack-reused 0
Unpacking objects: 100% (33/33), done.
Checking connectivity... done.
strong@foreverstrong:~/darknet_work$

在这里插入图片描述

2. git branch

git branch - 查看本地分支
git branch -r - 查看远程分支
git branch -a - 查看本地仓库和远程分支 (a 是 all 的简写)

strong@foreverstrong:~/darknet_work$ cd dbscan_clustering_algorithm/
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch
* master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch -r
  origin/HEAD -> origin/master
  origin/master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

3. git branch dbscan_dev - git checkout dbscan_dev

git branch dbscan_dev - 新建本地分支 dbscan_dev
git checkout dbscan_dev - 切换到本地分支 dbscan_dev

git checkout -b dev
一条命令等同于以下两条命令
git branch dev
git checkout dev

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch dbscan_dev
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch
  dbscan_dev
* master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch -r
  origin/HEAD -> origin/master
  origin/master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch -a
  dbscan_dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git checkout dbscan_dev 
Switched to branch 'dbscan_dev'
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch
* dbscan_dev
  master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

4. git push origin dbscan_dev

git add . - 添加项目中所有文件
git commit -m "20200303" - 添加备注 20200303
git push origin dbscan_dev - 提交代码到指定分支 dbscan_dev

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ ls
data  examples  libdbscan.so  LICENSE  Makefile  obj  README.md  results  scripts  src
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git status
On branch dbscan_dev
nothing to commit, working directory clean
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

modified: README.md

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git status
On branch dbscan_dev
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git add .
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git commit -m "20200303"
[dbscan_dev 8d99cf5] 20200303
 1 file changed, 1 deletion(-)
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git push origin dbscan_dev 
Username for 'https://github.com': chengyq116@163.com
Password for 'https://chengyq116@163.com@github.com': 
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 277 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote: 
remote: Create a pull request for 'dbscan_dev' on GitHub by visiting:
remote:      https://github.com/ForeverStrongCheng/dbscan_clustering_algorithm/pull/new/dbscan_dev
remote: 
To https://github.com/ForeverStrongCheng/dbscan_clustering_algorithm
 * [new branch]      dbscan_dev -> dbscan_dev
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$

5. git push origin dbscan_dev

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git status
On branch dbscan_dev
nothing to commit, working directory clean
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git branch
* dbscan_dev
  master
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

modified: README.md

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git status
On branch dbscan_dev
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git add .
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git commit -m "version 1.2."
[dbscan_dev eb77a4b] version 1.2.
 1 file changed, 1 insertion(+), 1 deletion(-)
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

5.1. fatal: The current branch dbscan_dev has no upstream branch.

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, 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.

Since Git 2.0, Git defaults 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)

fatal: The current branch dbscan_dev has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin dbscan_dev

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

5.2. git push origin dbscan_dev

strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ git push origin dbscan_dev 
Username for 'https://github.com': chengyq116@163.com
Password for 'https://chengyq116@163.com@github.com': 
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/ForeverStrongCheng/dbscan_clustering_algorithm
   8d99cf5..eb77a4b  dbscan_dev -> dbscan_dev
strong@foreverstrong:~/darknet_work/dbscan_clustering_algorithm$ 

6. Branch: master

在这里插入图片描述

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值