【12.23】代码管理平台 git
22.5/22.6 单机上使用git
1、安装
[root@arslinux-01 ~]# yum install -y git
2、创建 gitroot目录
[root@arslinux-01 ~]# mkdir /data/gitroot/
[root@arslinux-01 ~]# cd !$
cd /data/gitroot/
[root@arslinux-01 gitroot]#
3、初始化仓库
[root@arslinux-01 gitroot]# git init
初始化空的 Git 版本库于 /data/gitroot/.git/
[root@arslinux-01 gitroot]# ll -a
总用量 0
drwxrwxr-x 3 root root 18 7月 23 22:18 .
drwxr-xr-x 11 root root 135 7月 23 22:18 ..
drwxrwxr-x 7 root root 119 7月 23 22:18 .git
4.在库中创建一个文件,任意内容
[root@arslinux-01 gitroot]# echo "dasfdafasfafas" > 1.txt
[root@arslinux-01 gitroot]# echo "ddfdfdfw22ghghg" >> 1.txt
[root@arslinux-01 gitroot]# echo "dftyuoiuoryfhgjg" >> 1.txt
5、将文件添加到仓库(方法几乎和 svn 一致)
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@arslinux-01.(none)')
6、上传时出错,根据参考内容,设置邮箱和用户名
[root@arslinux-01 gitroot]# git config --global user.email "xxxxxx@qq.com"
[root@arslinux-01 gitroot]# git config --global user.name "arslinux"
也可以去 /root/.gitconfig 下更改
7、重新做上传
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master(根提交) f04992b] add 1.txt
1 file changed, 3 insertions(+)
create mode 100644 1.txt
8、更改 1.txt 内容
[root@arslinux-01 gitroot]# echo "888888" >> 1.txt
[root@arslinux-01 gitroot]# echo "777777" >> 1.txt
9、修改的内容如果提交上传,还需要操作 git add 和 git commit -a
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master 03d2ada] add 1.txt
1 file changed, 2 insertions(+)
10、查看当前仓库中的状态,比如是否有改动的文件
[root@arslinux-01 gitroot]# git status
# 位于分支 master
无文件要提交,干净的工作区
11、如果没有 git add 和 git commit 的步骤,会提示
[root@arslinux-01 gitroot]# echo " dasfadsf a" >> 1.txt
[root@arslinux-01 gitroot]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: 1.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
12、对比修改了哪些内容可以用 git diff
[root@arslinux-01 gitroot]# git diff
diff --git a/1.txt b/1.txt
index 03086e7..e51c67f 100644
--- a/1.txt
+++ b/1.txt
@@ -3,3 +3,4 @@ ddfdfdfw22ghghg
dftyuoiuoryfhgjg
888888
777777
+ dasfadsf a
13、再做几次操作,查看所有提交记录
[root@arslinux-01 gitroot]# vim 1.txt
// 增加一行
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master 3517c53] add 1.txt
1 file changed, 2 insertions(+)
[root@arslinux-01 gitroot]# vim 1.txt
// 删除一行
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "ch 1.txt"
[master b68b8fb] ch 1.txt
1 file changed, 1 deletion(-)
- 查看提交记录
[root@arslinux-01 gitroot]# git log
commit b68b8fb900160e2ab06b4204d97f659fa14fa26a
Author: arslinux <xxxxxx@qq.com>
Date: Tue Jul 23 22:45:24 2019 +0800
ch 1.txt
commit 3517c53c22f14c345206324bf9d190f157ec1f83
Author: arslinux <xxxxxx@qq.com>
Date: Tue Jul 23 22:44:55 2019 +0800
add 1.txt
commit 03d2adacc5ee700de75799b90c1534ad6d9e6b9a
Author: arslinux <xxxxxx@qq.com>
Date: Tue Jul 23 22:36:19 2019 +0800
add 1.txt
commit f04992b2e67da3ac3bfbc1c6a21053312f27ac76
Author: arslinux <xxxxxx@qq.com>
Date: Tue Jul 23 22:29:07 2019 +0800
add 1.txt
- 只显示一行信息 git log --pretty=oneline
[root@arslinux-01 gitroot]# git log --pretty=oneline
b68b8fb900160e2ab06b4204d97f659fa14fa26a ch 1.txt
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt
14、git reset --hard 字符串 回退版本,其中后面跟的字符串是简写
[root@arslinux-01 gitroot]# git reset --hard 3517c5
HEAD 现在位于 3517c53 add 1.txt
[root@arslinux-01 gitroot]# git log --pretty=oneline
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt
15、如果回退出了错,现在想回到 b68b8fb900 的版本
可以直接 git reset --hard b68b8fb900,也可以使用 git reflog
[root@arslinux-01 gitroot]# git reflog
3517c53 HEAD@{0}: reset: moving to 3517c5
b68b8fb HEAD@{1}: commit: ch 1.txt
3517c53 HEAD@{2}: commit: add 1.txt
03d2ada HEAD@{3}: commit: add 1.txt
f04992b HEAD@{4}: commit (initial): add 1.txt
然后在使用 git reset --hard b68b8fb
16、如果不小心删除了文件,那么用 git checkout – 文件名 来恢复
[root@arslinux-01 gitroot]# rm -rf 1.txt
[root@arslinux-01 gitroot]# ls
[root@arslinux-01 gitroot]# git checkout -- 1.txt
[root@arslinux-01 gitroot]# ls
1.txt
17、如果文件本修改,add 后没有 commit,想回到上一次提交的状态,可以使用 git reset HEAD 文件名,然后在执行git checkout – 文件名
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git reset HEAD 1.txt
重置后撤出暂存区的变更:
M 1.txt
[root@arslinux-01 gitroot]# git checkout -- 1.txt
两个步骤:
1)git reset HEAD 文件名 从缓存区撤销
2)git checkout -- 文件名 恢复成修改前的状态
18、删除文件
[root@arslinux-01 gitroot]# git rm 1.txt
rm '1.txt'
[root@arslinux-01 gitroot]# git commit -m "delete 1.txt"
[master 560134a] delete 1.txt
1 file changed, 7 deletions(-)
delete mode 100644 1.txt
[root@arslinux-01 gitroot]# ls
[root@arslinux-01 gitroot]# git checkout -- 1.txt
error: pathspec '1.txt' did not match any file(s) known to git.
19、文件删除后依然可以找回,用 git reset 找回
[root@arslinux-01 gitroot]# git log --pretty=oneline
560134a1708b3815cb53af6ae160240b574e2240 delete 1.txt
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt
[root@arslinux-01 gitroot]# git reset --hard 3517c53c22f1
HEAD 现在位于 3517c53 add 1.txt
[root@arslinux-01 gitroot]# ls
1.txt
22.7 简历远程仓库
1、首先到 https://github.com 注册一个账号,创建自己的git,点右上角 “+”,选择 New repository
2、仓库名字自定义,比如叫,可以添加注释(Description),权限选择public,点Create repository
3、添加key:右上角点自己头像,选择settings,左侧选择SSH and GPG keys
4、右侧点 New SSH key,把linux机器上的 ~/.ssh/id_rsa.pub 内容粘贴到这里,可以用 ssh-keygen 生成
5、在本地创建一个仓库并进入
[root@arslinux-01 ~]# mkdir /tmp/studygit/
[root@arslinux-01 ~]# cd /tmp/studygit/
6、根据提示进行操作,首先创建一个 README.md
[root@arslinux-01 studygit]# echo "# studygit" >> README.md
7、初始化,生成 .git
[root@arslinux-01 studygit]# git init
初始化空的 Git 版本库于 /tmp/studygit/.git/
[root@arslinux-01 studygit]# ll -a
总用量 4
drwxrwxr-x 3 root root 80 7月 25 22:05 .
drwxrwxrwt 11 root root 340 7月 25 22:05 ..
drwxrwxr-x 7 root root 200 7月 25 22:05 .git
-rw-rw-r-- 1 root root 11 7月 25 22:04 README.md
8、提交 README.md 到仓库
[root@arslinux-01 studygit]# git add README.md
[root@arslinux-01 studygit]# git commit -m "first commit"
[master(根提交) f6afa16] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
9、将仓库内容推到远程
[root@arslinux-01 studygit]# git remote add origin https://github.com/axxxxx4xxx/studygit.git
10、将更改推送到远程
[root@arslinux-01 studygit]# git push -u origin master
Username for 'https://github.com': axxxxx4xxx
Password for 'https://axxxxx4xxx@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 220 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/axxxxx4xxx/studygit.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
11、推送新增文件操作
[root@arslinux-01 studygit]# echo "73737372hhdjdjd" > 2.txt
[root@arslinux-01 studygit]# ls
2.txt README.md
[root@arslinux-01 studygit]# git add 2.txt
[root@arslinux-01 studygit]# git commit -m "add 2.txt"
[master 6e4549c] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 studygit]# git push
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:
git config --global push.default matching
若要不再显示本信息并从现在开始采用新的使用习惯,设置:
git config --global push.default simple
参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)
Username for 'https://github.com':xxxxxxx
Password for 'https://xxxxxx@github.com':
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
f6afa16..6e4549c master -> master
12、根据提示,设置一下 git config
[root@arslinux-01 studygit]# git config --global push.default simple
13、网页刷新,就可以看到新增的文件
22.8 克隆远程仓库
1、网页端仓库中,点右侧绿色“Clone or download”,复制生成的 git 链接
2、克隆到本机
[root@arslinux-01 studygit]# cd
[root@arslinux-01 ~]# cd /home/
[root@arslinux-01 home]# git clone https://github.com/arsenal4life/studygit.git
正克隆到 'studygit'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
[root@arslinux-01 home]# cd studygit/
[root@arslinux-01 studygit]# ls
2.txt README.md
3、更改 README.md 内容,并推送到远程
[root@arslinux-01 studygit]# echo "hello hello" >> README.md
[root@arslinux-01 studygit]# echo "bye" >> README.md
[root@arslinux-01 studygit]# git add README.md
[root@arslinux-01 studygit]# git commit -m "change README.md"
[master b2c0aa1] change README.md
1 file changed, 2 insertions(+)
[root@arslinux-01 studygit]# git push
Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
6e4549c..b2c0aa1 master -> master
4、成功
5、将远程更改文件同步到本地
——远程修改 2.txt 内容
[root@arslinux-01 studygit]# 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 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
来自 https://github.com/arsenal4life/studygit
b2c0aa1..cce2494 master -> origin/master
更新 b2c0aa1..cce2494
Fast-forward
2.txt | 3 +++
1 file changed, 3 insertions(+)
[root@arslinux-01 studygit]# cat 2.txt
73737372hhdjdjd
thank you
go go go
ore wa neko ga suki
22.9 分支管理
- git branch 查看分支
- git branch 分支名 创建分支
- git checkout 分支名 切换分支
[root@arslinux-01 gitroot]# git branch
* master
[root@arslinux-01 gitroot]# git branch arslinux
[root@arslinux-01 gitroot]# git branch
arslinux
* master
[root@arslinux-01 gitroot]# git checkout arslinux
切换到分支 'arslinux'
[root@arslinux-01 gitroot]# git branch
* arslinux
master
当前使用的分支前面会有一个 * 在 arslinux 分支下
编辑 2.txt,并提交到新分支
[root@arslinux-01 gitroot]# echo "kajlhlk" > 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "add 2.txt"
[arslinux 0f6b36c] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 gitroot]# ls
1.txt 2.txt
[root@arslinux-01 gitroot]# git checkout master
切换到分支 'master'
[root@arslinux-01 gitroot]# ls
1.txt
分支可以隔离开文件
- git merge 分支名 合并分支
[root@arslinux-01 gitroot]# git merge arslinux
更新 3517c53..0f6b36c
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 gitroot]# ls
1.txt 2.txt
想要将文件合并到哪个分支下,那么在合并前,需要先切换到该分支下,再进行合并分支操作
以上操作中,合并分之后,master 分支下有了 2.txt
——合并分支时,如果合并的分支下有相同文件名的文件,那么文件内容需要一致
[root@arslinux-01 gitroot]# echo "12sddsf2232" > 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "ch 2.txt"
[master bb88c13] ch 2.txt
1 file changed, 1 insertion(+), 1 deletion(-)
[root@arslinux-01 gitroot]# git checkout arslinux
切换到分支 'arslinux'
[root@arslinux-01 gitroot]# vim 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "ch 2.txt"
[arslinux 79c2a72] ch 2.txt
1 file changed, 1 deletion(-)
[root@arslinux-01 gitroot]# git checkout master
切换到分支 'master'
[root@arslinux-01 gitroot]# git merge arslinux
自动合并 2.txt
冲突(内容):合并冲突于 2.txt
自动合并失败,修正冲突然后提交修正的结果。
[root@arslinux-01 gitroot]# vim 2.txt
[root@arslinux-01 gitroot]# cat 2.txt
<<<<<<< HEAD
12sddsf2232
=======
>>>>>>> arslinux
-
如果master分支和aming分支都对2.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。
-
解决冲突的方法是在master分支下,编辑2.txt,改为aming分支里面2.txt的内容。 然后提交2.txt,再合并aming分支。
-
但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 可以编辑2.txt内容,改为想要的,然后提交。切换到aming分支,然后合并master分支到aming分支即可(倒着合并)。合并分支有一个原则,那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。
-
git branch -d 分支名 删除分支
-
git branch -D arslinux 强制删除分支
[root@arslinux-01 gitroot]# git branch -d arslinux
已删除分支 arslinux(曾为 fcb0316)。
如果分支没有合并,删除之前会提示,那就不合并,强制删除