一、首先需要下载git

  查看电脑是否安装git,打开终端,输入git,回车如果输出如下,则代表已安装了git

如果未安装,则会输出:

按照提示输入:sudo apt-get install git即可安装!!或者到此处下载:git下载, pkg包下载完成,双击安装。

输入命令:git --version 可查看当前git版本

二.安装后需要一些配置

 配置用户名和邮箱:

$ git config --global user.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"

使用命令:git config --list 可查看当前用户信息以及其他的一些信息

$ git config --list
core.excludesfile=/Users/mac/.gitignore_global
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
http.postbuffer=
https.postbuffer=
user.email=你的邮箱@qq.com
user.name=你的用户名
macdeMacBook-Pro:~ Artron_LQQ$

三.建立本地git仓库

1. cd到你的项目目录

$ 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] 添加项目
files changed, insertions(+)
create mode GitTest.xcodeproj/project.pbxproj
create mode GitTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata
create mode GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
create mode GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/GitTest.xcscheme
create mode GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/xcschememanagement.plist
create mode GitTest/AppDelegate.h
create mode GitTest/AppDelegate.m
create mode GitTest/Assets.xcassets/AppIcon.appiconset/Contents.json
create mode GitTest/Base.lproj/LaunchScreen.storyboard
create mode GitTest/Base.lproj/Main.storyboard
create mode GitTest/Info.plist
create mode GitTest/ViewController.h
create mode GitTest/ViewController.m
create mode GitTest/main.m
create mode GitTestTests/GitTestTests.m
create mode GitTestTests/Info.plist
create mode GitTestUITests/GitTestUITests.m
create mode 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编辑器;

四,建立远程库

在一些代码托管平台创建项目,例如github或者开源中国社区,这里已开源中国社区为例;

创建项目后,会生成一个HTTPS链接,如下:

https://git.oschina.net/liuqiqiang/gitTest.git  

五,将本地的库链接到远

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

$ git remote add origin https://git.oschina.net/liuqiqiang/gitTest.git  

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

输出:

$ git pull origin master
warning: no common commits
remote: Counting objects: , done.
remote: Total (delta ), reused (delta )
Unpacking objects: % (/), 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 | +
file changed, insertion(+)
create mode README.md

即pull成功,

七.接着执行:git push origin master

完成后输出:

$ git push origin master
Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), 15.63 KiB | bytes/s, done.
Total (delta ), reused (delta )
To https://git.oschina.net/liuqiqiang/gitTest.git
5e2dda1..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 branch newbranch

查看分支

$ git branch

输出:

* master
newbranch

*代表当前所在的分支

切换分支

$ 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-forward
GitTest.xcodeproj/project.pbxproj | +++++++++
.../UserInterfaceState.xcuserstate | Bin -> bytes
GitTest/test.h | +++++++++++++
GitTest/test.m | +++++++++++++
files changed, insertions(+)
create mode GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
create mode GitTest/test.h
create mode GitTest/test.m

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

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

$ git diff

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

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

$ git push -u origin master

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

删除分支

$ git branch -D newbranch

输出

Deleted branch newbranch (was 93a1347).

以上就是最简单的github操作了,也是在网上看着学的,注意在实际操作中多加练习,代码这东西,刚开始桥的多了也就记下了!

利用git提交代码的更多相关文章

  1. Git提交代码到主分区

    git 提交代码,本地新建一个my分支,不从本地master分支直接上传,而是先从本地my分支上提交至本地master分支,然后本地master提交至远程master分支 上.前提是远程只有一个mas ...

  2. Git提交代码和更新代码命令

    微信公众号:非科班的科班关注可了解更多的java教程和其它资源视频.问题或建议,请公众号留言; 1.Git提交代码 利用命令提交代码的步骤:1.1.拉取服务器代码,避免覆盖他人的代码 git pull ...

  3. git提交代码到github

    前言:转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52117504 git提交代码到github 命令汇总: git init git ...

  4. 使用git提交代码到github,每次都要输入用户名和密码的解决方法

    自从使用git提交代码到github后,发现自己使用git的功力增长了不少,但也遇到不少问题.比如,使用git提交代码到github的时候,经常要求输入用户名和密码,类似这种: 网上有这么一种解决方法 ...

  5. Git提交代码失败: empty ident name (for <>) not allowed

    使用git提交代码,报错如下: 下午2:56 Commit failed with error 0 files committed, 1 file failed to commit: 升级 empty ...

  6. 在使用Git提交代码的时候犯了个低级错误

    今天在使用git提交代码的时候,犯了个很低级的错误,按照一切流程当我add并commit提交代码,最后使用push到远程仓库, 接下来奇怪的事情发生了,push之后,查看远程仓库代码并没有发现提交记录 ...

  7. Git提交代码报错Git push error:src refspec XXX matches more than one解决方案

    Git提交代码push时,报错这个 error: src refspec master matches more than one. error: failed to push some refs t ...

  8. git提交代码到码云

    日常代码一般提交到github比较多,但我还是钟爱马爸爸,没错就是码云. 码云是中文版的代码托管的网站,不存在打开网速问题,使用也蛮方便的,日常自己保存托管代码已经足够,平时使用git提交代码到码云是 ...

  9. git提交代码报错 trailing whitespace的解决方法

    1. git提交代码报错 trailing whitespace 禁止执行pre-commit脚本 进入到项目目录中 chmod a-x .git/hooks/pre-commit 2.git提交代码 ...

随机推荐

  1. 详解 CORS跨域的几种不同实现方式

    CORS 定义 CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing),它允许浏览器向跨源服务器,发出XMLHttpRequ ...

  2. 周末班:Python基础之模块

    什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  3. HTML---引入css,js | 常用标签示例

    一.前端基础包括哪些?如何理解 二.css,js引入_及head中其他标签 三.特殊符号 四.常见的标签 4.1,form表单 4.2,input系列(单选框.复选框.input传文件.重置) 4.3 ...

  4. Openssl x509命令

    一.简介 x509指令是一个功能很丰富的证书处理工具.可以用来显示证书的内容,转换其格式,给CSR签名等 二.语法 openssl x509 [-inform DER|PEM|NET] [-outfo ...

  5. 【转】Android 增,删,改,查 通讯录中的联系人

    一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...

  6. linux文件系統详解

    什么是文件系统 文件系统是操作系统用于明确磁盘或分区上的文件的方法和数据结构,即在存储设备(磁盘)上组织文件的方法.操作系统中负责管理和存储文件信息的软件结构称为文件管理系统,简称文件系统. 从系统角 ...

  7. UI Automator 常用 API 整理

    主要类: import android.support.test.uiautomator.UiDevice; 作用:设备封装类,测试过程中获取设备信息和设备交互. import android.sup ...

  8. JavaWeb工程中web.xml基本配置(转载学习)

    一.理论准备 先说下我记得xml规则,必须有且只有一个根节点,大小写敏感,标签不嵌套,必须配对. web.xml是不是必须的呢?不是的,只要你不用到里面的配置信息就好了,不过在大型web工程下使用该文 ...

  9. Codeforces Global Round 2 Solution

    这场题目设置有点问题啊,难度:Div.2 A->Div.2 B->Div.2 D->Div.2 C->Div.2 D->Div.1 D-> Div.1 E-> ...

  10. 基于 HTML5 WebGL 的 3D 棉花加工监控系统

    前言 现在的棉花加工行业还停留在传统的反应式维护模式当中,当棉花加下厂的设备突然出现故障时,控制程序需要更换.这种情况下,首先需要客户向设备生产厂家请求派出技术人员进行维护,然后生产厂家才能根据情况再 ...