Git 分支操作.md 8.7 KB

Git 分支操作

// 查看分支
git branch:查看本地所有分支和当前所处分支
git branch -r:查看远程分支(前提是进行了关联)
git branch -a:查看所有分支(包括远程和本地分支)

// 创建分支
git branch branch_name:创建新分支
git checkout branch_name:在本地切换到branch_name这个分支

// 合并分支(若有多个分支,一般在master分支上合并其他分支)
git merge branch_name

// 删除分支(可以不用)
git branch -d branch_name:删除本地仓库的branch_name分支
git push -d origin branch_name:删除远程仓库的branch_name分支  // 不要用这个,误删就不好了

// 关联远程仓库分支
// branch_name最好不要是master分支,多人开发同时使用一个分支时,推送数据的时候容易冲突;branch_name 就用 xk 或者 xwy ,对应你们开发的分支
// 本地分支的名字需要和远程仓库的分支名相对应(需在本地创建和远程仓库对应名字一样的分支)
git push --set-upstream origin branch_name:设置本地的branch_name分支对应远程仓库的branch_name分支
// 如果你们本地也有多个名字不同的分支,用下面这个;(branch_name2可以是master,这样就不用在本地创建和远程仓库名字一样的分支)
git branch --set-upstream-to=origin/branch_name1 branch_name2:将远程的branch_name1分支与本地的branch_name2分支对应

// 将本地数据推送到远程仓库
git add .
git commit -m "desc"
git push // 不建议,若远程仓库和本地都只有一条命名相同的分支,则可以直接使用。其他情况不建议
// 若远程仓库与本地所关联的分支命名不同 (branch_name对应所要推送到的远程仓库分支)
git push origin HEAD:branch_name
// 若远程仓库与本地所关联的分支命名相同
git push origin HEAD

// 拉取数据
// 将远程仓库里的数据拉去到本地
git pull:将远程仓库的当前分支与本地仓库的当前分支合并
// 分支合并,多人多分支时使用,使用自己对应的分支
git pull origin branch_name:将远程仓库的branch_name分支与本地仓库的当前分支合并
git checkout -t origin/branch_name 将远程的branch_name分支拉取到本地

常用 git commit 注释

feat: 新功能特性
docs: 丰富文档
fix: 修复
update: 更新
chrone: 杂项

声明:在执行以下操作时,一定不要操作master分支。其他分支随意。

完整示例

以下命令皆在 git for windows 终端中运行

以我个人相对应的 cyx 分支为例

新建git仓库并初始化
在路径(可随意):d/桌面/Git 下,新建了一个文件夹TFPower,以下所有命令在TFPower文件夹下执行
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git init
Initialized empty Git repository in D:/桌面/Git/TFPower/.git/

cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git config --global user.name cyx

cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)
$ git config --global user.email 2862576553@qq.com

// 查看
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git config --global --list 
user.name=cyx
user.email=2862576553@qq.com
credential.http://git.fmode.cn:3000.provider=generic
关联远程仓库
// 关联远程仓库
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git remote add origin http://git.fmode.cn:3000/18779989085/202226701041.git



// 新建的文件加下是没有master分支的(git branch,为空),须随便创建一个文件
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower-app (master)$ touch demo.txt
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower-app (master)$ git add .
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower-app (master)$ git commit -m "commit demo.txt"
[master (root-commit) cd7df68] commit demo.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 demo.txt
 
// 出现了
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower-app (master)$ git branch
* master


// 获取特定远程分支的更新
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git fetch

// 关联分支
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git branch --set-upstream-to=origin/cyx master
branch 'master' set up to track 'origin/cyx'.

// 直接拉取错误,拒绝合并不相关的历史
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower-app (master)$ git pull
fatal: refusing to merge unrelated histories

//拉取分支最新版本(因为我已经在远程仓库创建好了),注意这里拉取的是cyx分支,跟之前关联的分支有关
git pull --allow-unrelated-histories
//即使关联其他分支,也可以拉去其他分支(master为例)
git pull origin master --allow-unrelated-histories // 不建议,专心开发自己相对应的分支就好
新建测试文件
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ touch test.txt
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git status
On branch master
Your branch is up to date with 'origin/cyx'.

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

nothing added to commit but untracked files present (use "git add" to track)

cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git add .
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git commit -m "chrone: test the cyx branch"
[master ce6f29f] chrone: test the cyx branch
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git branch
* master

// 直接 git push 会报错,因为我本地的分支为 master, 与云端上的不对应
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git push
fatal: The upstream branch of your current branch does not match
the name of your current branch.  To push to the upstream branch
on the remote, use

    git push origin HEAD:cyx

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.

To avoid automatically configuring an upstream branch when its name
won't match the local branch, see option 'simple' of branch.autoSetupMerge
in 'git help config'.

// 成功,需使用这种方式推送数据到云端(`git push origin HEAD:branch_name`, 其中branch_name对应的是云端分支名称)
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git push origin HEAD:cyx
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 271.00 KiB/s, done.
Total 3 (delta 1), reused 1 (delta 0), pack-reused 0 (from 0)
To http://git.fmode.cn:3000/18779989085/202226701041.git
   21a0091..ce6f29f  HEAD -> cyx
修改测试文件

在test.txt文件中添加如下内容(之前为空):

test
a
b
c
d
e
f
g
// 查看修改,修改了test.txt文件
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git status
On branch master
Your branch is up to date with 'origin/cyx'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git add .
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git commit -m "update: test the cyx branch & modified test.txt"
[master e696cab] update: test the cyx branch & modified test.txt
 1 file changed, 8 insertions(+)
 
 // 成功上传至远程仓库,且只能使用指令`git push origin HEAD:cyx`,直接`git push`会报错
cyx@LAPTOP-FA84GREJ MINGW64 /d/桌面/Git/TFPower (master)$ git push origin HEAD:cyx
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
To http://git.fmode.cn:3000/18779989085/202226701041.git
   ce6f29f..e696cab  HEAD -> cyx

结果

cyx 分支

image-20241209183428577

xk分支

image-20241209183616378

master分支

image-20241209183451118

可见:只修改了cyx分支内容,其他分支内容不变,实现不同开发人员独立开发。