Github本地新建分支并推送远程仓库

有时候需要在本地新建了代码分支,想直接推送到远程仓库并新建代码分支,操作如下:

第一步,先初始化本地git仓库(如果是从远程仓库拉下来的可以忽略这一步)

damon@damon-Latitude-7380:Ntrip$ git init
Initialized empty Git repository in /home/damon/Work/ntrip/Ntrip/.git/

第二步,添加远程仓库:

damon@damon-Latitude-7380:Ntrip$ git remote add origin https://github.com/lxxxen/ntrip
damon@damon-Latitude-7380:Ntrip$ git remote -v
origin	https://github.com/lxxxen/ntrip (fetch)
origin	https://github.com/lxxxen/ntrip (push)

第三步,把本地的代码先commit:

damon@damon-Latitude-7380:Ntrip$ git status
On branch master

Initial commit

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

	makefile
	ntripclient
	ntripclient.c
	serial.c
	startntripclient.sh

nothing added to commit but untracked files present (use "git add" to track)
damon@damon-Latitude-7380:Ntrip$ git add .
damon@damon-Latitude-7380:Ntrip$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   makefile
	new file:   ntripclient
	new file:   ntripclient.c
	new file:   serial.c
	new file:   startntripclient.sh

damon@damon-Latitude-7380:Ntrip$ git commit -m "NEW: new branch for ntrip serila."
[master (root-commit) ee026c3] NEW: new branch for ntrip serila.
 5 files changed, 1486 insertions(+)
 create mode 100755 makefile
 create mode 100755 ntripclient
 create mode 100755 ntripclient.c
 create mode 100644 serial.c
 create mode 100755 startntripclient.sh

第四步,新建本地分支并推送远程仓库:

damon@damon-Latitude-7380:Ntrip$ git branch
* master
damon@damon-Latitude-7380:Ntrip$ 
damon@damon-Latitude-7380:Ntrip$ 
damon@damon-Latitude-7380:Ntrip$ git branch test
damon@damon-Latitude-7380:Ntrip$ 
damon@damon-Latitude-7380:Ntrip$ git push origin test
Username for 'https://github.com': lxxxxx@126.com
Password for 'https://lxxxxx@126.com@github.com': 
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 25.23 KiB | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'test' on GitHub by visiting:
remote:      https://github.com/lxxxxx/ntrip/pull/new/test
remote: 
To https://github.com/lxxxxx/ntrip
 * [new branch]      test -> test

完事!

<think>好的,我现在需要帮助用户了解如何在Git中强制推送更改到GitHub远程仓库。首先,我需要回忆一下用户提供的引用内容,特别是引用[1]、[2]和[3],这些可能和问题相关。 用户的问题是关于强制推送(force push)到GitHub远程仓库。根据引用[2]和[3],当尝试强制推送到受保护的分支时,GitLab会报错,提示不允许这样做,解决办法是管理员取消分支的保护状态。但用户现在问的是GitHub,不是GitLab,所以需要确认GitHub的处理方式是否类似。 首先,我应该解释什么是强制推送。强制推送是指用本地的提交历史覆盖远程仓库的历史,这通常用于修正提交历史,比如修改commit信息、合commit等。但这样做有风险,尤其是多人协作时,可能会导致其他人的工作丢失,因此需要谨慎使用。 接下来,用户需要具体的步骤。根据引用[1],常规的推送流程是git add,commit,然后push。而强制推送一般使用git push --force或者git push --force-with-lease。需要解释这两个命令的区别,--force-with-lease相对安全,因为它会检查远程分支的当前状态是否和本地预期的一致,避免覆盖其他人的提交。 然后,用户可能遇到的问题是权限问题,即受保护的分支不允许强制推送。根据引用[3],在GitLab中需要管理员取消分支保护,同样,GitHub也有分支保护设置。需要指导用户如何在GitHub上检查和修改分支保护设置,比如进入仓库的Settings -> Branches,找到分支保护规则,允许强制推送或者取消保护。 另外,需要提醒用户强制推送的风险,确保在操作前备份代码,或者只在个人分支上进行,避免影响主分支或其他协作者。此外,如果已经推送了错误的提交,可能需要使用rebase或amend来修改历史,然后再强制推送。 最后,生成相关问题,可能涉及如何撤销强制推送、处理强制推送后的恢复,或者其他相关Git操作的问题。 总结步骤: 1. 解释强制推送的作用和风险。 2. 提供强制推送的命令。 3. 说明GitHub分支保护设置及如何调整。 4. 强调注意事项和最佳实践。 5. 生成相关问题。 需要确保使用中文,LaTeX格式正确,行内公式用$...$,独立公式用$$...$$,但这个问题可能不涉及数学公式,所以可能不需要用到。同时,根据引用内容正确标注来源,比如GitHub分支保护设置参考引用[3]的处理方式。</think>### 如何在Git中强制推送更改到GitHub远程仓库 #### 1. 强制推送的作用与风险 强制推送(`git push --force`)会覆盖远程仓库的提交历史,常用于修正本地历史记录(如合提交、修改提交信息)。但**可能覆盖他人提交**,导致协作混乱,需谨慎使用[^2][^3]。 #### 2. 强制推送操作步骤 1. **本地修改提交** 使用常规流程修改文件提交: ```bash git add . git commit -m "修正提交历史" ``` 2. **执行强制推送** - 使用`--force`覆盖远程分支: ```bash git push --force origin 分支名 ``` - 更安全的替代命令`--force-with-lease`(检查远程状态是否与本地一致): ```bash git push --force-with-lease origin 分支名 ``` #### 3. 绕过GitHub分支保护(如需) 若目标分支受保护,需**管理员权限**调整设置: 1. 进入GitHub仓库页面:`Settings > Branches`。 2. 在`Branch protection rules`中找到目标分支规则。 3. 取消勾选`Require pull request reviews before merging`或允许强制推送(部分版本支持)。 #### 4. 注意事项 - **备份分支**:强制推送前建议新建备份分支: ```bash git checkout -b backup_branch ``` - **协作场景**:确保其他成员已同步最新提交,避免历史冲突。 - **恢复方法**:若误操作,可通过`git reflog`找回本地历史,再重新推送。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值