新手Git+码云入门笔记

news/2024/10/22 11:07:30/

1.什么是Git?
2.什么是码云?
3.如何使用Git?
4.如何使用Git+码云实现代码管理?

什么是Git?

Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。(来自百度百科Git)

什么是码云?

码云(gitee.com)是 OSCHINA.NET 推出的代码托管平台,支持 Git 和 SVN,提供免费的私有仓库托管。可以理解为国内版的GitHub。相比GitHub来说,优点是不用科学上网,速度较快,更稳定;缺点是码云的开源项目较少,毕竟起步晚。
直接进入官网注册登录即可,后面步骤中要将本地项目推送到自己的码云代码仓库中。

如何使用Git?(以Windows系统为例)

1.下载并安装Git(官网下载速度较慢,可以选择国内的一些网站下载),这里提供一个超链接查看下载安装Git的详细操作流程。
2.Git使用
在你的代码根目录下,右键->Git Bash Here,在终端输入Git命令,首次使用建议在测试代码目录运行,熟练后再到正式代码目录使用,不放心的话先把正式代码备份一下。
安装后需要配置用户名和邮箱:

$ git config --global user.name "Your Name"  //Your Name替换成自己的
$ git config --global user.email "email@example.com"  //邮箱替换成自己的

使用 --global 修饰后设置的全局的用户,如果设置单个项目的用户,可cd到项目根目录下,执行如下命令:

$ git config user.name "Your Name"  //命令前的"$"是终端自带的,不用输入$
$ git config user.email "email@example.com"  

3.建立本地Git仓库(此处引用文章:https://www.cnblogs.com/jackchensir/p/8306448.html)
1.cd到你的项目目录(或者直接在项目目录右键->Git Bash Here)

$ cd /Users/cjk/Desktop/myShop

2.然后,输入git命令:

$ git init  

输出如下:

$ git init  
Initialized empty Git repository in /Users/cjk/Desktop/GitTest/.git/  

创建了一个空的本地仓库.

3.将项目的所有文件添加到缓存中:

$ git add .  

git add . (注意,后面有个点)表示添加目录下所有文件到缓存库,如果只添加某个文件,只需把 . 换成你要添加的文件名即可;

4.将缓存中的文件Commit到git库

git commit -m "添加你的注释,一般是一些更改信息"

下面是第一次提交时的输出:

$ git commit -m "添加项目"
[master (root-commit) 3102a38] 添加项目18 files changed, 1085 insertions(+)create mode 100644 GitTest.xcodeproj/project.pbxprojcreate mode 100644 GitTest.xcodeproj/project.xcworkspace/contents.xcworkspacedatacreate mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstatecreate mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/GitTest.xcschemecreate mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/xcschememanagement.plistcreate mode 100644 GitTest/AppDelegate.hcreate mode 100644 GitTest/AppDelegate.mcreate mode 100644 GitTest/Assets.xcassets/AppIcon.appiconset/Contents.jsoncreate mode 100644 GitTest/Base.lproj/LaunchScreen.storyboardcreate mode 100644 GitTest/Base.lproj/Main.storyboardcreate mode 100644 GitTest/Info.plistcreate mode 100644 GitTest/ViewController.hcreate mode 100644 GitTest/ViewController.mcreate mode 100644 GitTest/main.mcreate mode 100644 GitTestTests/GitTestTests.mcreate mode 100644 GitTestTests/Info.plistcreate mode 100644 GitTestUITests/GitTestUITests.mcreate mode 100644 GitTestUITests/Info.plist

或者不添加注释 git commit ,但是这样会进入vim(vi)编辑器

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#       modified:   LQQCircleShowImage.xcodeproj/project.pbxproj
#       modified:   LQQCircleShowImage/TableViewCell.m
#
~                                                                                                                                                       
"~/Desktop/LQQCircleShowImage/.git/COMMIT_EDITMSG" 8L, 292C

在这里可以输入更改信息,也可以不输入,然后 按住 shift + : ,输入wq 即可保存信息并退出vim编辑器;

如何使用Git+码云实现代码管理?

在一些代码托管平台创建项目,例如github或者开源中国社区,这里以开源中国社区为例;
1.创建项目后,会生成一个HTTPS链接,如下:
在这里插入图片描述
复制仓库地址:https://gitee.com/***/(复制自己仓库的地址)
2.将本地的库链接到远

终端中输入: git remote add origin HTTPS链接

$ git remote add origin https://gitee.com/***/*** //自己的仓库地址

3.上传代码到远程库,上传之前最好先Pull一下,再执行命令: git pull origin master

输出:

$ git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://git.oschina.net/liuqiqiang/gitTest* branch            master     -> FETCH_HEAD* [new branch]      master     -> origin/master
Merge made by the 'recursive' strategy.README.md | 1 +1 file changed, 1 insertion(+)create mode 100644 README.md

即pull成功

4.接着执行:git push origin master

完成后输出:

$ git push origin master
Counting objects: 34, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (29/29), done.
Writing objects: 100% (34/34), 15.63 KiB | 0 bytes/s, done.
Total 34 (delta 3), reused 0 (delta 0)
To https://git.oschina.net/liuqiqiang/gitTest.git5e2dda1..537ecfe  master -> master

即将代码成功提交到远程库!!!

注:如果pull之后出现 “ refusing to merge unrelated histories ”这句,就证明你合并pull两个不同的项目

出现的问题如何去解决fatal: refusing to merge unrelated histories

我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传。

先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法pull

因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在git pull,这句代码是在git 2.9.2版本发生的,最新的版本需要添加–allow-unrelated-histories

假如我们的源是origin,分支是master,那么我们 需要这样写git pull origin master --allow-unrelated-histories需要知道,我们的源可以是本地的路径

注意:操作的时候,指令不要输错了!!!

下面这个是输错了 orgin的输出:

git pull orgin master
fatal: 'orgin' does not appear to be a git repository
fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.

正确的应该是origin!!

如果在push的时候有如下输出:

$ git push -u origin master
To https://git.oschina.net/liuqiqiang/LQQCircleShowImage.git! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://git.oschina.net/liuqiqiang/LQQCircleShowImage.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

看提示可知道,需要先pull一下,即执行一次:git pull origin master

然后再执行:git push origin master

到这里,恭喜你已经完成了代码使用Git+码云的管理了,接下来是一些Git常用命令。

分支管理:

新建分支

$ git branch newbranch

查看分支

$ git branch

输出:

* masternewbranch

*代表当前所在的分支

切换分支

$ git checkout new branch

输出

Switched to branch 'newbranch'

切换后可用git branch查看是否切换到当前分支

master
* newbranch

提交改动到当前分支

$ git add .
$ git commit -a

可使用git status查看提交状态

接着切回主分支

$ git checkout master

输出:

Switched to branch 'master'

将新分支提交的改动合并到主分支上

$ git merge newbranch

输出:

Updating cc73a48..93a1347
Fast-forwardGitTest.xcodeproj/project.pbxproj                        |   9 +++++++++.../UserInterfaceState.xcuserstate                       | Bin 0 -> 7518 bytesGitTest/test.h                                           |  13 +++++++++++++GitTest/test.m                                           |  13 +++++++++++++4 files changed, 35 insertions(+)create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstatecreate mode 100644 GitTest/test.hcreate mode 100644 GitTest/test.m

这里我提交了两个文件,即:test.h和test.m

如果合并后产生冲突,可输入以下指令查看冲突:

$ git diff

修改之后,再次提交即可;

接下来,就可以push代码了:

$ git push -u origin master

这时可能需要你输入你的码云用户名和密码,按照提示输入即可;

删除分支

$ git branch -D newbranch

输出

Deleted branch newbranch (was 93a1347).

新手发文,大多数是搬运来的,按这个顺序能比较顺利得实现Git+码云的代码管理,如有侵权,请及时联系,会立即删除!


http://www.ppmy.cn/news/191677.html

相关文章

iFixit 拆解 2014 款 Mac mini拆机教程, 内存确认不能更换.

著名拆解网站 iFixit 已经完成对 2014 款 Mac mini 的拆解, 确认内存不能更换. 拆解的是标配款机型, 配置 1.4 GHz i5 处理器等. 机身基本和 2012 款相同, 去掉 FireWire 接口, 增加一个 Thunderbolt 接口. 依然是 A1347 型号, 但是改为 EMC 2840. 机身底部没有了以前的大拇指箭…

Rust 笔记:Rust 语言中的 结构体

Rust 笔记 Rust 语言中的 结构体 作者:李俊才 (jcLee95):https://blog.csdn.net/qq_28550263?spm1001.2101.3001.5343 邮箱 :291148484163.com 本文地址:https://blog.csdn.net/qq_28550263/article/detai…

【PHP】ThinkPhp6期末速通

目录 一、安装Composer二、设置Composer下载源三、Composer下载,安装TinkPHP6四、安装成功后 目录结构五、运行 ThinkPHP6 起步一、MVC二、单应用模式访问调试 三、安装视图四、模板渲染默认访问指定访问 五、模板变量默认赋值助手函数(若不使用默认赋值…

Redis的线程模型—文件事件处理器的详解

详细介绍了单线程Reactor模式的概念,以及Redis的线程模型—文件事件处理器的实现。 文章目录 1 Reactor模式2 文件事件处理器2.1 基本概念2.2 通信流程 Redis的线程模型是基于非常经典的单线程Reactor模式(netty架构也是基于Reactor模式)开发…

Windows调整处理器个数方法

前言: 今天,博主来教大家如何调整处理器个数。最好别闲着没事调它,建议就使用默认的1个处理器个数,一般不会卡,如果电脑存在运行吃力等情况可以调高成1~3之间,如果大于3个则会降低系统稳定性,和…

计算机处理器哪个最好,电脑处理器,哪个比较好

买电脑怎么看处理器?i5、i7差距大吗,高通、锐龙、英特尔处理器哪个好? 单从这个提问就可以看出题主是确实不懂处理器,因为这几个处理器品牌涉及到不同平台,他们是无法拿在一起来对比,英特尔和锐龙这个是PC处…

苹果a7处理器_我认为的最经典的苹果产品

我认为的最经典的苹果产品 1.iPhone 5s 作为最后的乔布斯式的设计,iPhone 5s绝对具有划时代的意义 最美iPhone之一,虽然我更喜欢iPhone 5,但不得不说,iPhone 5s的划时代意义比iPhone 5要更强一些,A7处理器,…

一文读懂处理器流水线

本文将讨论处理器的一个重要的基础知识:“流水线”。熟悉计算机体系结构的读者一定知道,言及处理器微架构,几乎必谈其流水线。处理器的流水线结构是处理器微架构最基本的一个要素,犹如汽车底盘对于汽车一般具有基石性的作用&#…