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 masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)index.htmlnothing 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 masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: index.htmlroot@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 masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: index.htmlUntracked files:(use "git add <file>..." to include in what will be committed)1.txt2.txtroot@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 masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: 1.txtnew file: 2.txtnew file: index.htmlroot@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 masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: 2.txtnew file: index.htmlUntracked files:(use "git add <file>..." to include in what will be committed)1.txtroot@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 repositry2 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 2.txtcreate 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.txt1 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 +0800update 2.txtcommit a02038c842580d2dce283ea028af2c7f02b95587
Author: wei.zhibao <1010101001@163.com>
Date: Sun Apr 25 23:06:03 2021 +0800init 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 a02038c842580d2dce283ea028af2c7f02b95587Author: wei.zhibao <1010101001@163.com>Date: Sun Apr 25 23:06:03 2021 +0800init 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.txt2 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.txtno 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.htmlroot@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 file1 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-fileroot@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-file1 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.xmlroot@weizhibiao:~/git-daemon/git-daemon-init# git commit -m "rename the file to state.xml"
[master 9da754a] rename the file to state.xml1 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).gitignoretest-ignorenothing 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).gitignorenothing 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.htmlno 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.htmlno 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.htmlno 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 version1 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 branchdevops
* master
再次查看多了一个devops分支。
#####################################
(3)切换分支,如下:
[root@k8s-master-1 daemon]# git checkout devops
Switched to branch 'devops'
[root@k8s-master-1 daemon]# git branch
* devopsmaster
查看当前分支为devops.
######################################
(4)创建以及切换分支,如下:
[root@k8s-master-1 daemon]# git checkout -b slave
Switched to a new branch 'slave'
[root@k8s-master-1 daemon]# git branch devopsmaster
* slave
查看得知目前分支为slave。
################################
(5)删除分支,如下:
[root@k8s-master-1 daemon]# git checkout master
Switched to branch 'master'
[root@k8s-master-1 daemon]# git branch devops
* masterslave
[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.txt1 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-forwarddevops.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.txt1 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 devops31 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.htmlUnmerged 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/daemonfd83ae4..7098e32 master -> origin/master
Updating fd83ae4..7098e32
Fast-forwardindex.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 333331 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.git7098e32..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.git230e52d..2647045 master -> master
登录gitlab查看migrate已经有了内容,如下:
git基础命令以及用法相关推荐
- Mongodb基础命令与用法
Mongodb基础命令与用法 查看版本号 [root@VM_0_12_centos bin]# ./mongo -version MongoDB shell version v3.6.5 git ve ...
- 工具箱@git基础命令上手指南
Git下载及安装 https://git-scm.com/downloads 基本认知 1.工作区,暂存区,本地仓库,远程仓库 1.默认配置即可 git config --global user.na ...
- Linux:搭建GIT服务,Linux中使用git,git基础命令,和原理
目录 GIT GIT安装 Git概念 Git使用 Git的文件分类 提交代码 Git提交代码总结 增加补充代码 diff 查看纤细修改内容 HEAD 检出[checkout] 重置[res ...
- Git学习5:Git常用命令简明用法
不要使用git commit -a 该命令可以对本地所有的变更文件(包括对本地修改和删除的文件)执行提交操作,但是不包括未被版本库跟踪的文件. git命令补充说明 显示.git目录所在的位置 git ...
- git clean命令的用法
git clean命令用来从你的工作目录中删除所有没有tracked过的文件. git clean经常和git reset --hard一起结合使用. 记住reset只影响被track过的文件, 所以 ...
- 【Git】Git 基础命令 ( 查看提交记录 git log | 版本回滚 git reset | 撤销回滚 git reflog )
文章目录 一.查看提交记录 git log 1.查看详细提交记录 2.查看简略提交记录 二.版本回滚 git reset 1.Git 中的版本表示 HEAD~1 2.版本库代码回滚 三.撤销回滚 1. ...
- 【Git】Git 基础命令 ( 添加暂存文件 git add | 提交文件至版本库 git commit | 查看版本库状态 git status | 查询文件修改 git diff )
文章目录 一.添加暂存文件 git add 二.提交文件至版本库 git commit 三.查看版本库状态 git status 四.查询文件修改 git diff 一.添加暂存文件 git add ...
- 【Git】Git 基础命令 ( Git 版本库概念 | 创建版本库 git init | 克隆版本库 git clone )
文章目录 一.Git 版本库概念 二.创建版本库 git init 三.克隆版本库 git clone 一.Git 版本库概念 Git 版本库概念 : Git 版本库 Repository 又称为 G ...
- Git基础命令(一)
1.Git 命令列表 判断git是否安装成功:输入git(所有git命令以git开头): 2.Git 具体命令 第一步:新建一个文件夹 mkdir demo cd demo touch demo.md ...
- git stash命令的用法
stash的字面意思:隐藏,储藏 当我们以多人协同工作的方式基于同一个github仓库进行开发时,免不了遇到多人同时在本机对同一文件进行编辑的情况出现. 看一个具体的场景,当我使用git pull时, ...
最新文章
- 基于Pyhton的图像隐写术--如何隐藏图像中的数据
- 关于linux cp命令的一d参数
- 王重敏的“中国主义”
- 15个基本的C#面试问题
- 属于你们的“礼仪小课堂”
- 巧用windows xp远程桌面web连接
- python的本质是什么意思_python生成器指的是什么意思
- 我自己的 psftp-cmd
- 【FlinkX】两个issue分析:reader和writer的通道数不一致+获取JobId
- 蓝桥真题,跑步问题c语言解决方案
- 计算机访问小米摄像机,小米摄像头连接教程
- 狂神说Swagger笔记
- Quorum NWR算法
- 基础C语言知识串串香10☞数组字符串结构体联合体枚举
- 量化人才之战如何取胜
- EastWave应用案例:同轴线仿真
- pdf解密工具(超实用)
- 用K-Means算法处理wine数据集和wine_quality数据集
- c语言获取终端输入字符串的函数scanf,gets,fgets
- python绘制分形图形教程_python-图形绘制(1)-turtle-递归-分形几何美学-分形树