Git日常使用

Git最近流行起来了,很多公司平时开发就直接用GIT作为版本管理工具了,不再像以前只有SVN来管理代码和多人协作开发!!所以问题来了,我们必须学会Git的简单使用……

取得服务器文件
git clone URL 取得CI4最新代码示例: git clone https://github.com/bcit-ci/CodeIgniter4


		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git clone https://github.com/bcit-ci/CodeIgniter4 
		以上是取得CI4最新代码示例
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git clone https://github.com/bcit-ci/CodeIgniter4 codeigniter4New
		以上是取得CI4并重命名为codeigniter4New 
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git pull
		以上是更新你的本地仓库至最新改动
		
一、git设置

		设置全局用户名与邮箱:
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git config --global user.name "临来笑笑生"
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git config --global user.email '[email protected]'
		
		查看配置:
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ cat .git/config
		[core]
		        repositoryformatversion = 0
		        filemode = false
		        bare = false
		        logallrefupdates = true
		        symlinks = false
		        ignorecase = true
		[remote "origin"]
		        url = https://github.com/bcit-ci/CodeIgniter
		        fetch = +refs/heads/*:refs/remotes/origin/*
		[branch "develop"]
		        remote = origin
		        merge = refs/heads/develop
		[gui]
		        wmstate = normal
		        geometry = 1061x563+32+32 233 255
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ 
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ 
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ 
		
		=================================
		查看远程仓库命令:
		elapse@luck:/var/www/ilapu.com$ git remote -v
		origin	http://47.115.44.149:8089/root/new_lapu.git (fetch)
		origin	http://47.115.44.149:8089/root/new_lapu.git (push)
		elapse@luck:/var/www/ilapu.com$ 
		直接设置远程地址加上用户名和密码:
		git remote set-url origin https://用户名:密码@github.com/用户名/项目名.git
		elapse@luck:/var/www/ilapu.com$ git remote set-url origin http://chenghui:[email protected]:8089/root/new_lapu.git
		elapse@luck:/var/www/ilapu.com$ git remote -v
		origin	http://chenghui:[email protected]:8089/root/new_lapu.git (fetch)
		origin	http://chenghui:[email protected]:8089/root/new_lapu.git (push)
		elapse@luck:/var/www/ilapu.com$ 
		========示例=========
		git remote set-url origin http://chenghui:[email protected]:8089/Chenchengwang/lapuTP5.git  
		origin	http://47.115.44.149:8089/Chenchengwang/lapuTP5.git (push)
		origin	http://47.115.44.149:8089/Chenchengwang/lapuTP5.git (fetch)
		origin	http://47.115.44.149:8089/Chenchengwang/lapuTP5.git (push)
		=================================
		elapse@luck:/var/www/lapuTP5$ git remote -v
		origin  http://luck:[email protected]:8089/root/laputp5.git (fetch)
		origin  http://luck:[email protected]:8089/root/laputp5.git (push)
		elapse@luck:/var/www/lapuTP5$ 
		
二、创建git仓库及操作

		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ mkdir abc
		
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ cd abc/
		
		elapse@Toka MINGW64 /d/Language/PHP/Code/abc (develop)
		$ git init
		
		elapse@Toka MINGW64 /d/Language/PHP/Code/abc (develop)
		$ git clone https://github.com/bcit-ci/CodeIgniter4
		
		elapse@Toka MINGW64 /d/Language/PHP/Code/abc (develop)
		$
		
		创建一个新的版本库
		git clone [email protected]:tutor/luck.git
		cd luck
		touch README.md
		git add README.md
		git commit -m "add README"
		git push -u origin master
		
		现有的文件夹或Git版本库
		cd existing_folder
		git init
		git remote add origin [email protected]:tutor/luck.git
		git add .
		git commit
		git push -u origin master
		
三、Git分支(创建、切换、删除)
 
		elapse@luck:/var/www/lapuTP5$ 查看分支
		elapse@luck:/var/www/lapuTP5$ git branch 
		  luck
		* master
		  product
		elapse@luck:/var/www/lapuTP5$ 创建新分支testOne并切换
		elapse@luck:/var/www/lapuTP5$ git switch -c testOne
		切换到一个新分支 'testOne'
		elapse@luck:/var/www/lapuTP5$ 查看分支
		elapse@luck:/var/www/lapuTP5$ git branch 
		  luck
		  master
		  product
		* testOne
		elapse@luck:/var/www/lapuTP5$ 把新testOne分支推到线上
		elapse@luck:/var/www/lapuTP5$ git push --set-upstream origin testOne
		总共 0 (差异 0),复用 0 (差异 0)
		remote: 
		remote: To create a merge request for testOne, visit:
		remote:   http://47.112.240.208:8089/root/laputp5/merge_requests/new?merge_request%5Bsource_branch%5D=testOne
		remote: 
		remote: 正在同步更新测试服代码...
		remote: Ref refs/heads/testOne successfully received.  自动布置测试服什么也没处理:只有分支 master 才会自动布暑到测试服! 
		remote: 测试服git代码自动化布置完成!  --临来笑笑生
		To http://47.112.240.208:8089/root/laputp5.git
		 * [new branch]      testOne -> testOne
		分支 'testOne' 设置为跟踪来自 'origin' 的远程分支 'testOne'。
		elapse@luck:/var/www/lapuTP5$ 切换到master分支
		elapse@luck:/var/www/lapuTP5$ git switch master 
		切换到分支 'master'
		您的分支与上游分支 'origin/master' 一致。
		elapse@luck:/var/www/lapuTP5$ 删除testOne分支
		elapse@luck:/var/www/lapuTP5$ git branch -d testOne 
		已删除分支 testOne(曾为 1329038)。
		elapse@luck:/var/www/lapuTP5$ 查看分支
		elapse@luck:/var/www/lapuTP5$ git branch 
		  luck
		* master
		  product
		elapse@luck:/var/www/lapuTP5$ 下面进行分支合并,这里是在Luck分支上把master分支合并过来
		elapse@luck:/var/www/lapuTP5$ git pull
		已经是最新的。
		elapse@luck:/var/www/lapuTP5$ git switch luck 
		切换到分支 'luck'
		您的分支与上游分支 'origin/luck' 一致。
		elapse@luck:/var/www/lapuTP5$ git pull
		已经是最新的。
		elapse@luck:/var/www/lapuTP5$ git branch 
		* luck
		  master
		  product
		elapse@luck:/var/www/lapuTP5$ 把master分支合并到当前分支
		elapse@luck:/var/www/lapuTP5$ git merge master --squash 
		删除 extend/Tools/email.class.php
		删除 extend/Tools/Wxpay.class.php
		删除 extend/Tools/WeChatAuth.class.php
		删除 extend/Tools/Push.class.php
		删除 extend/Tools/NECaptchaVerifier.class.php
		自动合并 extend/Tools/Emchat.php
		冲突(内容):合并冲突于 extend/Tools/Emchat.php
		删除 extend/Tools/ConfigParse.class.php
		删除 extend/Tools/Alidayu.class.php
		删除 application/common/Service/EmMessageService.class.php
		删除 application/common/Service/EmMessage.class.php
		删除 application/common/Model/UserDevModel.class.php
		删除 application/common/Model/EmMessageModel.class.php
		删除 application/common/Model/BaseModel.class.php
		自动合并 application/api/model/EmMessage.php
		冲突(内容):合并冲突于 application/api/model/EmMessage.php
		自动合并 application/api/controller/Group.php
		冲突(内容):合并冲突于 application/api/controller/Group.php
		压缩提交 -- 未更新 HEAD
		自动合并失败,修正冲突然后提交修正的结果。
		elapse@luck:/var/www/lapuTP5$ 从上面看到有几个文件合并有冲突,自动合并失败,要手动解决冲突后git add 冲突文件,才可以进行下一步,这里直接用VSCode进行解决冲突
		elapse@luck:/var/www/lapuTP5$ git commit -m '合并master分支'
		[luck 7815f31] 合并master分支
		 23 files changed, 908 insertions(+), 2631 deletions(-)
		 create mode 100644 application/api/controller/Blacklistapi.php
		 create mode 100644 application/api/model/GroupMemberDelete.php
		 delete mode 100755 application/common/Model/BaseModel.class.php
		 delete mode 100755 application/common/Model/EmMessageModel.class.php
		 delete mode 100755 application/common/Model/UserDevModel.class.php
		 delete mode 100755 application/common/Service/EmMessage.class.php
		 delete mode 100755 application/common/Service/EmMessageService.class.php
		 delete mode 100755 extend/Tools/Alidayu.class.php
		 delete mode 100755 extend/Tools/ConfigParse.class.php
		 delete mode 100755 extend/Tools/NECaptchaVerifier.class.php
		 delete mode 100755 extend/Tools/Push.class.php
		 delete mode 100755 extend/Tools/WeChatAuth.class.php
		 delete mode 100755 extend/Tools/Wxpay.class.php
		 delete mode 100755 extend/Tools/email.class.php
		elapse@luck:/var/www/lapuTP5$ 把分并后的内容推送到远端
		elapse@luck:/var/www/lapuTP5$ git push
		枚举对象: 37, 完成.
		对象计数中: 100% (37/37), 完成.
		使用 16 个线程进行压缩
		压缩对象中: 100% (19/19), 完成.
		写入对象中: 100% (20/20), 10.60 KiB | 2.65 MiB/s, 完成.
		总共 20 (差异 11),复用 0 (差异 0)
		remote: 
		remote: To create a merge request for luck, visit:
		remote:   http://47.112.240.208:8089/root/laputp5/merge_requests/new?merge_request%5Bsource_branch%5D=luck
		remote: 
		remote: 正在同步更新测试服代码...
		remote: Ref refs/heads/luck successfully received.  自动布置测试服什么也没处理:只有分支 master 才会自动布暑到测试服! 
		remote: 测试服git代码自动化布置完成!  --临来笑笑生
		To http://47.112.240.208:8089/root/laputp5.git
		   ce0f5f9..7815f31  luck -> luck
		elapse@luck:/var/www/lapuTP5$ git pull
		已经是最新的。
		elapse@luck:/var/www/lapuTP5$ 分支合并完成
		
三、更改代码的操作

		更新本地代码到最新版本(需要merge才能合到本地代码中):
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git fetch
		
		合并更新后的代码到本地
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git merge
		
		更新你的本地仓库至最新改动(git fetch和git merge组合)
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git pull
		
		修改代码后,查看已修改的内容
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git diff --cached
		
四、其它常用命令

		销毁自己的修改
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git reset --hard
		
		查看最新版本和上一个版本的差异(一个^表示向前推进一个版本)
		elapse@Toka MINGW64 /d/Language/PHP/Code (develop)
		$ git diff HEAD HEAD^
		
使用手册:
    http://idannywu.com/git.html
    https://git-scm.com/book/zh/v2
    http://rogerdudler.github.io/git-guide/index.zh.html
    http://wlog.cn/soft/git-quick-start.html