git的简单使用。

本文详细介绍如何使用Git进行分支管理,包括创建、切换分支的操作步骤,以及如何解决代码级和文件路径级的冲突。通过实例演示了如何高效地利用Git进行版本控制。

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

一般使用git都只有一个master分支,这是默认的。

假设现在只有你的git远程仓库里面只有master分支

首先,配置好git的环境,这在另一篇博客中已经写到。

其次使用git clone 命令将git上面的数据clone到本地仓库。此时git会默认为你创建一个master分支。

其次就是平时用的git add *,git commit -m '',git pull,git push的命令。

如果你的git远程仓库在原来版本的基础上又创建了一个新的分支,来记录另外一个版本。(多个分支

首先创建一个新的分支;(建立分支的原因是为了在本地也能一目了然的看的远程仓库的版本,也可以不用建)

git branch test

再切换当前的分支:

git checkout test

然后用git commit -m ''提交到本地仓库;

git commit -m'wt'

其次更新远程仓库的那个分支的代码;

git pull origin test

最后将代码push到远程仓库的那个分支。

git push origin test

此时就得用到git branch命令,下面给出git branch的常用命令:

参考:http://blog.youkuaiyun.com/wirelessqa/article/details/20153689

查看当前有哪些branch

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch
* master

新建一个branch xm2.x

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch xm2.x

切换到一个branch

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git checkout xm2.x

新建并且切换到该branch,例: xm2.x

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git checkout -b xm2.x

再次查看

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch
* master
  xm2.x

添加一个文件到你的repo

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git add bixiaopeng.txt

添加所有的文件 git add .

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git add .

commit一个文件

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git commit -m "bixiaopeng test case"

commit到本地

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git commit -a -m "xm2.x test case"
[xm2.x f78f430] xm2.x test case
 39 files changed, 384 insertions(+)
 create mode 100644 AndroidManifest.xml
………….

查看几次commit的区别

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git diff

将branch push到远程

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git push origin xm2.x
Counting objects: 78, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (51/51), done.
Writing objects: 100% (77/77), 565.97 KiB, done.
Total 77 (delta 3), reused 0 (delta 0)
remote: To git@mirror.gitlab.*****.com:/home/git/repositories/xiaopeng.bxp/xmrobotium.git
remote:  * [new branch]      xm2.x -> xm2.x
To git@gitlab.****.com:xiaopeng.bxp/xmrobotium.git
 * [new branch]      xm2.x -> xm2.x

查看远程分支

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch -r
  origin/master
  origin/xm2.x

查看本地和远程分支

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch -a
  master
* xm2.x
  remotes/origin/master
  remotes/origin/xm2.x

修改branch的名字

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch -m xm2.x test2.x
bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch -r
  origin/master
  origin/xm2.x

查看本地和远程所有的分支

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch -a
  master
* test2.x
  remotes/origin/master
  remotes/origin/xm2.x

删除远程分支

bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git push origin --delete xm2.x
remote: To git@mirror.gitlab.****.com:/home/git/repositories/xiaopeng.bxp/xmrobotium.git
remote:  - [deleted]         xm2.x
To git@gitlab.*****.com:xiaopeng.bxp/xmrobotium.git
 - [deleted]         xm2.x
bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch -r
  origin/master
  origin/test2.x 

 

最后说说git的冲突

git的冲突有两种:1.两个修个了同一处代码,代码级别的冲突。2.两个人修改了同一处的文件路径,文件路径级别的冲突。

对于第一种冲突查找到出冲突的代码的地方,根据实际删除<<<  >>>  === 和一些不需要的代码即可解决。对于第二种冲突,首先找到本工程的.xcodepro文件右击查看包内容。打开project.pbxproj文件,找到出冲突的地方,删除<<<  >>>  === 和一些不需要的文件路径即可解决。

温馨提示:header是自己添加的,其他的是别人添加的。

转载于:https://my.oschina.net/Atoman/blog/748937

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值