1、git常用命令
#创建本地仓库
git init
#查看当前分支
git branch
#查看所有分支
git branch -a
#切换分支
git checkout 分支名
#创建并切换分支
git checkout -b 分支名
#删除分支
git branch -d 分支名
#查看修改
git status
#提交文件
git add XXX
#删除文件
git rm XXX
#提交暂存区到本地仓库
git commit -m "说明信息"
#推送到远程仓库
git push origin 分支名
#拉取最新代码
git pull
#合并分支(先切换到你要合并到的那个分支)
git merge 被合并的分支
2、协同开发时冲突的解决
先看下分支上的代码
#include <stdio.h>
#include <string.h>
int main()
{
char data[100] = {0};
int length = strlen(data);
for(int i = 0; i < length; i++)
{
printf("%c", data[i]);
}
printf("\n");
return 0;
}
A用户修改代码,并提交。
#include <stdio.h>
#include <string.h>
int main()
{
char data[100] = "my name is A";
int length = strlen(data);
for(int i = 0; i < length; i++)
{
printf("%c", data[i]);
}
printf("\n");
return 0;
}
B用户修改代码
#include <stdio.h>
#include <string.h>
int main()
{
char data[100] = "my name is B";
int length = strlen(data);
for(int i = 0; i < length; i++)
{
printf("%c", data[i]);
}
printf("\n");
return 0;
}
当B用户git push时,就会出现冲突,下面是报错信息。
lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master)
$ git push origin master
To https://gitee.com/lingpe/kaol.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/lingpe/kaol.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
接下来我们需要需要先拉取远程分支的代码
lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 308 bytes | 51.00 KiB/s, done.
From https://gitee.com/lingpe/kaol
1bb7c56..65d76a9 master -> origin/master
Auto-merging main.cpp
CONFLICT (content): Merge conflict in main.cpp
Automatic merge failed; fix conflicts and then commit the result.
接下来看下哪里产生了冲突
lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master|MERGING)
$ vim main.cpp
#include <stdio.h>
#include <string.h>
int main()
{
<<<<<<< HEAD
char data[100] = "my name is B";
=======
char data[100] = "my name is A";
>>>>>>> 65d76a9f8ffabbd5983b101ca2b02b31bc854fb8
int length = strlen(data);
for(int i = 0; i < length; i++)
{
printf("%c", data[i]);
}
printf("\n");
return 0;
}
然后手动去解决冲突。先删除特殊符号,然后再根据需求保留或删除相应代码
#include <stdio.h>
#include <string.h>
int main()
{
char data1[100] = "my name is B";
char data2[100] = "my name is A";
int length = strlen(data1);
for(int i = 0; i < length; i++)
{
printf("%c", data1[i]);
}
printf("\n");
return 0;
}
然后再重新提交
lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master|MERGING)
$ git add .
lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master|MERGING)
$ git commit -m "解决冲突"
[master 32ffcce] 解决冲突
lng@DESKTOP-9TD21KL MINGW64 ~/Desktop/kaol/kaol (master)
$ git push origin master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 12 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 671 bytes | 671.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/lingpe/kaol.git
65d76a9..32ffcce master -> master
至此冲突成功解决。
|