【Git GitHub Idea集成】
1 Git介绍
分布式版本控制工具 VS 集中式版本控制工具
git是一个免费开源的分布式版本控制系统,可以快速高效地处理从小型到中型的各种项目。
1.1 Git进行版本控制
集中式版本控制工具:如CVS、SVN、VSS等等,都有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人们通过客户端连接到这台服务器,取出最新的文件或者进行更新,即:在集中式服务器上进行版本控制。
这种做法带来的好处是每个人在一定程度上可以看到其他人在做什么,而管理员也可以轻松掌握每个开发者的权限,并且管理一个集中化的版本控制系统,要比在本地的每个客户端维护本地数据库容易。但是一旦中央服务器出现单点故障,所有人都不能提交更新,也就无法进行协同工作。
即使事后进行了提交,也没有这段期间的版本信息了
分布式版本控制工具:典型的git,每个客户端都在本地进行版本控制,并且使用代码托管中心作为远程库,客户端首先要从远程库clone代码到本地库,进行修改更新做本地的版本控制,并推送到远程库保证远程库中的代码最新。
1.1.1 Git的运行机制
如图所示,工作区的代码
①首先通过git add命令添加到暂存区,作为临时存储
②再通过git commit提交到本地库,生成历史版本
历史版本一旦生成不允许修改,这是由于git的历史版本都是基于上一个版本生成的
1.1.2 Git和远程库
2 Git安装
右键出现GitGUI和Git bash here则说明安装成功。
3 Git命令
3.1 git config 设置数字签名和邮箱
git config --global user.name xxxx git config --global user.email xxxx
设置成功之后可以在C盘下当前用户中的.gitconfig文件中查看设置的信息。
签名的作用是区分不同的操作者的身份,用户的签名在每一个版本中都能看到,以此确定本次提交是谁做的。
Git首次安装必须设置一下用户签名和邮箱,否则无法进行提交代码。
这里设置的用户签名、邮箱与将来登录Github(或者其他代码托管中心)的账号没有任何关系。
3.2 git init 初始化本地库
$ git init Initialized empty Git repository in D:/xxx/git/git-demo/.git/
想让git管理目录,首先需要让git初始化获取当前目录的管理权,执行之后会自动生成一个.git的隐藏文件,使用linux命令ll -a才能访问到。
3.3 git status查看本地库状态
$ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
On branch master:表示当前分支
No commits yet:当前还没有提交过东西
nothing to commit (create/copy files and use "git add" to track):也没有东西需要提交到暂存区(现在工作区是空的)
创建一个txt文件之后再git status:
On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track)
前两行没有变,缓存区没有东西所以也不需要commit到本地库,但是工作区存在没有本track的文件hello.txt
3.4 git add, gir rm --cached 添加删除文件到暂存区
$ git add hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory
warning中的内容的意思是自动将文件的行末换行符由linux的LF转换为了weindows的CRLF。然后使用git status查看本地库状态:
$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: hello.txt
然后发现文件颜色由红色(没被添加到工作区)变为了绿色(添加到了工作区)
使用git rm --cached hello.txt 删除暂存区的文件:
$ git rm --cached hello.txt rm 'hello.txt'
然后查看现在暂存区的状态,发现文件又变成了红色:
$ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track)
3.5 git commit -m “日志信息” 提交到本地库
git commit -m "my first commit"
-m后面跟提交的日志信息,即使不加也会后面强制进行设置
779a747为版本号
$ git commit hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory [master (root-commit) 779a747] my first commit 1 file changed, 11 insertions(+) create mode 100644 hello.txt
再次git status查看本地库状态:
$ git status On branch master nothing to commit, working tree clean
当前为主分支,没有东西需要commit工作区是干净的
3.6 git reflog 查看引用日志信息
$ git reflog 779a747 (HEAD -> master) HEAD@{0}: commit (initial): my first commit
779a747为版本号,HEAD -> master表示当前指针指向的是master分支
3.7 git log 查看详细日志信息
$ git log commit 779a747d73bb4ea5f30f51f96e4c22ac1c88eb11 (HEAD -> master) Author: ‘tod4-lab’ <‘819574539@qq.com’> Date: Fri Jul 15 23:47:16 2022 +0800 my first commit
除了reflog的显示的信息外,可以看到提交的用户签名以及日期等详细信息
3.7 文件修改
①首先对文件进行修改,然后git status
$ 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
②将文件重新添加到暂存区
$ git add hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory
git status
$ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: hello.txt
③提交暂存区文件到本地库
$ git commit -m "my second commit" hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory [master 76b3a63] my second commit 1 file changed, 1 insertion(+), 1 deletion(-)
可以看到分配了新的版本号
并且一行被修改一行被删除(git通过整行的方式进行修改,先将一行删除再将一行添加实现修改)
④查看此时的本地库状态
$ git status On branch master nothing to commit, working tree clean
3.8 版本穿梭 git reset --hard 版本号
①查看当前版本信息
76b3a63 (HEAD -> master) HEAD@{0}: commit: my second commit 779a747 HEAD@{1}: commit (initial): my first commit
②进行版本穿梭
$ git reset --hard 779a747 HEAD is now at 779a747 my first commit
②再次查看版本信息
$ git reflog 779a747 (HEAD -> master) HEAD@{0}: reset: moving to 779a747 76b3a63 HEAD@{1}: commit: my second commit 779a747 (HEAD -> master) HEAD@{2}: commit (initial): my first commit
③查看文件内容为第一次提交的内容
$ cat hello.txt Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!
4 Git分支
分支
:在版本控制的过程中,为了同时推进多个任务,为每个任务创建一个单独的分支,使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候不影响主线分支的运行。
分支的好处
:可以同时推进多个功能开发,提高并发效率。多个分支在开发的过程中,如果有一个分支开发失败,不会对其他分支产生影响,失败的分支删除重新开始即可。
4.1 git branch -v 查看当前的分支
$ git branch -v * master 779a747 my first commit
-v表示显示版本号
*表示当前所在的分支
4.2 git branch 分支名 创建分支
$ git branch hotfix
git branch -v 查看分支信息:
$ git branch -v hotfix 779a747 my first commit * master 779a747 my first commit
4.3 git checkout切换分支
$ git checkout hotfix Switched to branch 'hotfix'
git branch -v 查看分支信息:
$ git branch -v * hotfix 779a747 my first commit master 779a747 my first commit
①修改文件,再git status查看本地库状态
$ git status On branch hotfix 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: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
②git add 添加文件到暂存区
$ git add hello.txt
③git commit 提交文件到本地库
$ git commit -m "hotfix first commit" hello.txt [hotfix bdd656f] hotfix first commit 1 file changed, 1 insertion(+), 1 deletion(-)
④再次查看本地库状态
$ git status On branch hotfix nothing to commit, working tree clean
4.4 git merge 分支名 将指定分支合并到当前分支
4.4.1 正常合并
正常分支
是指指定分支有修改而当前分支没有修改,即两个分支之间没有冲突的情况。
①首先切换到主分支
$ git checkout master Switched to branch 'master'
②将指定分支hotfix与主分支进行合并
$ git merge hotfix Updating 779a747..bdd656f Fast-forward hello.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
③读取文件发现master分支的文件被修改成了hotfix分支下的文件,并查看本地库状态
$ git status On branch master nothing to commit, working tree clean
4.4.2 冲突合并
冲突分支
与上面相反就是出现冲突的情况。
①在master分支修改文件,并提交到本地库
$ cat hello.txt Hello world! Hello world! master test Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!
②切换到hotfix分支修改文件
$ cat hello.txt Hello world! Hello world! hotfix test Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!
③切换回主分支并进行合并
$ git merge hotfix Auto-merging hello.txt CONFLICT (content): Merge conflict in hello.txt Automatic merge failed; fix conflicts and then commit the result.
由于两个分支存在冲突,可以看到git自动合并失败了,需要我们进行手动合并
此时的当前目录也会变为:/git/git-demo (master|MERGING)合并中的状态
④这时候再查看本地库状态
$ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
⑤手动合并代码,手动vim进文件发现文件变成了下面的样子,<<<<<<< HEAD到 的内容是主分支的内容,= 到 >>>>>>> hotfix 为hotfix分支冲突的内容。手动选择冲突内容并对
<<<<<<< HEAD Hello world! Hello world! master test ======= Hello world! Hello world! hotfix test >>>>>>> hotfix Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!
⑥修改之后重新commit到本地库
$ git commit -m 'merge commit' [master 236eae6] merge commit
注意这里不能再commit后面加文件名,这样的话会出现下面的情况
即在合并的过程中不能进行部分提交(不知道该提交哪个分支的冲突部分)
$ git commit -m 'merge commit' hello.txt fatal: cannot do a partial commit during a merge.
5 Git团队协作机制
5.1 团队内协作
5.2 跨团队协作
fork:叉子,复制远程库
4 Github 代码远程托管中心
4.1 create repository 创建远程仓库
略
4.2 git remote 创建远程库别名
① git remote add blogs \https://github.com/tod4-lab/blogs.git 创建远程库别名
② git remote -v查看当前的远程库别名
$ git remote -v blogs https://github.com/tod4-lab/blogs.git (fetch) blogs https://github.com/tod4-lab/blogs.git (push)
意味着push 和 fetch 都可以通过使用别名blogs来操控远程库
4.3 git push 远程仓库别名 分支名:推送本地库到远程仓库
$ git push blogs master Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 352 bytes | 352.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To https://github.com/tod4-lab/blogs.git 30773ff..36d1f6f master -> master
然后在GitHub的仓库下master分支就可以看到提交的代码了。
4.4 git pull 远程库别名+分支 拉取远程库到本地库
$ git pull blogs master remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 1 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 674 bytes | 56.00 KiB/s, done. From https://github.com/tod4-lab/blogs * branch master -> FETCH_HEAD 36d1f6f..6c35de8 master -> blogs/master Updating 36d1f6f..6c35de8 Fast-forward HelloWorld.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
查看代码:
$ cat HelloWorld.cpp #include <iostream> using namespace std; int main() { cout<<"HelloWorld!"<<endl; return 0; }
4.5 git clone + 地址 克隆远程库代码到本地库
克隆成功之后会自动为远程库添加别名为orign
5 团队协作
仓库 -> Settings -> Collaborators(合作者) -> add people进行添加团队成员,添加成功之后变可以直接进行代码push操作了
6 跨团队协作
①点击fork将仓库复制带自己的仓库中
②在自己的远程仓库修改代码
③Pull request -> new pull request -> create pull request
④Merge Request Pull合并请求的pull操作
7 SSH免密操作
在远程库获取代码的地方除了Https还有SSH的地址,因此也能够通过SSH的方式进行访问。
①使用命令生成SSH密钥
ssh-keygen -t rsa -C xxxx@xx.com
ssh-keygen为生成SSH密钥命令
-t指定加密算法:rsa为一种非对称加密协议加密算法
-C加密的描述:邮箱
②生成之后会在用户目录下生成一个.ssh的文件夹,里面包括一个id_rsa
存储着私钥
,一个id_rsa.pub
,存储着公钥
。
③在gitHub上 Settings -> Access-> SSH and GPG keys中添加公钥即可,添加完成之后页面如下所示:
⑤输入公钥之后当前windows连接此账户就不需要用户名和密码了
$ git pull blog master remote: Enumerating objects: 7, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (5/5), done. remote: Total 7 (delta 1), reused 5 (delta 0), pack-reused 0 Unpacking objects: 100% (7/7), 1.08 KiB | 42.00 KiB/s, done. From github.com:tod4-lab/blogs * branch master -> FETCH_HEAD * [new branch] master -> blog/master
如上可以直接进行pull操作
8 在IDEA集成git
8.1 配置.ignore忽略文件
如eclipse中的.settings、.classpath、.project等,Idea中的.idea、target等是不需要在服务器上运行的,即与实际项目的功能无关。把它们忽略掉能够屏蔽 IDE 工具之间的差异。
①在用户目录下建立git.ignore
文件,表示git屏蔽的文件
# Compiled class file *.class # Log file *.log # BlueJ files *.ctxt # Mobile Tools for Java (J2ME) .mtj.tmp/ # Package Files # *.jar *.war *.nar *.ear *.zip *.tar.gz *.rar # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* .classpath .project .settings target .idea *.iml
②在.gitconfig 文件中引用忽略配置文件(此文件在 Windows 的家目录中)
[user] name = ‘tod4-lab’ email = ‘819574539@qq.com’ [http] sslVerify = false [core] excludesfile = C:/Users/admin/git.ignore
8.3 在idea中配置Git
8.4 初始化本地库
点击VCS -> create Git repository -> 选择文件目录进行初始化本地库。
8.5 添加&提交
添加
:右键 -> git -> Add
提交
:右键 -> git -> commit
文件颜色:
红色
:未经track到暂存区
绿色
:添加到暂存区但是没有进行commit到本地库
8.6 切换版本
修改代码进行第二次提交:
如图,进行第二次commit的时候会出现与之前版本的对比
在左下角的Git -> log上可以看到当前的所有提交,右下角的黄色标签
表示指针指向的当前版本,绿色标签
表示当前的分支所在的版本。右键分支选择checkout即可切换分支。
8.7 创建、切换分支
创建分支
:点击右下角的红框内容然后输入创建的分支名,勾选第一个后会在创建之后自动切换分支。
切换分支
:同样点击右下角,点击master分支的checkout即可进行分支切换。
8.8 合并分支
点击右下角合并到当前分支即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步