git命令

创建新分支

要创建一个新分支并切换到该分支,可以使用以下命令

git checkout -b new-branch

或者,你可以分别创建和切换分支:

git branch new-branch

git checkout new-branch

查看分支

查看当前所有分支:

git branch

查看所有分支(包括远程分支):

git branch -a

列出所有远程分支:使用 git branch 命令查看远程分支。 

 git branch -r

检出(切换到)远程分支:如果你想要检出某个远程分支,例如 origin/feature-branch,可以使用以下命令:

 git checkout -b feature-branch origin/feature-branch

切换分支

切换到已有的分支:

git checkout branch-name

在 Git 2.23 及以上版本,可以使用 git switch 命令:

git switch branch-name

合并分支

将另一个分支合并到当前分支:

git checkout current-branch git merge branch-to-merge

删除分支

删除本地分支:

git branch -d branch-name

如果分支没有被完全合并,可以使用强制删除:

git branch -D branch-name

删除远程分支:

git push origin --delete branch-name

重命名分支

重命名当前分支:

git branch -m new-branch-name

重命名其他分支:

git branch -m old-branch-name new-branch-name

发布分支

将本地分支推送到远程仓库:

git push origin branch-name

跟踪远程分支

创建一个跟踪远程分支的本地分支:

git checkout -b local-branch-name origin/remote-branch-name

合并策略

在合并分支时,可以选择不同的策略,例如:

Fast-forward 合并

当目标分支直接位于源分支之后,可以直接快进:

git checkout master git merge feature-branch

No fast-forward 合并

强制创建一个新的合并提交:

git checkout master git merge --no-ff feature-branch

解决冲突

在合并分支时可能会遇到冲突,需要手动解决。解决冲突后:

  1. 编辑有冲突的文件,解决冲突。
  2. 将解决冲突的文件标记为已解决:

git add resolved-file

  1. 提交合并:

git commit

拉取远程分支的最新更改:

git pull origin <remote-branch-name>

例如,如果你想从远程存储库的 main 分支拉取更改到当前分支,可以使用:

git pull origin main

回滚代码

git branch 新分支名称 提交id

查看提交记录

git log

拉取远程分支

更新远程引用:首先确保你已经从远程仓库获取了最新的信息。

 git fetch

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值