git常用命令总结:https://www.cnblogs.com/jackchensir/p/8306448.html

利用git提交代码

一、首先需要下载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=524288000
https.postbuffer=524288000
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] 添加项目
18 files changed, 1085 insertions(+)
create mode 100644 GitTest.xcodeproj/project.pbxproj
create mode 100644 GitTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata
create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
create mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/GitTest.xcscheme
create mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/xcschememanagement.plist
create mode 100644 GitTest/AppDelegate.h
create mode 100644 GitTest/AppDelegate.m
create mode 100644 GitTest/Assets.xcassets/AppIcon.appiconset/Contents.json
create mode 100644 GitTest/Base.lproj/LaunchScreen.storyboard
create mode 100644 GitTest/Base.lproj/Main.storyboard
create mode 100644 GitTest/Info.plist
create mode 100644 GitTest/ViewController.h
create mode 100644 GitTest/ViewController.m
create mode 100644 GitTest/main.m
create mode 100644 GitTestTests/GitTestTests.m
create mode 100644 GitTestTests/Info.plist
create mode 100644 GitTestUITests/GitTestUITests.m
create 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编辑器;

四,建立远程库

在一些代码托管平台创建项目,例如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: 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成功,

七.接着执行: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.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 | 9 +++++++++
.../UserInterfaceState.xcuserstate | Bin 0 -> 7518 bytes
GitTest/test.h | 13 +++++++++++++
GitTest/test.m | 13 +++++++++++++
4 files changed, 35 insertions(+)
create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
create mode 100644 GitTest/test.h
create mode 100644 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常用命令总结 git常用命令总结的更多相关文章

  1. git常用命令,git版本控制和Xcode结合使用,用Xcode提交到github,github客户端使用

    1.git常用命令 查看命令: 1.git --help 查看git所有命令 2.git clone -help 查看git clone命令的细节 3.git config -l   查看当前所有配置 ...

  2. 开发过程中常用到的git命令

    将git上项目下载到本地 1.将项目下载到本地 git clone (git项目地址) 2.进入项目文件夹中(cd 某文件夹) 切换到要使用的分支 git checkout develop 3.抓取远 ...

  3. sublime 集成git插件,及git常用命令

    以前用了很久的totoiseSVN,显然不够高大上,开发界的版本装x利器还得是git.然而git的命令行对没怎么用过linux的人可能觉得还不如用gitGUI,可是当你知道sublime可以集成git ...

  4. Git知识总览(二) git常用命令概览

    上篇博客我们从 git clone 和 git status 两个命令开始,引出了一系列的git操作命令, 请参见:<Git知识总览(一) 从 git clone 和 git status 谈起 ...

  5. git提交项目常用命令及git分支的用法

    1.第一步首先从git托管平台clone项目,我这里就使用idea为例: 填写git的url与存放本地目录名及项目名     2.如果你对项目进行了一些修改,就可以执行git命令,进行提交. 有两种方 ...

  6. 开发环境之git:团队协作git工作流与常用命令

    此篇文章只是一篇傻瓜式的,记录工作中比较规范且常见的一个git工作流需要用到的命令,让你可以快速的开始工作.而不是一些长篇大论的理论知识,如果你有用过sourcetree或者其它图形化工具,结合你正在 ...

  7. 【github&&git】4、git常用命令(持续更新中)

    git常用命令(持续更新中) 本地仓库操作git int                                 初始化本地仓库git add .                       ...

  8. git(8):常用命令

    Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v ...

  9. Git的原理简介和常用命令

    Git和SVN是我们最常用的版本控制系(Version Control System, VCS),当然,除了这二者之外还有许多其他的VCS,例如早期的CVS等.顾名思义,版本控制系统主要就是控制.协调 ...

  10. Git-学习笔记(常用命令集合)

    这里引用一下百度百科里Git的内容: Git --- The stupid content tracker, 傻瓜内容跟踪器.Linus Torvalds 是这样给我们介绍 Git 的. Git 是用 ...

随机推荐

  1. Latex--入门系列一

    Latex 专业的参考 tex对于论文写作或者其他的一些需要排版的写作来说,还是非常有意义的.我在网上看到这个对于Latex的入门介绍还是比较全面的,Arbitrary reference .所以将会 ...

  2. 跨域问题解决----NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost:11000' is therfore not allowed access'

    NO 'Access-Control-Allow-Origin' header is present on the requested resource.Origin'http://localhost ...

  3. smarty之缓存机制

    当smarty开启缓存时,当tpl文件内容改变时,则缓存页面会重新生成 test.php: <?php date_default_timezone_set('asia/shanghai'); h ...

  4. CentOS 系统开启防火墙,屏蔽IP,解决DDOS攻击

    刚才发现网站特别慢,然后看了一下服务器状态 CPU 负载100%. 然后看了下网络,发现一个IP一直在请求本服务器的 443 端口,就是本站. 然后在终端通过 iftop 命令(一个流量健康软件,如果 ...

  5. JSP学习(5)

    JSP学习(5) 保存用户状态的两大机制 session对象 Cookie Cookie简介 是Web服务器保存在客户端的一系列文本信息 典型应用 判断注册用户是否已经登录 购物车处理 作用 对特定对 ...

  6. JS window对象 返回前一个浏览的页面 back()方法

    JS window对象 返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL. 语法: window.history.back();   返回前一个浏览的页面 back ...

  7. $_POST 和 php://input 的区别

    手册中摘取的几句话: 当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 时, ...

  8. Robomongo可视化命令

    ## 更新命令示例 db.getCollection('collection_name').update(    {'_id':ObjectId('id string')},     {$set:{' ...

  9. 类数组对象与 arguments

    类数组对象:拥有一个 length 属性和若干索引属性的对象 var array = ['name', 'age', 'sex']; var arrayLike = { 0: 'name', 1: ' ...

  10. URL跳转漏洞

    URL跳转原理: 由于越来越多的需要和其他第三方应用交互,以及在自身应用内部根据不同的逻辑将用户引向到不同的页面,譬如一个典型的登录接口就经常需要在认证成功之后将用户引导到登录之前的页面,整个过程中如 ...