有一个共享版本库,所有人从上面拉代码,也都往上面推送代码,这样就能做到版本控制了.
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)
//取前六位id
$ 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)
//显示commit属于哪个分支或tag
//mac系统要加上,高版本git默认开启的
$ 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'
//按id回退
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)
//回到update节点
$ 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)
//在里面写上*.a,表示屏蔽特定文件
$ 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 ..." to include in what will be committed)
//file02.a不被git管理了
.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)
//强制删除分支,-d是删除分支时需要做检查
$ 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
//这里显示add devFile是添加提交到了dev分支上
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)
//将dev分支添加进来
$ 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
//devFile.txt就被加进来了
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 ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
//我们修改了master的文件
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(+)
//切换到dev分支,修改相同的devFile.txt文件
林木@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(-)
//切换回master分支,合并dev
林木@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
//创建dev02,并提交一个文件
林木@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)
//在master中删除dev02
$ git branch -d dev02
//这个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)
//直接-D强制删除
$ 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
配置SSH公钥
//rsa是一种算法
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
//将本地的master分支推送到origin仓库
git push origin master
//查看远程仓库列表
$ git remote
origin
//查看分支对应关系
git branch -vv
dev 2c64c11 3
* master 5159c04 4
林木@xiaomi MINGW64 ~/Desktop/test (master)
//将本地的master分支对应到远程的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
//新建一个1.txt文件,并提交给本地仓库
林木@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 ..." 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)
//pull = fetch + merge
$ 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):合并file.txt中的冲突
CONFLICT (add/add): Merge conflict in file.txt
//自动合并失败;修复冲突,然后提交结果。
Automatic merge failed; fix conflicts and then commit the result.
//vim file.txt
<<<<<<< HEAD
update = 3
=======
update = 2
>>>>>>> cbbe2c81a0dbe451b5b15b336b6e0d9162ef2a3e
~
//手动修改后,再重新提交推送
https://www.cnblogs.com/huzixia/p/10392321.html
idea弱化了暂存区的概念,所以需要Git>add
在码云上复制了克隆连接后,在Git栏目中直接克隆
先拉取再推送
配置gitbash后,就能在idea中使用git终端了
把代码交给git托管,当天没有完成的任务放到一个分支中,进度继续向前推,以后有时间了再来完成