git基础命令以及用法

1: 配置用户名以及邮箱

root@weizhibiao:~/git-daemon/git-daemon-init# git config --global user.name=wei.zhibiao
root@weizhibiao:~/git-daemon/git-daemon-init# git config --global user.email=13263374898@163.com
root@weizhibiao:~/git-daemon/git-daemon-init# git config --list
user.name=wei.zhibao
user.email=10101010101@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
root@weizhibiao:~/git-daemon/git-daemon-init# 

2:初始化一个git仓库

root@weizhibiao:~/git-daemon# mkdir git-daemon-init
root@weizhibiao:~/git-daemon# git init git-daemon-init/
Initialized empty Git repository in /root/git-daemon/git-daemon-init/.git/
root@weizhibiao:~/git-daemon# 
以上会提示初始化了一个空的仓库
切换到git仓库,查看有一个.git目录,如下:
root@weizhibiao:~/git-daemon# cd git-daemon-init/
root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 12
drwxr-xr-x 3 root root 4096 4月  25 22:53 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
drwxr-xr-x 7 root root 4096 4月  25 22:53 .git/
root@weizhibiao:~/git-daemon/git-daemon-init# 

3:add文件到仓库

root@weizhibiao:~/git-daemon/git-daemon-init# touch index.html
root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 12
drwxr-xr-x 3 root root 4096 4月  25 22:56 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
drwxr-xr-x 7 root root 4096 4月  25 22:53 .git/
-rw-r--r-- 1 root root    0 4月  25 22:56 index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master

No commits yet

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

	index.html

nothing added to commit but untracked files present (use "git add" to track)
root@weizhibiao:~/git-daemon/git-daemon-init# 
########################################
以上使用git status可以看到,新建的文件还能没有到add到本地
使用如下命令对文件进行追踪:
root@weizhibiao:~/git-daemon/git-daemon-init# git add index.html 
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master

No commits yet

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

	new file:   index.html

root@weizhibiao:~/git-daemon/git-daemon-init# 
有多个文件时,可以使用git add . 或者git add file1 file2进行添加,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# touch 1.txt
root@weizhibiao:~/git-daemon/git-daemon-init# touch 2.txt
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master

No commits yet

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

	new file:   index.html

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

	1.txt
	2.txt

root@weizhibiao:~/git-daemon/git-daemon-init# 
使用git add . 追踪所有新的文件,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git add .
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master

No commits yet

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

	new file:   1.txt
	new file:   2.txt
	new file:   index.html

root@weizhibiao:~/git-daemon/git-daemon-init# 

4: 取消追踪文件

当文件被追踪后,要取消时,可使用如下命令进行:
root@weizhibiao:~/git-daemon/git-daemon-init# git rm --cached 1.txt 
rm '1.txt'
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master

No commits yet

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

	new file:   2.txt
	new file:   index.html

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

	1.txt

root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 12
drwxr-xr-x 3 root root 4096 4月  25 22:59 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
-rw-r--r-- 1 root root    0 4月  25 22:59 1.txt
-rw-r--r-- 1 root root    0 4月  25 22:59 2.txt
drwxr-xr-x 7 root root 4096 4月  25 23:02 .git/
-rw-r--r-- 1 root root    0 4月  25 22:56 index.html
root@weizhibiao:~/git-daemon/git-daemon-init# 
################################################
git rm --cached file.txt 并非删除本地文件

5: git commit 对文件修改进行确认,提交代码

当系统中创建新的文件并进行add之后,首次进行commit,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "init repositry"
[master (root-commit) a02038c] init repositry
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.txt
 create mode 100644 index.html
root@weizhibiao:~/git-daemon/git-daemon-init# 
对其中一个文件进行修改之后再进行commit,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# echo "11111" > 2.txt 
root@weizhibiao:~/git-daemon/git-daemon-init# git add 2.txt 
root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "update 2.txt"
[master 306c065] update 2.txt
 1 file changed, 1 insertion(+)
root@weizhibiao:~/git-daemon/git-daemon-init# 
#########################
对git仓库的commit记录进行查看,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git log 
commit 306c065c3c5fe11f6c63911cc684582f0e060f3e (HEAD -> master)
Author: wei.zhibao <1010101001@163.com>
Date:   Sun Apr 25 23:09:53 2021 +0800

    update 2.txt

commit a02038c842580d2dce283ea028af2c7f02b95587
Author: wei.zhibao <1010101001@163.com>
Date:   Sun Apr 25 23:06:03 2021 +0800

    init repositry
root@weizhibiao:~/git-daemon/git-daemon-init# 
可以看到修改的信息以及时间和user
############################
多分支时可以使用如下命令查看日志,可显示具体的分支,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git log --graph
* commit 306c065c3c5fe11f6c63911cc684582f0e060f3e (HEAD -> master)
| Author: wei.zhibao <1010101001@163.com>
| Date:   Sun Apr 25 23:09:53 2021 +0800
| 
|     update 2.txt
| 
* commit a02038c842580d2dce283ea028af2c7f02b95587
  Author: wei.zhibao <1010101001@163.com>
  Date:   Sun Apr 25 23:06:03 2021 +0800
  
      init repositry
root@weizhibiao:~/git-daemon/git-daemon-init#  

6:文件修改内容对比

对文件输入内容,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# echo "1111" > index.htm
使用git diff 查看如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git diff
diff --git a/index.html b/index.html
index e69de29..5f2f16b 100644
--- a/index.html
+++ b/index.html
@@ -0,0 +1 @@
+1111
可以看到文件index.html添加了1111内容。
#########################################################
对另外一个文件添加内容,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# echo "2222" > 2.txt 
root@weizhibiao:~/git-daemon/git-daemon-init# 
root@weizhibiao:~/git-daemon/git-daemon-init# git diff
diff --git a/2.txt b/2.txt
index f7c6dd0..c7dc989 100644
--- a/2.txt
+++ b/2.txt
@@ -1 +1 @@
-11111
+2222
diff --git a/index.html b/index.html
index e69de29..5f2f16b 100644
--- a/index.html
+++ b/index.html
@@ -0,0 +1 @@
+1111
root@weizhibiao:~/git-daemon/git-daemon-init# git diff 2.txt
diff --git a/2.txt b/2.txt
index f7c6dd0..c7dc989 100644
--- a/2.txt
+++ b/2.txt
@@ -1 +1 @@
-11111
+2222
#################################
以上得知,使用git diff可以查看所有文件的修改,查看单独的文件可使用如下命令:
git diff file.txt
如果文件修改之后进行了git add操作,可以使用git diff --staged进行查看。
root@weizhibiao:~/git-daemon/git-daemon-init# git add .
root@weizhibiao:~/git-daemon/git-daemon-init# git diff 
root@weizhibiao:~/git-daemon/git-daemon-init# git diff --staged
diff --git a/2.txt b/2.txt
index f7c6dd0..c7dc989 100644
--- a/2.txt
+++ b/2.txt
@@ -1 +1 @@
-11111
+2222
diff --git a/index.html b/index.html
index e69de29..5f2f16b 100644
--- a/index.html
+++ b/index.html
@@ -0,0 +1 @@
+1111
,,,,,,注意,一旦使用了git commit,就不能使用git diff进行查看修改。
root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "update the index 2.txt"
[master 1835595] update the index 2.txt
 2 files changed, 2 insertions(+), 1 deletion(-)
root@weizhibiao:~/git-daemon/git-daemon-init# git diff --staged
root@weizhibiao:~/git-daemon/git-daemon-init# 

7: 文件的删除,移动,重命名

可以手动的删除,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# rm -rf 2.txt 
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    2.txt

no changes added to commit (use "git add" and/or "git commit -a")
root@weizhibiao:~/git-daemon/git-daemon-init# git add .
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    2.txt
######################################
但是需要进行git add . 或者git rm file,之后进行git commit

也可以直接进行git rm file进行删除文件,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 16
drwxr-xr-x 3 root root 4096 4月  26 22:20 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
drwxr-xr-x 8 root root 4096 4月  26 22:21 .git/
-rw-r--r-- 1 root root    5 4月  26 21:55 index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git rm index.html 
rm 'index.html'
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    index.html

root@weizhibiao:~/git-daemon/git-daemon-init# git add .
root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "delete index.html file "
[master 7b477e8] delete index.html file
 1 file changed, 1 deletion(-)
 delete mode 100644 index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git status
On branch master
nothing to commit, working tree clean
root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 12
drwxr-xr-x 3 root root 4096 4月  26 22:24 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
drwxr-xr-x 8 root root 4096 4月  26 22:25 .git/
root@weizhibiao:~/git-daemon/git-daemon-init# 
#########################################
#########################################
对文件进行改名,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 12
drwxr-xr-x 3 root root 4096 4月  26 22:26 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
-rw-r--r-- 1 root root    0 4月  26 22:25 1.txt
-rw-r--r-- 1 root root    0 4月  26 22:25 2.txt
drwxr-xr-x 8 root root 4096 4月  26 22:26 .git/
-rw-r--r-- 1 root root    0 4月  26 22:26 index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git mv 1.txt  test-file
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	renamed:    1.txt -> test-file

root@weizhibiao:~/git-daemon/git-daemon-init# git add .
root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "rename the 1.txt to test-file "
[master 6fdbdaf] rename the 1.txt to test-file
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 1.txt => test-file (100%)
root@weizhibiao:~/git-daemon/git-daemon-init# ll
total 12
drwxr-xr-x 3 root root 4096 4月  26 22:27 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
-rw-r--r-- 1 root root    0 4月  26 22:25 2.txt
drwxr-xr-x 8 root root 4096 4月  26 22:28 .git/
-rw-r--r-- 1 root root    0 4月  26 22:26 index.html
-rw-r--r-- 1 root root    0 4月  26 22:25 test-file
#########################################
将文件移动到其他目录,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git mv 2.txt rename/state.xml
root@weizhibiao:~/git-daemon/git-daemon-init# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	renamed:    2.txt -> rename/state.xml

root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "rename the file to state.xml"
[master 9da754a] rename the file to state.xml
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 2.txt => rename/state.xml (100%)
root@weizhibiao:~/git-daemon/git-daemon-init# ll rename/
total 8
drwxr-xr-x 2 root root 4096 4月  26 22:38 ./
drwxr-xr-x 4 root root 4096 4月  26 22:38 ../
-rw-r--r-- 1 root root    0 4月  26 22:25 state.xml

8:文件忽略

对不需要的文件进行忽略,具体步骤如下:
在git目录下创建.gitignore文件,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# touch .gitignore
root@weizhibiao:~/git-daemon/git-daemon-init# ll -a
total 16
drwxr-xr-x 4 root root 4096 4月  28 22:51 ./
drwxr-xr-x 3 root root 4096 4月  25 22:53 ../
drwxr-xr-x 8 root root 4096 4月  26 22:39 .git/
-rw-r--r-- 1 root root    0 4月  28 22:51 .gitignore
-rw-r--r-- 1 root root    0 4月  26 22:26 index.html
drwxr-xr-x 2 root root 4096 4月  26 22:38 rename/
-rw-r--r-- 1 root root    0 4月  26 22:25 test-file
添加文件爱,进行测试,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# touch test-ignore
root@weizhibiao:~/git-daemon/git-daemon-init# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore
	test-ignore

nothing added to commit but untracked files present (use "git add" to track)
root@weizhibiao:~/git-daemon/git-daemon-init# 
将test-ignore文件名添加到.gitignore文件中,如下;
root@weizhibiao:~/git-daemon/git-daemon-init# cat .gitignore 
test-ignore
再次查看git status,文件test-ignore已被忽略如下:
root@weizhibiao:~/git-daemon/git-daemon-init# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore

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

9:回退内容

当对一个文件修改之后,使用git status 查看有修改的内容
使用git checkout -- filename进行回退,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# echo "22222" >> index.html 
root@weizhibiao:~/git-daemon/git-daemon-init# git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
root@weizhibiao:~/git-daemon/git-daemon-init# git checkout -- index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git status
On branch master
nothing to commit, working tree clean
#########################################
从缓存区中撤销,如下:
root@weizhibiao:~/git-daemon/git-daemon-init# echo "3333" >> index.html 
root@weizhibiao:~/git-daemon/git-daemon-init# 
root@weizhibiao:~/git-daemon/git-daemon-init# 
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
root@weizhibiao:~/git-daemon/git-daemon-init# git add index.html 
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git reset HEAD index.html
Unstaged changes after reset:
M	index.html
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
##############################################
如果进行了commit操作之后,需要返回到上一个版本,操作如下:
root@weizhibiao:~/git-daemon/git-daemon-init# 
root@weizhibiao:~/git-daemon/git-daemon-init# git log --oneline
7172107 (HEAD -> master) delete test-file  ###当前版本
daf5ab3 add ignore file
9da754a rename the file to state.xml
6fdbdaf rename the 1.txt to test-file
5e2a357 add the file
7b477e8 delete index.html file
a62039e delete 2.txt
1835595 update the index 2.txt
306c065 update 2.txt
a02038c init repositry
root@weizhibiao:~/git-daemon/git-daemon-init# git reset HEAD^
Unstaged changes after reset:
M	index.html
D	test-file
root@weizhibiao:~/git-daemon/git-daemon-init# git log --oneline
daf5ab3 (HEAD -> master) add ignore file  ###回退之后的版本
9da754a rename the file to state.xml
6fdbdaf rename the 1.txt to test-file
5e2a357 add the file
7b477e8 delete index.html file
a62039e delete 2.txt
1835595 update the index 2.txt
306c065 update 2.txt
a02038c init repositry
root@weizhibiao:~/git-daemon/git-daemon-init#
######回退到上上版本可使用 git reset HEAD^^#######
#################################################
如果要回退到指定版本,可使用如下命令:
git reset --hard HEAD[hash号]  回退到指定的hash版本
root@weizhibiao:~/git-daemon/git-daemon-init# git log --oneline
daf5ab3 (HEAD -> master) add ignore file
9da754a rename the file to state.xml
6fdbdaf rename the 1.txt to test-file
5e2a357 add the file
7b477e8 delete index.html file
a62039e delete 2.txt
1835595 update the index 2.txt
306c065 update 2.txt
a02038c init repositry
root@weizhibiao:~/git-daemon/git-daemon-init# git reset --hard 5e2a357
HEAD is now at 5e2a357 add the file
root@weizhibiao:~/git-daemon/git-daemon-init# git log --oneline
5e2a357 (HEAD -> master) add the file
7b477e8 delete index.html file
a62039e delete 2.txt
1835595 update the index 2.txt
306c065 update 2.txt
a02038c init repositry
###########需要注意的是,回退到相应版本后,之前版本会被删除######
###########回退到旧版本,之前的版本还在,如下
root@weizhibiao:~/git-daemon/git-daemon-init# git log --oneline
5e2a357 (HEAD -> master) add the file
7b477e8 delete index.html file
a62039e delete 2.txt
1835595 update the index 2.txt
306c065 update 2.txt
a02038c init repositry
root@weizhibiao:~/git-daemon/git-daemon-init# git checkout a62039e -- .
root@weizhibiao:~/git-daemon/git-daemon-init# git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   index.html
此时已经回退到相应的版本,进行commit操作生成新的版本
root@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "recovery to old version"
[master 67aaa4e] recovery eo old version
 1 file changed, 1 insertion(+)
root@weizhibiao:~/git-daemon/git-daemon-init# git log --oneline
67aaa4e (HEAD -> master) recovery eo old version
5e2a357 add the file
7b477e8 delete index.html file
a62039e delete 2.txt
1835595 update the index 2.txt
306c065 update 2.txt
a02038c init repositry

10、分支管理

(1)查看当前分支
[root@k8s-master-1 daemon]# git branch
* master
以上可以看到只有默认的master分支
####################################
(2)创建新的devops分支,如下:
[root@k8s-master-1 daemon]# git branch devops
[root@k8s-master-1 daemon]# git branch
  devops
* master
再次查看多了一个devops分支。
#####################################
(3)切换分支,如下:
[root@k8s-master-1 daemon]# git checkout devops
Switched to branch 'devops'
[root@k8s-master-1 daemon]# git branch
* devops
  master
查看当前分支为devops.
######################################
(4)创建以及切换分支,如下:
[root@k8s-master-1 daemon]# git checkout -b slave 
Switched to a new branch 'slave'
[root@k8s-master-1 daemon]# git branch 
  devops
  master
* slave
查看得知目前分支为slave。
################################
(5)删除分支,如下:
[root@k8s-master-1 daemon]# git checkout master
Switched to branch 'master'
[root@k8s-master-1 daemon]# git branch 
  devops
* master
  slave
[root@k8s-master-1 daemon]# git branch -d slave
Deleted branch slave (was 64a85f0).
[root@k8s-master-1 daemon]# git branch 
  devops
* master
    首先需要切换到其他分支才能删除此分支,强制删除用-D。
##############################################
(6)合并分支
在master分支创建index.html文件,如下:
[root@k8s-master-1 test-daemon]# touch index.html
[root@k8s-master-1 test-daemon]# git add index.html 
[root@k8s-master-1 test-daemon]# git commit -m "add the file index.html"
切换devops分支,创建devops.txt文件,如下:
[root@k8s-master-1 test-daemon]# git checkout devops
Switched to branch 'devops'
[root@k8s-master-1 test-daemon]# touch devops.txt
[root@k8s-master-1 test-daemon]# echo "wsx" >> devops.txt
[root@k8s-master-1 test-daemon]# git add .
[root@k8s-master-1 test-daemon]# git commit -m "add the file devops.txt"
[devops ef052a7] add the file devops.txt
 1 file changed, 1 insertion(+)
 create mode 100644 devops.txt
[root@k8s-master-1 test-daemon]# git checkout master
Switched to branch 'master'
[root@k8s-master-1 test-daemon]# git merge devops
Updating 879b3af..ef052a7
Fast-forward
 devops.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 devops.txt
注意:
当分支内容冲突时,可以使用git merge branch_name --abort(撤销合并,内容不会变化)
修改冲突之后进行git commit进行合并,如下:
切换devops分支进行修改代码,如下:
root@weizhibiao:~/merge# vim 2.txt
root@weizhibiao:~/merge# cat 2.txt 
{
    <title == devops3>
}
root@weizhibiao:~/merge# git commit -am "devops update the 2.txt"
[devops 819a649] devops update the 2.txt
 1 file changed, 1 insertion(+), 1 deletion(-)
root@weizhibiao:~/merge#
切换master分支,进行修改2.txt代码如下:
root@weizhibiao:~/merge# cat 2.txt 
{
    <title == devops2>
}
root@weizhibiao:~/merge# git add 2.txt 
root@weizhibiao:~/merge# git commit -m "master udpate to devops3"
[master e533019] master udpate to devops3
 1 file changed, 1 insertion(+), 1 deletion(-)
进行分支合并,会出现如下错误提示:
root@weizhibiao:~/merge# git branch 
  devops
* master
root@weizhibiao:~/merge# git merge devops 
Auto-merging 2.txt
CONFLICT (content): Merge conflict in 2.txt
Automatic merge failed; fix conflicts and then commit the result.
root@weizhibiao:~/merge# 

通过git status查看错误提示,如下:
root@weizhibiao:~/merge# git status 
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

	modified:   index.html

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   2.txt
查看2.txt文件,内容如下:
root@weizhibiao:~/merge# cat 2.txt 
{
<<<<<<< HEAD
    <title == devops3>
=======
    <title == devops2>
>>>>>>> devops
      }
以上可知相同地方内容冲突,解决方法如下:
(1):使用git merge --abort  回退合并,修改代码之后进行重新提交并合并
root@weizhibiao:~/merge# git merge --abort
root@weizhibiao:~/merge# git status 
On branch master
nothing to commit, working tree clean
(2):解决冲突,如下:
编辑文件,选择留下内容(此处选择两者都留下),如下:
root@weizhibiao:~/merge# cat 2.txt 
{
    <title == devops3>
    <title == devops2>
      }
root@weizhibiao:~/merge# git add .
root@weizhibiao:~/merge# git commit -m "fix merge CONFLICT"

11、克隆代码

使用git clone直接clone代码,如下:
[root@k8s-master-1 git]# git clone http://192.168.1.94:48080/root/daemon.git
Cloning into 'daemon'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
######################################
如果该仓库有多个分支,可以使用git clone --no-checkout 快速clone,不切换master分支
但是克隆之后内容为空,之后手动切换分支,如下:
[root@k8s-master-1 git]# git clone --no-checkout http://192.168.1.94:48080/root/daemon.git
Cloning into 'daemon'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@k8s-master-1 git]# ll
total 0
drwxr-xr-x 3 root root 18 May 10 15:42 daemon
[root@k8s-master-1 git]# cd daemon/
[root@k8s-master-1 daemon]# ll
total 0
[root@k8s-master-1 daemon]# git checkout master   ###手动切换一下分支
Already on 'master'
[root@k8s-master-1 daemon]# ll
total 4
-rw-r--r-- 1 root root 10 May 10 15:42 README.md
[root@k8s-master-1 daemon]# 

12:上传代码到远端仓库
首先在远端建立gitlab仓库,如下:
在这里插入图片描述
在这里插入图片描述
本地仓库上传,步骤如下:

(1):查看远端仓库,如下:
[root@k8s-master-1 daemon]# git remote 
[root@k8s-master-1 daemon]# 
(2):本地仓库推送至远端,如下:
[root@k8s-master-1 deamon]# git remote  add origin http://192.168.1.94:48080/root/daemon.git
[root@k8s-master-1 deamon]# git remote 
origin
[root@k8s-master-1 deamon]# git push -u origin master
Username for 'http://192.168.1.94:48080': root
Password for 'http://root@192.168.1.94:48080': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.1.94:48080/root/daemon.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

登录gitlab查看,index.html已经被上传,如下:
在这里插入图片描述
当远端仓库内容被修改之后,本地仓库如果没有被更新,再次上传回失败,具体情况如下:
在gitlab添加内容如下:
在这里插入图片描述
此时本地再进行上传时会报错,如下:

[root@k8s-master-1 deamon]# 
[root@k8s-master-1 deamon]# git push -u origin master
Username for 'http://192.168.1.94:48080': root
Password for 'http://root@192.168.1.94:48080': 
To http://192.168.1.94:48080/root/daemon.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'http://192.168.1.94:48080/root/daemon.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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
###########################
此时需要先进行pull,更新本地内容,如下:
[root@k8s-master-1 deamon]# 
[root@k8s-master-1 deamon]# git pull 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From http://1.68.5.94:48080/root/daemon
   fd83ae4..7098e32  master     -> origin/master
Updating fd83ae4..7098e32
Fast-forward
 index.html | 1 +
 1 file changed, 1 insertion(+)
[root@k8s-master-1 deamon]# cat index.html 
111111
22222[root@k8s-master-1 deamon]# 
再次编辑内容后,上传,如下:
[root@k8s-master-1 deamon]# echo "33333" >> index.html 
[root@k8s-master-1 deamon]# cat index.html 
111111
2222233333
[root@k8s-master-1 deamon]# git add index.html 
[root@k8s-master-1 deamon]# git commit -m "add index.html 33333"
[master 903fcca] add index.html 33333
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@k8s-master-1 deamon]# git log --oneline 
903fcca add index.html 33333
7098e32 add index.html  2222
fd83ae4 first commit
再次上传,如下:
[root@k8s-master-1 deamon]# git push -u origin master
Username for 'http://192.168.1.94:48080': root
Password for 'http://root@192.168.1.94:48080': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 257 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.1.94:48080/root/daemon.git
   7098e32..903fcca  master -> master
Branch master set up to track remote branch master from origin.
[root@k8s-master-1 deamon]# 
注:
git pull命令等于git fetch + git merge。git fetch先把内容下载到缓存区,之后进行git merge进行合并

在这里插入图片描述
13:删除远端分支:
在gitlab创建新的分支,如下:

在这里插入图片描述
在这里插入图片描述
在命令行操作,删除远端分支,如下:

首先在本地pull一下远端的分支,如下:
[root@k8s-master-1 deamon]# git pull 
From http://192.168.1.94:48080/root/daemon
 * [new branch]      test-push-delete -> origin/test-push-delete
Already up-to-date.
[root@k8s-master-1 deamon]# git checkout test-push-delete
Branch test-push-delete set up to track remote branch test-push-delete from origin.
Switched to a new branch 'test-push-delete'
[root@k8s-master-1 deamon]# git branch 
  master
* test-push-delete
[root@k8s-master-1 deamon]# git checkout master
Switched to branch 'master'
删除远端分支test-push-delete,如下:
[root@k8s-master-1 deamon]# git push origin --delete test-push-delete 
Username for 'http://192.168.1.94:48080': root
Password for 'http://root@192.168.1.94:48080': 
To http://192.168.1.94:48080/root/daemon.git
 - [deleted]         test-push-delete
[root@k8s-master-1 deamon]# 

gitlab界面查看,分支已经被删除,如下:
在这里插入图片描述
14、仓库迁移
在gitlab上在创建一个名为migrate的仓库,如下:
在这里插入图片描述
在这里插入图片描述
本地命令行进行迁移,如下:

[root@k8s-master-1 deamon]# git remote -v
origin	http://192.168.1.94:48080/root/daemon.git (fetch)
origin	http://192.168.1.94:48080/root/daemon.git (push)
[root@k8s-master-1 deamon]# 
[root@k8s-master-1 deamon]# git remote set-url origin  http://192.168.1.94:48080/root/migrate.git
[root@k8s-master-1 deamon]# git remote -v
origin	http://192.168.1.94:48080/root/migrate.git (fetch)
origin	http://192.168.1.94:48080/root/migrate.git (push)
[root@k8s-master-1 deamon]# 
以上发现origin名字发生变化,变为migrate
接下来进行git push提交,如下:
[root@k8s-master-1 deamon]# git push --all
Username for 'http://192.168.1.94:48080': root
Password for 'http://root@192.168.1.94:48080': 
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (11/11), 959 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To http://192.168.1.94:48080/root/migrate.git
   230e52d..2647045  master -> master

登录gitlab查看migrate已经有了内容,如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值