单机使用Git(续代码管理平台)
进入myproject目录
[root@localhost ~]# svnadmin create /data/svnroot/myproject/
[root@localhost ~]# cd !$
cd /data/svnroot/myproject/
安装git
[root@localhost myproject]# yum install -y git
[root@localhost myproject]# git config --global user.name "lsk" //配置基本信息
[root@localhost myproject]# git config --global user.email "example@qq.com"
创建目录进去
[root@localhost myproject]# mkdir /data/gitroot
[root@localhost myproject]# cd /data/gitroot
[root@localhost gitroot]# git init //初始化仓库
Initialized empty Git repository in /data/gitroot/.git/
[root@localhost gitroot]# ll -a
total 0
drwxr-xr-x. 3 root root 18 Mar 3 14:48 .
drwxr-xr-x. 4 root root 36 Mar 3 14:47 ..
drwxr-xr-x. 7 root root 119 Mar 3 14:48 .git
[root@localhost gitroot]#
[root@localhost gitroot]# ls
[root@localhost gitroot]# echo "hello world" > 1.txt //创建一个新文件
[root@localhost gitroot]# git add 1.txt //把1.txt添加到仓库
[root@localhost gitroot]# git commit -m "add new file 1.txt" //add完了必须要commit才算真正把文件提交到git仓库里
[master (root-commit) bf068c3] add new file 1.txt
1 file changed, 1 insertion(+)
create mode 100644 1.txt
[root@localhost gitroot]# cat 1.txt 看一下文件的内容
hello world
[root@localhost gitroot]#
[root@localhost gitroot]# echo "new" >> 1.txt 追加新的内容
[root@localhost gitroot]#
[root@localhost gitroot]# cat 1.txt 再查看下
hello world
new
[root@localhost gitroot]#
[root@localhost gitroot]# git status 1.txt 查看仓库的状态,例如是否有文件改动
# 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: 1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost gitroot]# git diff 1.txt 对比仓库里的版本,修改了什么内容
diff --git a/1.txt b/1.txt
index 3b18e51..3a4b9d0 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
hello world
+new 这里的+ 号代表刚刚新添加的
[root@localhost gitroot]# echo "1" >> 1.txt
[root@localhost gitroot]# cat 1.txt
hello world
new
1
[root@localhost gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index 3b18e51..49a10d3 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,3 @@
hello world
+new
+1
版本回退
##多更改几次1.txt,然后add,commit
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git commit -m "1.txt change 1"
[master d0db786] 1.txt change 1
1 file changed, 2 insertions(+)
[root@localhost gitroot]# echo > 1.txt
[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git commit -m "1.txt change 1"
[master 29cc729] 1.txt change 1
1 file changed, 1 insertion(+), 3 deletions(-)
[root@localhost gitroot]# cat 1.txt //没有内容
[root@localhost gitroot]#
[root@localhost gitroot]# git log //查看历史记录(所有提交记录)
commit 29cc7293dfed8e45f41919e5e1549c106fdce1bd
Author: lsk <example@qq.com>
Date: Wed Mar 3 15:14:27 2021 +0800
1.txt change 1 //所写的描述
commit d0db786c9f22f15e25f7463f59fbd1e5aab44dbb
Author: lsk <example@qq.com>
Date: Wed Mar 3 15:13:18 2021 +0800
1.txt change 1
commit bf068c39f2e0d875da71711238ae71dfb5877855
Author: lsk <example@qq.com>
Date: Wed Mar 3 14:56:44 2021 +0800
add new file 1.txt
[root@localhost gitroot]# git log --pretty=oneline //一行显示
29cc7293dfed8e45f41919e5e1549c106fdce1bd 1.txt change 1
d0db786c9f22f15e25f7463f59fbd1e5aab44dbb 1.txt change 1
bf068c39f2e0d875da71711238ae71dfb5877855 add new file 1.txt
[root@localhost gitroot]# git reset --hard bf068c39f2e0
HEAD is now at bf068c3 add new file 1.txt
[root@localhost gitroot]# cat 1.txt
hello world
删除后再回退到之前的版本
[root@localhost gitroot]# rm -f 1.txt
[root@localhost gitroot]# ls
[root@localhost gitroot]# git checkout -- 1.txt
[root@localhost gitroot]# ls
1.txt
[root@localhost gitroot]# cat 1.txt
hello world
[root@localhost gitroot]# git log
commit bf068c39f2e0d875da71711238ae71dfb5877855
Author: lsk <example@qq.com>
Date: Wed Mar 3 14:56:44 2021 +0800
add new file 1.txt
[root@localhost gitroot]# git reflog //查看所有历史版本
bf068c3 HEAD@{0}: reset: moving to bf068c39f2e0
29cc729 HEAD@{1}: commit: 1.txt change 1
d0db786 HEAD@{2}: commit: 1.txt change 1
bf068c3 HEAD@{3}: commit (initial): add new file 1.txt
[root@localhost gitroot]# git reset --hard 29cc729 //回退版本,其中后面跟的字符串是简写
HEAD is now at 29cc729 1.txt change 1
[root@localhost gitroot]#
[root@localhost gitroot]# git log
commit 29cc7293dfed8e45f41919e5e1549c106fdce1bd
Author: lsk <example@qq.com>
Date: Wed Mar 3 15:14:27 2021 +0800
1.txt change 1
commit d0db786c9f22f15e25f7463f59fbd1e5aab44dbb
Author: lsk <example@qq.com>
Date: Wed Mar 3 15:13:18 2021 +0800
1.txt change 1
commit bf068c39f2e0d875da71711238ae71dfb5877855
Author: lsk <example@qq.com>
Date: Wed Mar 3 14:56:44 2021 +0800
add new file 1.txt
[root@localhost gitroot]# git log --pretty=oneline
29cc7293dfed8e45f41919e5e1549c106fdce1bd 1.txt change 1
d0db786c9f22f15e25f7463f59fbd1e5aab44dbb 1.txt change 1
bf068c39f2e0d875da71711238ae71dfb5877855 add new file 1.txt
删除文件
[root@localhost gitroot]# echo -e "111111\n222222" >2.txt 添加新文件2.txt
[root@localhost gitroot]# ls
1.txt 2.txt
[root@localhost gitroot]# cat 2.txt 查看文件内容
111111
222222
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m "new file 2.txt"
[master 88cde82] new file 2.txt
1 file changed, 2 insertions(+)
create mode 100644 2.txt
[root@localhost gitroot]# git rm 2.txt 删除
rm '2.txt'
[root@localhost gitroot]# ls
1.txt
[root@localhost gitroot]# git commit -m "rm 2.txt" 提交确认
[master c04e138] rm 2.txt
1 file changed, 2 deletions(-)
delete mode 100644 2.txt
[root@localhost gitroot]# git log --pretty=oneline 一行显示
c04e1380641d727ae3df2004bc31cecf575f5ab1 rm 2.txt
88cde82f95d98d07f1f4b8a99534aaf2d77326be new file 2.txt
29cc7293dfed8e45f41919e5e1549c106fdce1bd 1.txt change 1
d0db786c9f22f15e25f7463f59fbd1e5aab44dbb 1.txt change 1
bf068c39f2e0d875da71711238ae71dfb5877855 add new file 1.txt
Git远程仓库
建立远程仓库
在浏览器中:https://github.com 注册一个账号
[root@localhost gitroot]# ssh-keygen 生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xtWX2h1ZI+sOLqSbz9ODR4ilFlakSLefzEh2xlxkTC4 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| . ...+= . ..|
| . o =.+o o.+|
| . =.E....oo |
| o+Boo .+ ..|
| ..S*..... .|
| =o...o |
| .. .+. . |
| +o.+ |
| o.oo . |
+----[SHA256]-----+
[root@localhost gitroot]# cat /root/.ssh/id_rsa 查看密钥
id_rsa id_rsa.pub
[root@localhost gitroot]# cat /root/.ssh/id_rsa.pub
把cat /root/.ssh/id_rsa.pub的密钥复制上去
创建仓库
单击“Create repository”按钮
连接的方式为ssh
把本地仓库推送到远程仓库
[root@localhost gitroot]# git remote add origin git@github.com:toffee2001/demo1.git
[root@localhost gitroot]# ls
1.txt
把本地的giroot仓库推送到远程的gitroot下一次再推送
[root@localhost ~]# git push -u origin master
##去网页查看一下,远程仓库会多一个1.txt
[root@localhost gitroot]# ls
1.txt
[root@localhost gitroot]# cd /opt/
c
[root@localhost opt]# git clone git@github.com:toffee2001/demo1.git
[root@localhost opt]# ls
demo1 myproject
[root@localhost opt]# cd demo1/
[root@localhost demo1]# ls
1.txt
[root@localhost demo1]# ll -a
total 0
drwxr-xr-x. 3 root root 18 Mar 3 14:48 .
drwxr-xr-x. 4 root root 36 Mar 3 14:47 ..
-rw-r--r--. 4 root root 1 Mar 3 14:47 1.txt
drwxr-xr-x. 7 root root 119 Mar 3 14:48 .git
错误一
fatal: Not a git repository (or any of the parent directories): .git
##解决方法
[root@localhost ~]# git init
Initialized empty Git repository in /root/.git/
错误二
[root@localhost ~]# git push -u origin master
ssh: Could not resolve hostname github.com: Name or service not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
解决方法
ssh-keygen -t rsa -C 1398499734@qq.com
[root@localhost demo1]# echo "gitroot" < gitroot.sh
[root@localhost demo1]# ls
1.txt gitroot.sh
[root@localhost demo1]# cat gitroot.sh
gitroot
[root@localhost demo1]# git add gitroot.sh
[root@localhost demo1]# git commit -m "new file gitroot"
[master 88cde82] new file 2.txt
1 file changed, 1 insertions(+)
create mode 100644 gitroot.sh
[root@localhost demo1]# git push
##去网页查看一下,远程仓库会多一个gitroot.sh
[root@localhost demo1]# cat gitroot.sh
gitroot
##可以看见网页和此处的内容都是gitroot
Git分支
进入目录
[root@localhost demo1]# cd
[root@localhost ~]# cd /data/
gitroot/ svnroot/
[root@localhost ~]# cd /data/gitroot/
查看分支
[root@localhost gitroot]# git branch
* master
创建分支
[root@localhost gitroot]# git branch lsk
查看一下
[root@localhost gitroot]# git branch
lsk
* master
切换到了lsk分支下
[root@localhost gitroot]# git checkout lsk
Switched to branch 'lsk'
[root@localhost gitroot]# git branch
* lsk
master
[root@localhost gitroot]# ls
1.txt
编辑新的文件
[root@localhost gitroot]# echo "askd" > 2.txt
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m "new file 2.txt"
[lsk 88cde82] new file 2.txt
1 file changed, 1 insertions(+)
create mode 100644 2.txt
[root@localhost gitroot]# ls
1.txt 2.txt
//再用git branch查看,会看到有两个分支master和lsk,当前使用的分支前面会有一个*在lsk分支下 ,编辑2.txt,并提交到新分支
[root@localhost gitroot]# git checkout lsk
Switched to branch 'lsk'
[root@localhost gitroot]# git branch
lsk
* master
[root@localhost gitroot]# ls
1.txt
分支的合并
[root@localhost gitroot]# git merge lsk 合并分区(把lsk分支合并到了master)
Updating 4ab5058..89b88e8
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
[root@localhost gitroot]# ls
1.txt 2.txt
合并冲突
[root@localhost gitroot]# echo "2" >> 2.txt 追加新内容
[root@localhost gitroot]# git checkout lsk 切换到lsk分区
M 2.txt
Switched to branch 'lsk'
[root@localhost gitroot]# echo "1" >> 2.txt
[root@localhost gitroot]# git add 2.txt 添加提交
[root@localhost gitroot]# git commit -m "new file 2.txt"
[lsk 88cde82] new file 2.txt
1 file changed, 2 insertions(+)
[root@localhost gitroot]# cat 2.txt
asda
2
1
[root@localhost gitroot]# git branch 查看是否在master分区下
lsk
* master
[root@localhost gitroot]# cat 2.txt
asda
[root@localhost gitroot]# echo add 2 >> 2.txt
[root@localhost gitroot]# cat 2.txt
asda
add 2
[root@localhost gitroot]# git add 2.txt 添加提交
[root@localhost gitroot]# git commit -m "new file 2.txt"
[lsk 88cde82] new file 2.txt
1 file changed, 1 insertions(+)
[root@localhost gitroot]# cat 2.txt
asda
add 2
[root@localhost gitroot]# git merge lsk 合并失败
合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支
[root@localhost gitroot]# vi 2.txt 编辑文件,改为一样的
2
1
[root@localhost gitroot]# git add 2.txt 再次提交确定
[root@localhost gitroot]# git commit -m "2.txt"
[root@localhost gitroot]# git merge lsk 合并成功
Already up-to-date.
分支删除
[root@localhost gitroot]# git branch -d lsk
Deleted branch lsk (was 89b88e8).
[root@localhost gitroot]# git branch -D lsk -D强制删除
使用分支的原则:
① master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
② 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master。
③ 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己PC上)里面开发代码,然后合并到dev分支。
远程分支
##查看远程分支
[root@localhost gitroot]# git ls-remote origin
git push分支分两种情况:
① 当本地分支和远程分支一致时
git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用git push origin branch-name命令。
② 当本地分支比远程分支多
默认git push只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name命令如果推送失败,先用git pull抓取远程的新提交。
[root@localhost gitroot]# cd /mnt/
[root@localhost mnt]# git clone git@github.com:toffee2001/demo1.git
[root@localhost mnt]# ls
demo1
[root@localhost mnt]# cd demo1/
[root@localhost demo1]# ll -a
total 0
drwxr-xr-x. 3 root root 18 Mar 3 14:48 .
drwxr-xr-x. 4 root root 36 Mar 3 14:47 ..
-rw-r--r--. 4 root root 1 Mar 3 14:47 1.txt
drwxr-xr-x. 7 root root 119 Mar 3 14:48 .git
-rw-r--r--. 4 root root 1 Mar 3 14:47 gitroot.sh
[root@localhost demo1]# git branch
* master
git clone的时候默认只把master分支克隆下来。如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name命令,需要注意本地和远程分支的名称要一致
```bash
[root@localhost demo1]# git checkout -b demo origin/demo
[root@localhost demo1]# git branch
* demo
master