Git:分布式版本控制工具
有一个共享版本库,所有人从上面拉代码,也都往上面推送代码,这样就能做到版本控制了.
概念流程图
基本配置
git bash here是git的luinx控制台
开始时一定要设置用户名和邮箱,这两个可以随便写,目的中你以后用哪个id来上传代码
>修改配置信息
git config --global user.name "username"
git config --global user.email "email"
git config --global user.name
git config --global user.email
新建一个.bashrc(我是放在了d盘的git安装目录下了)文件,在里面添加代码
#用于输出git提交日志
alias git-log='git log --pretty=oneline --all --graph --abbrev-commit'
#用于输出当前目录所有文件及基本信息
alias ll='ls -al'
然后执行下面的代码
~/代表你的用户目录
source ~/.bashrc
本地仓库
在空目录中打开控制台,执行代码,就能看到一个.git目录了
这个空目录就是我们的本地仓库了,除了.git目录外,都是我们的工作目录
git init
常用命令
touch file01.tet
git add .
git status
git commit -m "add file01"
git commit -m 'update file01'
rm -rf .file01.tet.swp
git log
vim file01.tet
vim 复杂的编辑器,相当于windows的 editplus, notepad++ 等
步骤:
1、执行 vim file01.txt进入编辑器(默认命令模式),
2、点击a或i进入编辑模式,敲入内容:
3、然后按键盘上的esc键退出编辑模式(进入到命令模式),
4、最后敲冒号:,
5、再敲wq保存并退出。
日志
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --all
commit bc32ce203c8a3fb6253f0b2a429ad0c849bfcde0 (HEAD -> master)
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 09:54:57 2022 +0800
update file01
commit 0141964afd011a0e44af35fff8ae9153aa5d5bff
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 09:40:36 2022 +0800
add file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --pretty=oneline
bc32ce203c8a3fb6253f0b2a429ad0c849bfcde0 (HEAD -> master) update file01
0141964afd011a0e44af35fff8ae9153aa5d5bff add file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --pretty=oneline --abbrev-commit
bc32ce2 (HEAD -> master) update file01
0141964 add file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --pretty=oneline --abbrev-commit --all
bc32ce2 (HEAD -> master) update file01
0141964 add file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --pretty=oneline --abbrev-commit --all --graph
* bc32ce2 (HEAD -> master) update file01
* 0141964 add file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --pretty=oneline --abbrev-commit --all --graph --decorate
* bc32ce2 (HEAD -> master) update file01
* 0141964 add file01
日志别名
alias git-log='git log --pretty=oneline --abbrev-commit --all --graph --decorate'
版本回退
git reset --hard 0141964
$ clear
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git reflog
0141964 (HEAD -> master) HEAD@{0}: reset: moving to 0141964
bc32ce2 HEAD@{1}: commit: update file01
0141964 (HEAD -> master) HEAD@{2}: commit (initial): add file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git reset --hard bc32ce2
HEAD is now at bc32ce2 update file01
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log
commit bc32ce203c8a3fb6253f0b2a429ad0c849bfcde0 (HEAD -> master)
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 09:54:57 2022 +0800
update file01
commit 0141964afd011a0e44af35fff8ae9153aa5d5bff
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 09:40:36 2022 +0800
add file01
屏蔽特定文件
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ touch .gitignore
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ vim .gitignore
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ touch file02.a
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
新建 文本文档.txt
nothing added to commit but untracked files present (use "git add" to track)
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log
commit bc32ce203c8a3fb6253f0b2a429ad0c849bfcde0 (HEAD -> master)
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 09:54:57 2022 +0800
update file01
commit 0141964afd011a0e44af35fff8ae9153aa5d5bff
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 09:40:36 2022 +0800
add file01
分支
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git branch -D dev
Deleted branch dev (was 61b24fd).
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git branch
* master
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git checkout -b dev
Switched to a new branch 'dev'
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ touch devFile.txt
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git commit -m 'add devFile'
[dev 9add000] add devFile
3 files changed, 3 deletions(-)
delete mode 100644 .gitignore
rename file03.txt => devFile.txt (100%)
delete mode 100644 file01.tet
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git log
commit 9add000e4e78b4cc0a3f7e3985e26a06e82d7048 (HEAD -> dev)
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 14:59:39 2022 +0800
add devFile
commit f0f9923a5b1c82eb193321b84347fd6825e75ece (master)
Author: LinMu <3276295265@qq.com>
Date: Mon Oct 10 14:50:51 2022 +0800
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git checkout master
Switched to branch 'master'
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git merge dev
Updating f0f9923..9add000
Fast-forward
.gitignore | 1 -
file03.txt => devFile.txt | 0
file01.tet | 2 --
3 files changed, 3 deletions(-)
delete mode 100644 .gitignore
rename file03.txt => devFile.txt (100%)
delete mode 100644 file01.tet
解决冲突
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: devFile.txt
no changes added to commit (use "git add" and/or "git commit -a")
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git commit -m 'update devFile-1'
[master 29f0293] update devFile-1
1 file changed, 1 insertion(+)
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git checkout dev
Switched to branch 'dev'
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git commit -m '3'
[dev 2c64c11] 3
1 file changed, 1 insertion(+), 1 deletion(-)
林木@xiaomi MINGW64 ~/Desktop/test (dev)
$ git checkout master
Switched to branch 'master'
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git merge dev
Auto-merging devFile.txt
CONFLICT (content): Merge conflict in devFile.txt
Automatic merge failed; fix conflicts and then commit the result.
然后就会出现合并冲突
<<<<<<< HEAD
hujie=2
=======
hujie=3
>>>>>>> dev
开发原则
- master一般只用来上线
- develop是平时开发
- hotfix用来修复bug,最后还要合并到master和develop上去
-d和-D的区别
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git checkout -b dev02
Switched to a new branch 'dev02'
林木@xiaomi MINGW64 ~/Desktop/test (dev02)
$ touch file.txt
林木@xiaomi MINGW64 ~/Desktop/test (dev02)
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (dev02)
$ git commit -m 'a'
[dev02 8a1e77b] a
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file.txt
林木@xiaomi MINGW64 ~/Desktop/test (dev02)
$ git checkout master
Switched to branch 'master'
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git branch -d dev02
error: The branch 'dev02' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev02'.
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git branch -D dev02
Deleted branch dev02 (was 8a1e77b).
显示分支操作轨迹(快进模式)
只有两个分支同时修改过一个文件后,才会显示轨迹
$ alias git-log='git log --pretty=oneline --abbrev-commit --all --graph --decorate'
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git-log
* 5159c04 (HEAD -> master) 4
|\
| * 2c64c11 (dev) 3
* | bcb6003 2
|\|
| * e998f81 1
* | fe123ca Merge branch 'dev'
|\|
| * 7f25882 dev update
| * 8775dd5 dev update devFile
* | 90535fc master update
* | 29f0293 update devFile-1
|/
* 9add000 add devFile
* f0f9923 add file03
* bc32ce2 update file01
* 0141964 add file01
远程Git仓库:码云
配置SSH公钥
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub
在码云设置的ssh密钥中,把密钥粘贴过去.
ssh -T git@gitee.com
输入yes,显示Hi 林木! You've successfully authenticated, but GITEE.COM does not provide shell access.
代表成功
git remote add origin git@https://gitee.com/LinMu12/git_test.git
git remote set-url origin git@gitee.com:LinMu12/git_test.git
推送
git push origin master
$ git remote
origin
分支对应关系
git branch -vv
dev 2c64c11 3
* master 5159c04 4
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git push --set-upstream origin master
Everything up-to-date
branch 'master' set up to track 'origin/master'.
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git push
Everything up-to-date
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git branch -vv
dev 2c64c11 3
* master 5159c04 [origin/master] 4
克隆远程仓库
在哪个git文件夹中打开,就把文件下载到哪个文件夹里面
git clone git@gitee.com:LinMu12/git_test.git cloneGit
两个文件夹的属性基本完全一致
$ git log --oneline
5159c04 (HEAD -> master, origin/master, origin/HEAD) 4
2c64c11 3
bcb6003 2
e998f81 1
fe123ca Merge branch 'dev'
$ git log --oneline
5159c04 (HEAD -> master, origin/master) 4
2c64c11 (dev) 3
bcb6003 2
e998f81 1
抓取和拉取
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ touch 1.txt
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
1.txt
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git commit -m 'add 1'
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --oneline
ecfebce (HEAD -> master) add 1
fa7cb98 (origin/master) deleteDevFile
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git push
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git log --oneline
ecfebce (HEAD -> master, origin/master) add 1
克隆仓库就会发现远程仓库多了一些操作
林木@xiaomi MINGW64 ~/Desktop/cloneGit (master)
$ git fetch
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 210 bytes | 19.00 KiB/s, done.
From gitee.com:LinMu12/git_test
fa7cb98..ecfebce master -> origin/master
$ git log --oneline --all
ecfebce (origin/master, origin/HEAD) add 1
fa7cb98 deleteDevFile
5159c04 (HEAD -> master) 4
本地分支与远程分支合并
$ git merge origin/master
林木@xiaomi MINGW64 ~/Desktop/cloneGit (master)
$ git log --oneline --all
ecfebce (HEAD -> master, origin/master, origin/HEAD) add 1
fa7cb98 deleteDevFile
拉取
$ git add .
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git commit -m 'del'
[master 24cb4d8] del
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 1.txt
林木@xiaomi MINGW64 ~/Desktop/test (master)
$ git push
pull 直接抓取并合并
林木@xiaomi MINGW64 ~/Desktop/cloneGit (master)
$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 163 bytes | 18.00 KiB/s, done.
From gitee.com:LinMu12/git_test
ecfebce..24cb4d8 master -> origin/master
Updating ecfebce..24cb4d8
Fast-forward
1.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 1.txt
林木@xiaomi MINGW64 ~/Desktop/cloneGit (master)
$ git log --oneline --all
24cb4d8 (HEAD -> master, origin/master, origin/HEAD) del
远程合并冲突
failed to push some refs to 'gitee.com:LinMu12/git_test.git
git pull
本地仓库修改后推送到远程仓库
另一个本地仓库修改同一个文件后,推送给远程仓库,就会提示合并冲突
CONFLICT (add/add): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
<<<<<<< HEAD
update = 3
=======
update = 2
>>>>>>> cbbe2c81a0dbe451b5b15b336b6e0d9162ef2a3e
~
idea整合Git
https://www.cnblogs.com/huzixia/p/10392321.html
idea弱化了暂存区的概念,所以需要Git>add
合并冲突
在码云上复制了克隆连接后,在Git栏目中直接克隆
先拉取再推送
- 两个仓库修改了同一个文件,第二个仓库推送时,会提示你合并冲突,直接x掉.
- 项目目录中会红色显示冲突的文件,修改后再推送就行了.
铁令
- 切换分支前先把代码提交了
- 只要不删文件目录,代码就能找回来
配置GitBash
配置gitbash后,就能在idea中使用git终端了
开发经验
把代码交给git托管,当天没有完成的任务放到一个分支中,进度继续向前推,以后有时间了再来完成
|