【图文教程】代码管理平台 git

本文是一篇关于Git代码管理的图文教程,详细讲解了从单机使用Git创建仓库,建立远程仓库,克隆仓库,到分支管理、标签管理和别名设置的全过程。重点介绍了如何在本地和远程之间同步代码,以及解决合并冲突的方法和分支使用原则。
摘要由CSDN通过智能技术生成

1. 单机上使用git

git是分布式的仓库,我们不需要把代码上传或更新到某个特定的服务器上,所以它不需要依赖网络,我们可以在本地创建一个git仓库。

  • git安装命令
yum install -y git
  • 创建git仓库
mkdir /data/gitroot/
  • git 初始化仓库
1. cd /data/gitroot/

2. git init         #初始化git仓库
Initialized empty Git repository in /data/gitroot/.git/  #初始化空的 Git 版本库于 /data/gitroot/.git/

3. [root@test01 gitroot]# ls -la .
total 0
drwxr-xr-x. 3 root root  18 Nov 23 14:31 .
drwxr-xr-x. 5 root root  49 Nov 23 14:30 ..
drwxr-xr-x. 7 root root 119 Nov 23 14:31 .git

4. [root@test01 gitroot]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs
  • 创建文件,输入代码,并添加到仓库中
[root@test01 gitroot]# vim 1.txt
[root@test01 gitroot]# git add 1.txt               #把1.txt添加到仓库
[root@test01 gitroot]# git commit -m "add 1.txt"   #add完了必须要commit才算真的添加到仓库

*** 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@test02.(none)')


# 为了避免老是打印提示信息,可以随便设置一下这两项信息
[root@test01 gitroot]# git config --global user.name "sc" 
[root@test01 gitroot]# git config --global user.email sc@none.com
  • 修改文件中的内容,然后进行提交
[root@test01 gitroot]# echo 888 >1.txt
[root@test01 gitroot]# git add 1.txt
[root@test01 gitroot]# git commit -m "add 1.txt agin"
[master a2d4181] add 1.txt agin
 1 file changed, 1 insertion(+), 6 deletions(-)
  • git status 命令可以查看当前仓库中的状态,比如是否有改动的文件等
[root@test01 gitroot]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#
# 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
  • git diff 命令可以对比某个文件本次修改了什么内容,相比较仓库里面的版本
[root@test01 gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index dc6a988..190a180 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1 @@
-888
+123
  • git log 查看所有的提交记录
1. [root@test01 gitroot]# git log
commit 90dc23420b39ffb3153aca5ed2d327c81b524c19
Author: sc <sc@none.com>
Date:   Mon Nov 23 16:43:43 2020 +0800

    add 1.txt

commit 8de29fe07e3b1199d5ccec3ef1ff6d648cf75422
Author: sc <sc@none.com>
Date:   Mon Nov 23 16:43:02 2020 +0800

    add 1.txt agin


2. [root@test01 gitroot]# git log --pretty=oneline        #一行显示提交记录
90dc23420b39ffb3153aca5ed2d327c81b524c19 add 1.txt
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin
  • 回退版本,其中后面跟的字符串是简写
1. [root@test01 gitroot]# git log --pretty=oneline   
3f5eccf4e0c55b679db5a6ce4adf7452ca774adb ad 1.txt
90dc23420b39ffb3153aca5ed2d327c81b524c19 add 1.txt
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin


2. [root@test01 gitroot]# git reset --hard 8de2      #回退版本,后面跟字符串简写
HEAD is now at 8de29fe add 1.txt agin


3. [root@test01 gitroot]# cat 1.txt
adfsdaf
a
a
a
a
a
a


4. [root@test01 gitroot]# git log --pretty=oneline
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin
  • 如果回退版本后,发现不合适,想要回退到新版本或者其他历史版本上,可以使用 git reflog 命令查看所有历史版本
[root@test01 gitroot]# git reflog           #查看所有历史版本
8de29fe HEAD@{
   0}: reset: moving to 8de2
3f5eccf HEAD@{
   1}: commit: ad 1.txt
90dc234 HEAD@{
   2}: commit: add 1.txt
8de29fe HEAD@{
   3}: commit (initial): add 1.txt agin
  • 通过git可以恢复删除的文件,前提是你已经将文件提交到了仓库中。如果不小心把某个文件删除了,而这个文件已经存储在仓库中的话,就可以从仓库恢复这个文件
[root@test01 gitroot]# ls
1.txt
[root@test01 gitroot]# rm -fr 1.txt
[root@test01 gitroot]# ls
[root@test01 gitroot]# git checkout -- 1.txt     #恢复删除的仓库文件
[root@test01 gitroot]# ls
1.txt
  • 如果某个文件进行了修改,add后但没有commit,再想回退到上一次提交的状态,可以使用 git reset HEAD filename,再执行git checkout – filename
[root@test01 gitroot]# vim 1.txt
[root@test01 gitroot]# git add 1.txt
[root@test01 gitroot]# git reset HEAD 1.txt   #退回上一次提交状态
Unstaged changes after reset:
M	1.txt
[root@test01 gitroot]# cat 1.txt
a
a
a
a
a
a
[root@test01 gitroot]# git checkout -- 1.txt  #恢复库文件
[root@test01 gitroot]# cat 1.txt
adfsdaf
a
a
a
a
a
a
  • 删除仓库中的文件
[root@test01 gitroot]# git rm 1.txt
rm '1.txt'
[root@test01 gitroot]# git commit -m "delete 1.txt"
[master 3272657] delete 1.txt
 1 file changed, 7 deletions(-)
 delete mode 100644 1.txt
  • 即便删除了仓库中的文件,也是可以通过版本id来恢复的
[root@test01 gitroot]# git log --pretty=oneline
3272657e568a05095a392d66ed74b1ef52e15c4a delete 1.txt
8de29fe07e3b1199d5ccec3ef1ff6d648cf75422 add 1.txt agin
[root@test01 gitroot]# git reset --hard 8de29
HEAD is now at 8de29fe add 1.txt agin
[root@test01 gitroot]# ls
1.txt
[root@test01 gitroot]# cat 1.txt
adfsdaf
a
a
a
a
a
a

2. 建立远程仓库

  • 首先到登陆 https://github.com 注册账号

请添加图片描述

请添加图片描述

  • 登录之后,点击右上角,头像旁边的 + 图标,创建一个自己的repository(仓库)

请添加图片描述

  • 填写仓库的相关信息

请添加图片描述

  • 创建完成,如下,远程仓库就创建好了

请添加图片描述

  • 可以把GitHub上创建的仓库,添加密钥,作为我们的远程服务端

请添加图片描述

请添加图片描述

[root@test01 ~]# 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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值