Git入门使用

本文详细介绍了Git的基本使用方法,包括创建和克隆仓库、文件提交、查看日志等常见操作,同时深入探讨了分支管理和合并冲突的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Git

git常用命令:
git init --bare /var/lib/git/project   #创建仓库
git clone 192.168.2.100:/var/lib/git/project   #克隆仓库
cd project   #进入仓库,所有关于仓库文件的命令都要进入仓库操作
git add .  #提交到暂存区
git commit -m "XXXXX"  #提交到仓库
git push  #提交到远程服务器
git log --oneline  #查看最精简日志
git reflog  #查看本机操作记录
git reset XXX --hard  #回到过去的时间节点
git status    		#查询现在的仓库跟踪状态,如下
# 位于分支 master
# 您的分支落后 'origin/master' 共 4 个提交,并且可以快进。
#   (使用 "git pull" 来更新您的本地分支)
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       web02-03.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

git服务器

[root@git ~]# yum -y install git
[root@git ~]# mkdir /var/lib/git	#创建git存储数据的目录
[root@git ~]# git  init  --bare  /var/lib/git/project		#创建仓库,存储数据的目录
Initialized empty Git repository in /var/lib/git/project/	
[root@git ~]# ls /var/lib/git/project
branches  config  description  HEAD  hooks  info  objects  refs

test

[root@test ~]# yum -y install git
[root@test ~]# git clone 192.168.1.17:/var/lib/git/project
Cloning into 'project'...
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.17' (ECDSA) to the list of known hosts.
root@192.168.1.17's password:			#可以配置免密登录
warning: You appear to have cloned an empty repository.
 #克隆服务端的仓库
[root@test ~]# cd project   #进入仓库
[root@test project]# ls
[root@test project]# git config --global user.email "you@example.com"	#配置用户
[root@test project]# git config --global user.name "Your Name"			#工作中写自己的
[root@test project]# echo "test-01" > test-01.txt  #创建测试文件
[root@test project]# git add .   #提交到暂存区
[root@test project]# git commit -m "test-01"  #再次提交文件保存到仓库中即可成功
[master(根提交) 3f0f227] test-01       #提交成功
......
[root@test project]# ssh-keygen
[root@test project]# ssh-copy-id root@192.168.1.17
[root@test project]# git push  #将本地仓库中的数据推送到远程服务器,首次推送可以按ctrl+c终止,按提示输入以下习惯配置
[root@test project]# git config --global push.default simple   #设置使用习惯
[root@test project]# git push   #再次推送到远程服务器
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 192.168.1.17:/var/lib/git/project
 * [new branch]      master -> master

查看日志记录 git reflog 常用

[root@test project]# git log           #查看git日志
commit e4ee5b8a9c72c6f686bc11dc040f9d1e606bf873
Author: Your Name <you@example.com>
Date:   Fri Jan 15 22:32:18 2021 +0800

    test-04
[root@test project]# git log --pretty=oneline			#查看精简日志
e4ee5b8a9c72c6f686bc11dc040f9d1e606bf873 test-04
[root@test project]# git log --oneline					#最精简日志
e4ee5b8 test-04
[root@test project]# git reflog				#查看本机git操作日志常用
e4ee5b8 HEAD@{0}: commit: test-04

head指针还原 git reset xxxx --hard

如果需要还原到之前的版本(时间节点),可以利用head指针对应日志记录中的随机字符串指向需要的版本
[root@test project]# git reflog  #查看回复记录之后的日志记录,head@{0}代表当前所在版本位置
6f63b89 HEAD@{0}: commit (initial): test-01
[root@test project]# echo xxx>> test-01.txt		#文件追加xxx
[root@test project]# git add .
[root@test project]# git commit -m "test-01++"  #保存记录
[root@test project]# git reflog
82998e0 HEAD@{0}: commit: test-01++		#第一行当前位置指针
6f63b89 HEAD@{1}: commit (initial): test-01
[root@test project]# cat test-01.txt		#现在的代码
test-01
xxx
[root@test project]# git reset 6f63b89 --hard
HEAD is now at 6f63b89 test-01
[root@test project]# cat test-01.txt		#回到了之前的代码
test-01
[root@test project]# git reflog
6f63b89 HEAD@{0}: reset: moving to 6f63b89  #还原的日志
82998e0 HEAD@{1}: commit: test-01++
6f63b89 HEAD@{2}: commit (initial): test-01

git分支创建与合并分支

[root@git ~]# rm -rf /var/lib/git/project   #服务端删除仓库
[root@git ~]# git  init  --bare  /var/lib/git/project   #重新创建仓库
[root@test ~]# rm -rf project/    #客户端删除仓库
[root@test ~]# git clone 192.168.1.17:/var/lib/git/project
[root@test ~]# cd project/   
[root@test project]# echo abc > 项目流程.txt   #创建测试文件
[root@test project]# git add .  
[root@test project]# git commit -m "001"   
[root@test project]# git branch hotfix  #创建分支hotfix
[root@test project]# git branch develop
[root@test project]# git branch  #查看分支,*是所在分支
  develop
  hotfix
* master
[root@test project]# git checkout hotfix  #切换分支到hotfix的
[root@test project]# echo abc > hotfix001.txt  #创建属于hotfix分支的文件
[root@test project]# git add .  #提交
[root@test project]# git commit -m "hotfix001"  #提交
[root@test project]# git checkout master  #切换到master分支
[root@test project]# echo abc > master001.txt  #创建属于master分支的文件
[root@test project]# git add .  #提交
[root@test project]# git commit -m "hotfix001"  #提交
[root@test project]# git merge hotfix  #在master下合并分支hotfix,合并时会进入信息编辑文本,写入合并的备注信息,保存退出即可

分支中的特殊情况:如果分别在不同分支,创建同名文件,内容不同,再合并时,会发生冲突,需要手工修改冲突文件

[root@test project]# echo abc > abc.txt  #在master分支继续创建文件
[root@test project]# git add .
[root@test project]# git commit -m "abc"
[root@test project]# git checkout hotfix  #切换到hotfix的分支
[root@test project]# echo xyz > abc.txt  #在hotfix分支继续创建文件
[root@test project]# git add .
[root@test project]# git commit -m "abc"  
[root@test project]# git checkout master  #切换到master的分支
[root@test project]# git merge hotfix  #再次合并分支,失败
[root@test project]#vim abc.txt  #编写文件,由人来决定最终合并内容保存退出即可解决冲突
<<<<<<< HEAD   
abc			#修改一致后,保存
=======
xyz
>>>>>>> hotfix0
[root@test project]# git add .
[root@test project]# git commit -m "abc+"  #提交,完成合并
[root@test project]# git merge hotfix
Already up-to-date.

git服务的使用方式:

使用http访问git,只读
[root@git ~]# yum -y install  httpd  gitweb   #装包
[root@git ~]# vim +10  /etc/gitweb.conf  #进入配置文件并转到第10行
our $projectroot = "/var/lib/git";   #去掉第10行的注释
[root@git ~]# killall nginx  #如果有其他占用80端口的服务要关闭
[root@git ~]# systemctl restart httpd  #开启httpd
使用火狐访问http:#192.168.1.17/git/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值