使用Command Line(终端)提交代码到远程库

  1. $ git
  2. usage: git [--version] [--help] [-C <path>] [-c name=value]
  3. [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
  4. [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
  5. [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
  6. <command> [<args>]
  7. These are common Git commands used in various situations:
  8. start a working area (see also: git help tutorial)
  9. clone      Clone a repository into a new directory
  10. init       Create an empty Git repository or reinitialize an existing one
  11. work on the current change (see also: git help everyday)
  12. add        Add file contents to the index
  13. mv         Move or rename a file, a directory, or a symlink
  14. reset      Reset current HEAD to the specified state
  15. rm         Remove files from the working tree and from the index
  16. examine the history and state (see also: git help revisions)
  17. bisect     Find by binary search the change that introduced a bug
  18. grep       Print lines matching a pattern
  19. log        Show commit logs
  20. show       Show various types of objects
  21. status     Show the working tree status
  22. grow, mark and tweak your common history
  23. branch     List, create, or delete branches
  24. checkout   Switch branches or restore working tree files
  25. commit     Record changes to the repository
  26. diff       Show changes between commits, commit and working tree, etc
  27. merge      Join two or more development histories together
  28. rebase     Forward-port local commits to the updated upstream head
  29. tag        Create, list, delete or verify a tag object signed with GPG
  30. collaborate (see also: git help workflows)
  31. fetch      Download objects and refs from another repository
  32. pull       Fetch from and integrate with another repository or a local branch
  33. push       Update remote refs along with associated objects
  34. ‘git help -a‘ and ‘git help -g‘ list available subcommands and some
  35. concept guides. See ‘git help <command>‘ or ‘git help <concept>‘
  36. to read about a specific subcommand or concept.
  37. macdeMacBook-Pro:~ Artron_LQQ$

如果未安装,则会输出:

[ruby] view
plain
 copy

  1. $ git
  2. The program ‘git‘ is currently not installed. You can install it by typing:
  3. sudo apt-get install git

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

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

[objc] view
plain
 copy

  1. $ git --version
  2. git version 2.5.4 (Apple Git-61)
  3. macdeMacBook-Pro:~ Artron_LQQ$

当然,网上也有一些安装git的途径,可自行学习...

二.安装后需要一些配置

配置用户名和邮箱:

[objc] view
plain
 copy

  1. $ git config --global user.name "Your Name"
  2. $ git config --global user.email "[email protected]"


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

[objc] view
plain
 copy

  1. $ git config user.name "Your Name"
  2. $ git config user.email "[email protected]"

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

[objc] view
plain
 copy

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

三.建立本地git仓库

1. cd到你的项目目录

[ruby] view
plain
 copy

  1. $ cd /Users/mac/Desktop/GitTest

2. 然后,输入git命令:

[objc] view
plain
 copy

  1. $ git init

输出如下:

[objc] view
plain
 copy

  1. $ git init
  2. Initialized empty Git repository in /Users/mac/Desktop/GitTest/.git/

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

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

[objc] view
plain
 copy

  1. $ git add .

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

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

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

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

[objc] view
plain
 copy

  1. $ git commit -m "添加项目"
  2. [master (root-commit) 3102a38] 添加项目
  3. 18 files changed, 1085 insertions(+)
  4. create mode 100644 GitTest.xcodeproj/project.pbxproj
  5. create mode 100644 GitTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata
  6. create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
  7. create mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/GitTest.xcscheme
  8. create mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/xcschememanagement.plist
  9. create mode 100644 GitTest/AppDelegate.h
  10. create mode 100644 GitTest/AppDelegate.m
  11. create mode 100644 GitTest/Assets.xcassets/AppIcon.appiconset/Contents.json
  12. create mode 100644 GitTest/Base.lproj/LaunchScreen.storyboard
  13. create mode 100644 GitTest/Base.lproj/Main.storyboard
  14. create mode 100644 GitTest/Info.plist
  15. create mode 100644 GitTest/ViewController.h
  16. create mode 100644 GitTest/ViewController.m
  17. create mode 100644 GitTest/main.m
  18. create mode 100644 GitTestTests/GitTestTests.m
  19. create mode 100644 GitTestTests/Info.plist
  20. create mode 100644 GitTestUITests/GitTestUITests.m
  21. create mode 100644 GitTestUITests/Info.plist

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

[objc] view
plain
 copy

  1. # Please enter the commit message for your changes. Lines starting
  2. # with ‘#‘ will be ignored, and an empty message aborts the commit.
  3. # On branch master
  4. # Changes to be committed:
  5. #       modified:   LQQCircleShowImage.xcodeproj/project.pbxproj
  6. #       modified:   LQQCircleShowImage/TableViewCell.m
  7. #
  8. ~
  9. ~
  10. ~
  11. ~
  12. ~
  13. ~
  14. ~
  15. ~
  16. ~
  17. ~
  18. ~
  19. ~
  20. ~
  21. ~
  22. ~
  23. "~/Desktop/LQQCircleShowImage/.git/COMMIT_EDITMSG" 8L, 292C

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

四,建立远程库

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

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

[objc] view
plain
 copy

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

五,将本地的库链接到远程库

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

[objc] view
plain
 copy

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

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

输出:

[objc] view
plain
 copy

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

即pull成功,

七.接着执行:git push origin master

完成后输出:

[objc] view
plain
 copy

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

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

接着到你的远程库查看,提交前:

提交成功后:

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

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

[objc] view
plain
 copy

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

正确的应该是origin!!

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

[objc] view
plain
 copy

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

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

然后再执行:git push origin master

分支管理

新建分支

[objc] view
plain
 copy

  1. $ git branch newbranch

查看分支

[objc] view
plain
 copy

  1. $ git branch

输出:

[objc] view
plain
 copy

  1. * master
  2. newbranch

*代表当前所在的分支

切换分支

[objc] view
plain
 copy

  1. $ git checkout new branch

输出

[objc] view
plain
 copy

  1. Switched to branch ‘newbranch‘

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

[objc] view
plain
 copy

  1. master
  2. * newbranch

提交改动到当前分支

[objc] view
plain
 copy

  1. $ git add .
  2. $ git commit -a

可使用git status查看提交状态

接着切回主分支

[objc] view
plain
 copy

  1. $ git checkout master

输出:

[objc] view
plain
 copy

  1. Switched to branch ‘master‘

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

[objc] view
plain
 copy

  1. $ git merge newbranch

输出:

[objc] view
plain
 copy

  1. Updating cc73a48..93a1347
  2. Fast-forward
  3. GitTest.xcodeproj/project.pbxproj                        |   9 +++++++++
  4. .../UserInterfaceState.xcuserstate                       | Bin 0 -> 7518 bytes
  5. GitTest/test.h                                           |  13 +++++++++++++
  6. GitTest/test.m                                           |  13 +++++++++++++
  7. 4 files changed, 35 insertions(+)
  8. create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
  9. create mode 100644 GitTest/test.h
  10. create mode 100644 GitTest/test.m

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

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

[objc] view
plain
 copy

  1. $ git diff

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

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

[objc] view
plain
 copy

  1. $ git push -u origin master

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

删除分支

[objc] view
plain
 copy

  1. $ git branch -D newbranch

输出

[objc] view
plain
 copy

  1. Deleted branch newbranch (was 93a1347).

分支管理相关资料:http://gitbook.liuhui998.com/3_3.html

时间: 2024-07-29 17:19:05

使用Command Line(终端)提交代码到远程库的相关文章

git提交代码至远程仓库

代码提交 代码提交一般有五个步骤: 1.查看目前代码的修改状态 2.查看代码修改内容 3.暂存需要提交的文件 4.提交已暂存的文件 5.同步到服务器 1.     查看目前代码的修改状态 提交代码之前,首先应该检查目前所做的修改,运行git status命令 a)        已暂存 (changes to be committed) new file //表示新建文件 modified //表示修改文件 deleted //表示删除文件 b)       已修改 (changed but n

github本地提交代码到远程仓库

1.git工作状态: Workspace: 工作区  :等于平时放代码的地方 Index / Stage: 暂存区,临时存放你的改动,它只是一个文件,保存即将提交到文件列表信息 Repository: 仓库区(或版本库),安全存放数据的位置,这里面有你提交到所有版本的数据.其中HEAD指向最新放入仓库的版本 Remote: 远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑用于远程数据交换 2.git工作流程: 在工作目录中添加.修改文件: 将需要进行版本管理的文件放入暂存区域:

【转】Intellij IDEA 提交代码到远程GitHub仓库

1.文章参考自:http://my.oschina.net/lujianing/blog/180728 2.设置相关绑定 Settings——Version Control——Git——Path to Git executable——选择你的安装目录 Settings ——Version Control——GitHub这边Host:github.com Login:账号 Password:密码  3.项目的本地git提交 intellij内部集成了git版本控制 所以在本地可以直接进行使用 3.

Git创建远程分支并提交代码到远程分支

1.可以在VS中新建分支 2.可以通过git branch -r 命令查看远端库的分支情况 这些红色都是远程的分支 3.从已有的分支创建新的分支(如从master分支),创建一个dev分支 (不用vs建分支 可以这样建) 4.建立本地到远端仓库的链接 --这样代码才能提交上去 使用命令行 git push --set-upstream origin dev //dev为创建分支的名字 5.远程仓库已创建分支并提交代码 如图所示远程仓库已经创建了dev分支 下面也有代码上传上去了. 原文地址:ht

git第一次提交代码到远程仓库

本操作说明是先有代码,后来创建git仓库,然后把本地代码提交到远程仓库的操作步骤: 1.初始化 在当前你要提交的目录下执行 git init 2.创建远程仓库名称 git remote add gitOrigin https://github.com/***.git 3.告诉git你是谁 git config --global user.email "[email protected]" git config --global user.name "yourname"

Git提交代码到远程服务器

学习应该循序渐进,不应该是一蹴而就,也不要过分追求完美,是不断修复弥补自己的不足. 1.下载Git 不用说了,这个是必须的,也是最简单的步骤,地址如下: http://git-scm.com/download 这里会提供三个版本的下载地址,读者可以自行查找. 2.创建代码库 远程的代码库创建,我们可以直接去github上申请一个账号,然后在上面创建代码库,这里不细说了. 3.设置Git 在Git工作之前,为了能够跟踪到是谁对这段代码进行了修改,需要做一个配置信息,主要是用户名和邮箱,如下: gi

Git学习笔记(三)远程库(GitHub)协同开发,fork和忽略特殊文件

远程库 远程库,通俗的讲就是不再本地的git仓库!他的工作方式和我们本地的一样,但是要使用他就需要先建立连接! 远程库有两种,一个是自己搭建的git服务器:另一种就是使用GitHub,这个网站就是提供Git仓库托管服务的,所以,只要注册一个GitHub账号,就可以免费获得Git远程仓库.友情提示:在GitHub上免费托管的Git仓库,任何人都可以看到喔(但只有你自己才能改).所以,不要把敏感信息放进去. 远程仓库的好处: 1.我们可以随时随地的与仓库建立连接,以实时存放我们开发的内容: 2.与他

【转载】从0开始学习 GITHUB 系列之「向GITHUB 提交代码」

转载自http://stormzhang.com 之前的这篇文章「从0开始学习 GitHub 系列之「Git速成」」相信大家都已经对 Git 的基本操作熟悉了,但是这篇文章只介绍了对本地 Git 仓库的基本操作,今天我就来介绍下如何跟远程仓库一起协作,教你们向 GitHub 上提交你们的第一行代码! 1. SSH 你拥有了一个 GitHub 账号之后,就可以自由的 clone 或者下载其他项目,也可以创建自己的项目,但是你没法提交代码.仔细想想也知道,肯定不可能随意就能提交代码的,如果随意可以提

GitHub 系列之「向GitHub 提交代码」

1.SSH 你拥有了一个 GitHub 账号之后,就可以自由的 clone 或者下载其他项目,也可以创建自己的项目,但是你没法提交代码.仔细想想也知道,肯定不可能随意就能提交代码的,如果随意可以提交代码,那么 GitHub 上的项目岂不乱了套了,所以提交代码之前一定是需要某种授权的,而 GitHub 上一般都是基于 SSH 授权的. 那么什么是 SSH 呢? 简单点说,SSH是一种网络协议,用于计算机之间的加密登录.目前是每一台 Linux 电脑的标准配置.而大多数 Git 服务器都会选择使用