git 的基本使用

git config命令查看用户名,邮箱
git config user.name
git config user.email

git config命令 修改自己的用户名和邮箱


//仅对当前仓库有效
git config --local user.email "你的名字"
git config --local user.email "你的邮箱"

//对当前用户的所有仓库有效
git config --global user.email "你的名字"
git config --global user.email "你的邮箱"
ssh-keygen -t rsa -C "Account email"
原文链接:https://blog.csdn.net/weixin_42988712/article/details/124689681

idea 操作git仓库

idea 创建git仓库,在IDEA中对建项目,选择VCS - > Import into Version Control -> Create Git Repository
,例如这里选择了项目源代码同目录,完成本地仓库的创建。

Branches 创建分支

在本地仓库创建分支,名称fenzhi01

idea 提交

IDEA中对操作做了一定的简化,commit和push可以在一步中完成。

idea 移除git本地仓库

https://blog.csdn.net/weixin_45967322/article/details/119832440

1.IDEA中移除本项目git配置
File -> Settings -> Version Control 选择减号,并移除当前项目的git配置
可以看到项目文件颜色标记消失 右上角也没有了git标识。
2.删除.git及相关文件
找到项目目录并删除.git和.idea文件夹,以及.gitignore配置文件,git的信息才算全部清除。为了安全起见,执行删除操作前先关闭IDEA。
3.再次启动IDEA

Idea开发工具操作git回滚提交步骤

https://blog.csdn.net/wxw1997a/article/details/120179416

IDEA将module纳入Git管理的方法
https://blog.csdn.net/u012069313/article/details/123543639

日常修改代码提交

日常修改代码提交
1.在github创建仓库,然后就会有主分支;
2.本地新建一个文件夹,初始化本地仓库:git init ;
3.与远程仓库建立连接:git remote add origin 仓库地址 ;
4.如果要将代码直接提交到主分支,则直接执行git add ...;

5. 修改文件提交,提交时加上参数 -a 表示新增。
PS D:\repo\orderTest1> git commit -am "1.2" 
[master 1ecaa30] 1.2
 1 file changed, 3 insertions(+)

$ git push origin master

git 的基本命令

$ cd 当前项目目录
$ git init
$ git remote add origin [git仓库地址]
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master -f

创建远程仓库

删除git本地仓库
https://blog.csdn.net/weixin_46314847/article/details/126417599
真正删除项目的git信息,还需要到项目的根目录中,删除两个目录:.git 和 .idea

git init 初始化本地仓库

git init 初始化一个本地仓库,执行该命令后会在很低初始化一个没有任何文件的空仓库。
生成.git文件夹。

tree -a 可查看.git文件目录树

git add

git add .
git add pom.xml 添加一个文件

git remote add

git remote add origin 连接远程仓库

与远程仓库建立连接:git remote add origin 仓库地址
git remote add origin https://e.coding.net/p8test/lydpro1/orderTest.git

问题 git remote origin allready exists
git remote rm origin
git remote add origin https**
add、commit、push
git与提交有关的三个命令对应的操作:
add 把文件从IDE的工作目录添加到本地仓库的stage区。
commit 把stage区的暂存文件提交到当前分支的仓库,并清空stage区。
push 把本地仓库的提交同步到远程仓库。

git commit -m "commit 2"
git push -u origin master
git push origin master
git push --force origin master

添加一个src目录及文件
在这里插入图片描述

git pull

git pull 命令用于从远程获取代码并合并本地的版本。
git pull 命令是 git fetch 和 git merge 命令的组合,git 从指定的远程仓库中抓取内容,然后马上尝试将其合并进你所在的分支中。

$ git pull
$ git pull origin

将远程主机 origin 的 master 分支拉取过来,与本地的 brantest 分支合并。
git pull origin master:brantest

如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
git pull origin master 命令表示,取回 origin/master 分支,再与本地的 master 分支合并。

$ git pull origin master
From https://e.coding.net/p8test/lydpro1/p8-orderTest1
 * branch            master     -> FETCH_HEAD

git fetch

git fetch [remote-name] 从远程仓库中获得数据

git fetch 并不会自动合并或修改你当前的工作。 当准备好时你必须手动将其合并入你的工作。

git中pull和fetch的区别是什么
Git fetch和git pull区别为:远端跟踪分支不同、拉取不同、commitID不同。

一、远端跟踪分支不同
1、Git fetch:Git fetch能够直接更改远端跟踪分支。
2、git pull:git pull无法直接对远程跟踪分支操作,我们必须先切回本地分支然后创建一个新的commit提交。

二、拉取不同
1、Git fetch:Git fetch会将数据拉取到本地仓库 - 它并不会自动合并或修改当前的工作。
2、git pull:git pull是从远程获取最新版本并merge到本地,会自动合并或修改当前的工作。

三、commitID不同
1、Git fetch:使用Git fetch更新代码,本地的库中master的commitID不变,还是等于1。
2、git pull:使用git pull更新代码,本地的库中master的commitID发生改变,变成了2。

fetch 和 pull 两个命令
fetch是从远程仓库下载文件到本地的origin/master,然后可以手动对比修改决定是否合并到本地的master库。pull则是直接下载并合并。

拉取覆盖

将远程代码强制拉取同步(覆盖)到本地文件夹

git fetch --all
git reset --hard origin/master
git pull
git checkout 丢弃所有本地未提交的修改

git checkout .
丢弃本地改动
如果本地的修改不重要,那么可以直接把本地的的修改丢弃:

不丢弃操作

问题 error: Your local changes to the following files would be overwritten by merge:

解决方法:如果你想保留本地新修改的代码,同时想把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来,不想修改的代码被覆盖掉)看了git的手册,发现可以这样解决:

git stash # 暂存到堆栈区
git pull origin master
git stash pop


# 查看stash内容
git stash list

git clone

如果你使用 clone 命令克隆了一个仓库,命令会自动将其添加为远程仓库并默认以 “origin” 为简写。 所以 git fetch origin 会抓取克隆(或上一次抓取)后新推送的所有工作。

git branch 创建分支

建立本地分支Testbranch(分支名):git branch Testbranch 。 或者 git checkout -b 分支名 (直接创建分支并切换到分支) ;

问题:提交git仓库的时候遇到问题,提示Your branch is up-to-date with 'origin/master'.

需要新建一个分支 git branch newBranch
检查分支是否创建成功 git branch(前面的*代表的是当前你所在的工作分支)
* master
  newBranch

切换到你的新分支git checkout newBranch
Switched to branch 'newBranch'

将你的改动提交到新分支上
git add .
git commit -m "v2.1"
On branch newBranch
nothing to commit, working tree clean

然后切换到主分支 git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

将新分支提交的改动合并到主分支上 git merge newBranch
Already up to date.

PS D:\p8-repo\p8-orderTest1> git push -u origin master
Everything up-to-date
branch 'master' set up to track 'origin/master'.

最后还可以删除这个分支 git branch -D newBranch
Deleted branch newBranch (was 9beb881).

git merge 合并代码

git merge 合并服务器上的代码与本地代码;
git merge origin/test #合并远程分支test到当前分支;
切换到本地当前某个分支,执行该命令,成功后再点击push

git commit
git commit --amend  追加提交,它可以在不增加一个新的commit-id的情况下将新修改的代码追加到前一次的commit-id中
git push

把本地合并后的master push到远程master上

1.进入一个文件夹testproject
2.git init
3.git remote add origin http://192.168.1.11:testproject
4.git pull http://192.168.1.11:testproject
5.git add .
6.git commit -m “new file 1”
7.git push -u origin master

------------------------------------------------------------------------------------
git push 的 -u 参数含义
https://blog.csdn.net/Lakers2015/article/details/111318801

带上-u 参数其实就相当于记录了push到远端分支的默认值,这样当下次我们还想要继续push的这个远端分支的时候推送命令就可以简写成git push即可。
git reset 撤回

问题

idea连接remote地址提示失败 Failed to connect to 172.17.18.80 port 8080: Operation timed out
解决方法:使用cmd命令创建项目,连接remote后,把idea重启

[rejected] master->master (non-fast-forward)
https://blog.csdn.net/zyz00000000/article/details/84402189

refusing to merge unrelated histories的解决方案

gitee

制作token

git生成ssh公钥(ssh-keygen)

配置URL地址

git remote set-url origin https://gitee.com/zhangsan/my-cloud.git

提交到gitee仓库失败处理

D:\my-cloud>git push origin master
To https://gitee.com/zhangsan/my-cloud.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/zhangsan/my-cloud.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

1.使用 git fetch origin master 先合并之前的历史,在进行提交

D:\my-cloud>git fetch origin master
From https://gitee.com/zhangsan/my-cloud
 * branch            master     -> FETCH_HEAD

D:\my-cloud>git merge origin FETCH_HEAD
merge: origin - not something we can merge

2.使用 git pull --rebase origin master

D:\my-cloud> git pull --rebase origin master
From https://gitee.com/lyndon1107/v5-cloud
 * branch            master     -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
error: could not apply 60c82d5... 1
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 60c82d5... 1
git branch 提示“no branch, rebasing master”

出现这个错误是因为本地提交了commit但是未push成功,所以使用git pull --rebase,由于远程仓库和本地的commit有冲突,Git无法自动解决冲突时,会切换到一个匿名分支,然后使用git branch发现报错“no branch, rebasing master”。

解决办法:
在当前匿名分支下,解决完冲突,然后使用命令git rebase --continue,可以将代码合并到之前操作时的分支(我当时是在master分支所以后面都说master分支),执行完git rebase --continue后就自动回到了master分支,但是之前提交的commit没有在本地仓库区(Repository)了,回到了本地暂存区(Index / Stage),这时候需要重新commit,之后再执行git push origin master就可以将变更推到远程仓库了。

https://blog.csdn.net/weixin_43971373/article/details/119729483

git pull --rebase
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

命令git rebase --continue
You must edit all merge conflicts and then
mark them as resolved using git add

D:\my-cloud>git add .

D:\my-cloud>git commit -m "3"
[detached HEAD f101534] 3
 1 file changed, 1 insertion(+)
D:\my-cloud>git push -u origin master
Enumerating objects: 1194, done.
Counting objects: 100% (1194/1194), done.
Delta compression using up to 8 threads
Compressing objects: 100% (1119/1119), done.
Writing objects: 100% (1192/1192), 17.13 MiB | 877.00 KiB/s, done.
Total 1192 (delta 404), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (404/404), completed with 1 local object.
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/zhangsan/my-cloud.git
   efa74d1..f101534  master -> master
branch 'master' set up to track 'origin/master'.

其他

集中式和分布式版本控制系统的区别:

集中式版本控制系统,版本库是集中存放在中央服务器的,工作的时候,用的是自己的电脑,所以,我们首先需要,从中央服务器上拉取最新的版本。

集中式版本控制系统的一个最大毛病就是必须联网才能工作,所以对于网络环境比较差的情况使用集中式版本控制系统是一件比较让人头疼的事情。

分布式版本控制系统没有中央服务器的概念,我们使用相关的客户端提取的不只是最新的文件,而是把代码仓库完整地镜像下来,相当于每个人的电脑都是一个完整的版本库,这样的话,任何一处协同工作的服务器出现故障,都可以用任何一个镜像出来的本地仓库恢复。并且,即便在网络环境比较差的情况下也不用担心,因为版本库就在本地电脑上。

个人总结:
(1).分布式版本控制系统下的本地仓库包含代码库还有历史库,在本地就可以查看版本历史
(2).而集中式版本控制系统下的历史仓库是存在于中央仓库,每次对比与提交代码都必须连接到中央仓库
(3).多人开发时,如果充当中央仓库的Git仓库挂掉了,任何一个开发者都可以随时创建一个新的中央仓库然后同步就可以恢复中央仓库

Git、GitHub、GitLab三者之间的联系以及区别
Git 是一个版本控制系统。记录一个或多个文件内容变化,方便我们查阅特定版本修订情况的系统。
Git 已经成为越来越多开发者的青睐,因为分布式的优势是很显著的。
Git 的三个概念,提交commit 仓库repository 分支branch

Gitee 即码云,是 oschina 免费给企业用的,不用自己搭建环境,可以建立自己的私有仓库。
是由开源中国社区推出的基于git的代码托管服务平台。目前招商银行,中国科学技术大学,CSDN等多家企业和机构都在使用码云平台。

GitHub 和 GitLab 都是基于 web 的 Git 仓库,使用起来二者差不多,它们都提供了分享开源项目的平台,为开发团队提供了存储、分享、发布和合作开发项目的中心化云存储的场所。
GitHub是一个面向开源及私有软件项目的托管平台,只支持git作为唯一的版本库格式进行托管。
GitHub 如果使用私有仓库,是需要付费的。

GitLab 可以在上面创建私人的免费仓库。
GitLab 让开发团队对他们的代码仓库拥有更多的控制,相比较 GitHub , 它有不少特色:
(1)允许免费设置仓库权限;
(2)允许用户选择分享一个 project 的部分代码;
(3)允许用户设置 project 的获取权限,进一步提升安全性;
(4)可以设置获取到团队整体的改进进度;
(5)通过 innersourcing 让不在权限范围内的人访问不到该资源;
所以,从代码的私有性上来看,GitLab 是一个更好的选择。但是对于开源项目而言,GitHub 依然是代码托管的首选。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值