Git学习记录

目录

Git

Git介绍

版本控制

版本控制工具

集中式版本控制工具

分布式版本控制工具

Git工作机制

​编辑

Git和代码托管中心

Git安装

Git常用命令

设置用户签名

初始化本地库

查看本地库状态

添加到暂存区

提交到本地库

修改文件

历史版本

查看历史版本

版本穿梭

Git分支

分支的操作:

Git团队协作借助机制

团队内协作

跨团队协作

GitHub

GitHub介绍

创建远程仓库

远程仓库操作

创建远程仓库别名

推送本地分支到远程仓库

从远程仓库拉取文件到本地

克隆远程仓库到本地

邀请加入团队

跨团队协作

SSH免密登录

idea集成git

配置git忽略文件

定位Git程序

idea中git操作

操作:

初始化:

添加暂存区:

提交本地库:

切换版本:

创建分支:

切换分支:

合并分支:

idea集成github

集成操作:

将项目分享到远程仓库:

推送文件到远程仓库:

拉取文件到远程仓库:

克隆远程仓库:


Git

Git介绍

Git官网:git-scm.com

Git是一个免费的,开源的分布式版本控制系统,可以快速高效的处理各种项目。

Git易于学习,体积小,性能极快。它具有廉价的本地库,方便的暂存区和多个工作流分支等特性。

版本控制

版本控制是一种记录文件内容变化,以便将来查阅特定版本修订情况的系统。

版本控制最重要的是可以记录文件修改历史记录,从而让用户能够查看历史版本,方便版本切换。

版本控制可以帮助我们从个人开发过渡到团队协作。

版本控制工具

集中式版本控制工具

CVS、SVN(Subversion)、VSS……

集中式的版本控制系统把所有文件的修订版本都保存在一个集中管理的服务器,协同工作的人都通过客户端连接到这台服务器上,取出最新的文件或者提交更新。

  • 优点:
    • 管理员可以轻松控制每个开发者的权限
    • 开发者可以看到其他开发者的操作
    • 管理版本更加轻松
  • 缺点:
    • 中央服务器的单点故障后开发者都无法获取更新或拉取
分布式版本控制工具

Git、Mercurial、Bazaar、Darcs……

分布式的版本控制系统中,客户端提交的不是最新版本的文件快照,而是把代码仓库完整地镜像到本地库。这样一处的故障可以使用另一处的文件进行修复。实际上每一次的文件拉取都是对整个文件仓库的完整备份。

分布式版本控制系统解决了集中式版本控制的缺陷:

1.服务器断网也可以进行开发(因为版本控制都是在本地库上操作)

2.每个客户端都保存完整项目(包含历史记录,更加安全)

分布式版本控制系统也有一个中央服务器,每个客户端可以选择将编辑后文件push上去,这样中央服务器内的文件就是最新的了。

Git工作机制

Git和代码托管中心

代码托管中心是基于网络服务器的远程代码仓库,一般简单地称为远程库。

局域网:

        GitLab

互联网:

        GitHub(外网)

        Gitee(国内)

Git安装

下载下来后一路默认到安装即可。

Git常用命令

命令名称 作用
git config --global user.name 用户名 设置用户签名
git config --global user.email 邮箱 设置用户签名
git init 初始化本地库
git status 查看本地库状态
git add 文件名 添加到暂存区
git commit -m "日志信息" 文件名 提交到本地库
git reflog 查看历史记录
git reset --hard 版本号 版本穿梭

设置用户签名

安装后用户签名只需要设置一次,不然提交代码时会报错

基本语法:

        git config --global user.name 用户名

        git config --global user.email 邮箱

验证用户签名:

        C:\用户\当前使用的用户 中有一个 .gitconfig 文件 用户签名就设置在里面

签名作用:

        版本控制时可以通过签名确认哪个版本是谁提交的

注意:

        现在设置的用户名和邮箱和将来登录GitHub(或其他)的账号无任何关系

初始化本地库

要git管理一个目录,就先要git获取到该目录的管理权(即初始化)

基本语法:

        git init

实际操作:

        打开要被git管理的文件夹:如 D:\git\git-test

        右键点击:Git Bash Here

        输入:git init

        这样目录下就会有 .git 的隐藏文件夹了,就代表初始化成功

查看本地库状态

基本语法:

        git status

首次查看:(工作区未有文件)

$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

添加文件(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)

添加到暂存区

要追踪给文件就要将它放到暂存区中,就要使用:git add 命令了 

基本语法:

        git add 文件名

$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it

warining无所谓的,这时候在查看状态:

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt

表示当前git已经追踪到这个文件,但此时文件是可以移除追踪的,使用命令:

        git rm --cachea 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)

提交到本地库

暂存区的文件并没有形成版本,需要把他提交到本地库:git commit -m "日志信息" 文件名

基本语法:

        git commit -m "日志信息" 文件名(记得先添加到暂存区)

$ git commit -m "my first commit" hello.txt
[master (root-commit) 74da8e6] my first commit
 1 file changed, 18 insertions(+)
 create mode 100644 hello.txt

提交之后,查看状态:(显示没有东西需要提交)

$ git status
On branch master
nothing to commit, working tree clean

 git reflog可以看版本,后面会详细讲

$ git reflog
74da8e6 (HEAD -> master) HEAD@{0}: commit (initial): my first commit

修改文件

21111113333333333333 hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!·

查看状态:(显示hello.txt文件被修改且未被添加暂存区)

$ 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 ,添加后再次查看:

$ 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,提交后再次查看:

$ git status
On branch master
nothing to commit, working tree clean

使用git reflog:

$ git reflog
11cd1c6 (HEAD -> master) HEAD@{0}: commit: my second commit
74da8e6 HEAD@{1}: commit (initial): my first commit

历史版本

查看历史版本

基本语法:

        git reflog 查看版本信息

        git log 查看版本详细信息

$ git reflog
11cd1c6 (HEAD -> master) HEAD@{0}: commit: my second commit
74da8e6 HEAD@{1}: commit (initial): my first commit

reflog:11cd1c6:极简版版本号;(HEAD -> master):指针指向哪个版本号

            my second commit:日志信息

$ git log
commit 11cd1c6f93ab9fa729f251a996fbcfe5acf59f04 (HEAD -> master)
Author: Xxxxx <Xxxxxxx@qq.com>
Date:   Fri Mar 15 00:58:09 2024 +0800

    my second commit

commit 74da8e66d05aea30c15b81f1b0b6da54eead6953
Author: Xxxxx <Xxxxxxx@qq.com>
Date:   Fri Mar 15 00:46:16 2024 +0800

    my first commit

log: 11cd1c6f93ab9fa729f251a996fbcfe5acf59f04:完整版本号

        Author: Xxxxx <Xxxxxxx@qq.com>:提交者

        Date:   Fri Mar 15 00:58:09 2024 +0800:提交日期

        my second commit 日志信息

        (HEAD -> master):指针指向哪个版本号

版本穿梭

如果对最新版本不满,可以改回之前的版本

基本语法:

        git reset --hard 版本号

查看版本:

$ git reflog
11cd1c6 (HEAD -> master) HEAD@{0}: commit: my second commit
74da8e6 HEAD@{1}: commit (initial): my first commit

版本穿梭:(74da为版本号前几位,不一定是4位,只要能区分版本,但似乎需要4位以上)

$ git reset --hard 74da
HEAD is now at 74da8e6 my first commit

查看版本:

$ git reflog
74da8e6 (HEAD -> master) HEAD@{0}: reset: moving to 74da
11cd1c6 HEAD@{1}: commit: my second commit
74da8e6 (HEAD -> master) HEAD@{2}: commit (initial): my first commit

查看文件:(以回到第一个版本)

$ cat hello.txt
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!

在 .git 文件夹下有个 HEAD 文件,打开后看到:ref: refs/heads/master,说明当前是在master分支上,在 .git/refs/heads 文件夹下有个master 打开后看到:74da8e66d05aea30c15b81f1b0b6da54eead6953 这个是当前版本的版本号

底层逻辑是指针的引用:head -> master -> 版本号

Git分支

版本控制时,对于每个任务,我们可以创建每个任务的单独分支。开发者可以把自己的工作从开发主线上分离开来,开发自己分支时不会影响主线的运行。简单来说,开发分支相当于把主线复制一份给自己,修改好了再提交到主线。(分支的底层也是指针的引用)

分支的优点:

        同时推进多个功能开发,通过开发效率。

        一个分支的失败不会影响另外的分支,只需要把失败的分支删除。

分支的操作:

命令名称 作用
git branch 分支名 创建分支
git branch -v 查看分支
git checkout 分支名 切换分支
git merge 分支名 把指定分支合并到当前分支上

查看分支

基本语法:

        git branch -v

$ git branch -v
* master 74da8e6 my first commit

创建分支

基本语法:

        git branch 分支名

$ git branch hot-fix

创建后查看分支:

$ git branch -v
  hot-fix 74da8e6 my first commit
* master  74da8e6 my first commit

修改分支:

基本语法:

        git checkout 分支名

当前:

Xxx@Xxxxxx MINGW64 ~/Desktop/git-test (master)

使用命令后:

Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (hot-fix)
$ git branch -v
* hot-fix 74da8e6 my first commit
  master  74da8e6 my first commit

使用hot-fix修改文件后提交:

$ cat hello.txt
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!

查看版本:

$ git reflog
e65054b (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix first commit
74da8e6 (master) HEAD@{1}: checkout: moving from master to hot-fix
74da8e6 (master) HEAD@{2}: reset: moving to 74da
11cd1c6 HEAD@{3}: reset: moving to 11cd
11cd1c6 HEAD@{4}: commit: my second commit
74da8e6 (master) HEAD@{5}: commit (initial): my first commit

此时.git 下文件HEAD内容为:ref: refs/heads/hot-fix

        .git/refs/heads 下 hot-fix 内容为:e65054b2627a1c162dcc8850d0a01cbb52ef2ce4

                                    master内容为:74da8e66d05aea30c15b81f1b0b6da54eead6953
分支合并:

先切换为master分支,查看hello.txt文件,发现文件未有变化:

Xxx@Xxxxxx MINGW64 ~/Desktop/git-test (master)
$ cat hello.txt
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!

当我们在master分支时可以使用分支合并将hot-fix分支修改的部分合并。

基本语法:

        git merge 分支名

$  git merge hot-fix
Updating 74da8e6..e65054b
Fast-forward
 hello.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Xxx@Xxxxxx MINGW64 ~/Desktop/git-test (master)
$ cat hello.txt
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!

产生冲突:

分支合并时,当两个分支在同一个文件的同一个位置有两种完全不同的修改。Git无法自动选择,需要人为决定新代码的内容。

在master分支上修改hello.txt,在最后一行添加 hello master,并把它提交:

$ cat hello.txt
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello master

在hot-fix分支上修改hello.txt,在最后一行添加 hello hot-fix,并把它提交:

$ cat hello.txt
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello hot-fix

切换为master分支,使用分支合并命令:

查看状态:

$ 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")

both modified :hello.txt  -----  两者修改了hello.txt

此时需要我们手动修改:

vim hello.txt 打开后:

hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
<<<<<<< HEAD
hello master
=======
hello hot-fix
>>>>>>> hot-fix
~

修改为:

$ cat hello.txt
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello master
hello hot-fix

修改后要再次提交:(注意:此时add时可以有文件名,但提交时没有有文件名,不然会报错)

Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (master|MERGING)
$ git commit -m "master 2 merge" hello.txt
fatal: cannot do a partial commit during a merge.

Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (master|MERGING)
$ git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        modified:   hello.txt


Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (master|MERGING)
$ git commit -m "master 2 merge" hello.txt
fatal: cannot do a partial commit during a merge.

Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (master|MERGING)
$ git commit -m "master 2 merge"
[master 7f4094d] master 2 merge

注意:在master分支下手动进行合并,合并结果只在master分支下,hot-fix分支并没有合并:

Xxx@Xxxxxxx MINGW64 ~/Desktop/git-test (hot-fix)
$ cat hello.txt
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello hot-fix

Git团队协作借助机制

团队内协作

跨团队协作

GitHub

GitHub介绍

GitHub官网:https://github.com/

创建远程仓库

先打开github官网并登录,

远程仓库操作

命令名称         作用
git remote -v 查看当前所有远程地址别名
git remote add 别名 远程地址 起别名
git push 别名 分支 推送本地分支上的内容到远程仓库
git clone 远程地址 将远程仓库的内容克隆到本地
git pull 远程仓库地址别名 远程分支名 将远程仓库对于分支最新内容拉取后与当前本地分支直接合并

远程仓库地址(https):https://github.com/Xxxxx/git-test.git

创建远程仓库别名

基本语法:

        git remote -v 查看当前所有远程地址别名

        git remote add 别名 远程地址

给git-test地址起别名:

$ git remote add git-test https://github.com/Xxxxx/git-test.git

查看别名:

$ git remote -v
git-test        https://github.com/qiuzhifeng666/git-test.git (fetch)
git-test        https://github.com/qiuzhifeng666/git-test.git (push)

两个别名:一个是拉取用的,一个是推送用的。

推送本地分支到远程仓库

基本语法:

        git push 别名 分支

把master分支推送到远程仓库:

git push git-test master

点击Sign in with your browser进行登录,登录成功后显示推送成功:

$ git push git-test master
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (15/15), 1.09 KiB | 1.09 MiB/s, done.
Total 15 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/Xxxxxxxx/git-test.git
 * [new branch]      master -> master

推送成功后刷新github界面:

从远程仓库拉取文件到本地

基本语法:

        git pull 别名 分支

如果别的开发者将远程仓库的文件覆盖修改了,那本地库可以通过拉取文件来获得最新的文件:

将文件修改为:

在本地库上使用命令:git pull git-test master

$ git pull git-test 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 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 932 bytes | 84.00 KiB/s, done.
From https://github.com/Xxxxxxxx/git-test
 * branch            master     -> FETCH_HEAD
   7f4094d..6f25233  master     -> git-test/master
Updating 7f4094d..6f25233
Fast-forward
 hello.txt | 1 +
 1 file changed, 1 insertion(+)

 查看文件:

$ cat hello.txt
111111111111111111111111111111
hello qiu! hello git! hot-fix
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello qiu! hello git!
hello master
hello hot-fix

克隆远程仓库到本地

基本语句:

        git clone 远程仓库地址

将远程仓库的文件克隆到一个工作区:

先创建一个工作区:创建git-test2文件夹

获取远程仓库的https地址: https://github.com/Xxxxxxxx/git-test.git

在git-test2文件夹下打开git bash窗口,输入命令:git clone https://github.com/Xxxxxxxx/git-test.git

克隆任何远程仓库是不需要凭证的,因为拿得到远程仓库地址说明该仓库是开源的。

命令执行完毕后git-test2文件夹下会多一个git-test文件夹,这个就是克隆下来的完整文件。

进入克隆下来的git-test文件夹,输入命令查看别名:

$ git remote -v
origin  https://github.com/Xxxxxxxx/git-test.git (fetch)
origin  https://github.com/Xxxxxxxx/git-test.git (push)

clone操作完成以下功能:1.拉取代码        2.初始化本地仓库        3.创建别名(默认为origin)

邀请加入团队

假设老板创建一个远程仓库(git-test)

你(员工)去克隆这个远程仓库,修改代码完成后再推送到远程仓库时,即使你已经获得了当前账号凭证(推送文件需要远程仓库管理系统(如github)的凭证),仍然无法推送成功,因为你还没有加入到这个远程仓库(git-test)的队伍中。

该操作由远程仓库管理者(老板)来执行:

 邀请涵:https://github.com/Xxxxxxxx/git-test/invitations

员工在github网站登录后输入链接

跨团队协作

需要其他团队来修改当前文件。

现在代码出现问题,需要其他团队的协助,这时让其他团队的工作人员在远程仓库管理系统(github)中找到本团队的远程仓库。

找到已修改的仓库:

此时自己就会有这个仓库,将仓库内文件修改后提交,解决问题后可以发送一个pull request:

此时本团队的远程仓库:

 合并:

SSH免密登录

拉取文件还有一种方式:SSH

但要使用SSH,需要有“钥匙”:

在C:\用户\XXX(当前用户)下打开git bash窗口(如果有.ssh文件夹了,可以删掉重新来一遍):

使用命令生成 .ssh 秘钥目录:

ssh-keygen -t rsa -C Xxxxxxxxx

解析:ssh-keygen:生成秘钥      -t:使用哪种加密方式生成秘钥        -C:描述

输入命令之后,敲3次回车(即默认选择),

在打开C:\用户\XXX 目录会发现多了一个 .ssh 文件夹,文件夹中有两个文件:公钥(id_rsa.pub)和私钥(id_rsa)。

把公钥复制一份,在github打开用户的setting,打开SSH and GPG keys:

添加成功

使用SSH拉取文件:

idea集成git

配置git忽略文件

使用idea这类工具时会生成一些 .iml文件 或 .idea 文件夹等不需要的文件,这时就要配置git忽略文件来使到时候git不去控制这些文件(添加,提交,推送)。

配置方法:

创建忽略规则文件:xxxx.ignore(前缀名无所谓,推荐:git.ignore)

该文件放哪都行,但为了方便让 ~/.gitconfig文件引用,建议也放在用户名家目录下

在C:\用户\XXX(当前用户)下创建文件:git.ignore,将以下模板输入:


# 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

hs_err_pid*

.classpath
.project
.settings
target
.idea
*.iml

创建文件后还需要在 .gitconfig 中引用该文件:

还是在用户家目录:C:\用户\XXX 下有 .gitconfig添加一下信息:

[core]
	excludesfile = C:/Users/XXX/git.ignore

这里必须使用 " / " ,不能使用 " \ "

定位Git程序

配置好git忽略文件后,还需要定位git程序才能在idea中使用git

idea中git操作

idea集成git后就可以在idea进行git操作了

操作:

初始化:

创建git的本地库:在idea中双击shift,输入:import into version control,选择create git repository,选择项目的根目录。

添加暂存区:

已添加到暂存区:

提交本地库:

修改后再次提交会显示相关内容:

切换版本:

创建分支:

切换分支:

合并分支:

正常合并:

合并冲突:

master分支进行修改并提交:

hot-fix分支进行修改并提交:

分支合并:

合并冲突,需要手动合并:

合并成功:

idea集成github

集成操作:

没有的可以去插件商城安装。

设置github账号:(使用token登录)

在github网站上打开用户的setting,

验证完账号密码后,

在idea中输入口令就完成了。

将项目分享到远程仓库:

设置好别名后提交即可生成远程仓库。

推送文件到远程仓库:

push操作中,本地库的版本一定要高于远程仓库的版本。因此,在修改文件前一定要先拉取远程仓库代码。

修改文件并提交后:

拉取文件到远程仓库:

克隆远程仓库:

相关推荐

  1. 学习记录25】学习一些比较有用的git命令

    2024-03-17 06:58:01       28 阅读
  2. BERT、GPT学习问题个人记录

    2024-03-17 06:58:01       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 06:58:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 06:58:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 06:58:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 06:58:01       20 阅读

热门阅读

  1. 七大排序(简洁思路版)

    2024-03-17 06:58:01       21 阅读
  2. Dart学习相关

    2024-03-17 06:58:01       17 阅读
  3. 常用图像滤波器,图像增强

    2024-03-17 06:58:01       23 阅读
  4. MVCC(多版本并发控制)原理实现

    2024-03-17 06:58:01       18 阅读