设为首页 收藏本站
查看: 1183|回复: 0

[经验分享] Git提交到码云(转)

[复制链接]

尚未签到

发表于 2018-1-13 08:02:14 | 显示全部楼层 |阅读模式
  一.下载安装Git
  查看电脑是否安装Git,打开终端,输入git,回车如果输出如下,则代表已安装了git

[ruby] view plain copy


  • $ git
  • usage: git [--version] [--help] [-C <path>] [-c name=value]
  •            [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
  •            [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
  •            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
  •            <command> [<args>]

  • These are common Git commands used in various situations:

  • start a working area (see also: git help tutorial)
  •    clone      Clone a repository into a new directory
  •    init       Create an empty Git repository or reinitialize an existing one

  • work on the current change (see also: git help everyday)
  •    add        Add file contents to the index
  •    mv         Move or rename a file, a directory, or a symlink
  •    reset      Reset current HEAD to the specified state
  •    rm         Remove files from the working tree and from the index

  • examine the history and state (see also: git help revisions)
  •    bisect     Find by binary search the change that introduced a bug
  •    grep       Print lines matching a pattern
  •    log        Show commit logs
  •    show       Show various types of objects
  •    status     Show the working tree status

  • grow, mark and tweak your common history
  •    branch     List, create, or delete branches
  •    checkout   Switch branches or restore working tree files
  •    commit     Record changes to the repository
  •    diff       Show changes between commits, commit and working tree, etc
  •    merge      Join two or more development histories together
  •    rebase     Forward-port local commits to the updated upstream head
  •    tag        Create, list, delete or verify a tag object signed with GPG

  • collaborate (see also: git help workflows)
  •    fetch      Download objects and refs from another repository
  •    pull       Fetch from and integrate with another repository or a local branch
  •    push       Update remote refs along with associated objects

  • 'git help -a' and 'git help -g' list available subcommands and some
  • concept guides. See 'git help <command>' or 'git help <concept>'
  • to read about a specific subcommand or concept.
  • macdeMacBook-Pro:~ Artron_LQQ$

如果未安装,则会输出:
[ruby] view plain copy


  • $ git
  • The program 'git' is currently not installed. You can install it by typing:
  • sudo apt-get install git

按照提示输入:sudo apt-get install git即可安装!!或者到此处下载:git下载, pkg包下载完成,双击安装。  输入命令:git --version 可查看当前git版本

[objc] view plain copy


  • $ git --version
  • git version 2.5.4 (Apple Git-61)
  • macdeMacBook-Pro:~ Artron_LQQ$
  当然,网上也有一些安装git的途径,可自行学习...

  二.安装后需要一些配置
  配置用户名和邮箱:

[objc] view plain copy

  • $ git config --global user.name "Your Name"
  • $ git config --global user.email "email@example.com"
  

  

  

  

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

[objc] view plain copy


  • $ git config user.name "Your Name"
  • $ git config user.email "email@example.com"
  

  

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

[objc] view plain copy


  • $ 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到你的项目目录

[ruby] view plain copy


  • $ cd /Users/mac/Desktop/GitTest
  

  
2. 然后,输入git命令:


[objc] view plain copy


  • $ git init
  输出如下:

[objc] view plain copy


  • $ git init
  • Initialized empty Git repository in /Users/mac/Desktop/GitTest/.git/
  创建了一个空的本地仓库.
  3.将项目的所有文件添加到缓存中:

[objc] view plain copy


  • $ git add .

git add . (注意,后面有个点)表示添加目录下所有文件到缓存库,如果只添加某个文件,只需把 . 换成你要添加的文件名即可;  4.将缓存中的文件Commit到git库
  git commit -m "添加你的注释,一般是一些更改信息"
  下面是第一次提交时的输出:

[objc] view plain copy


  • $ 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)编辑器

[objc] view plain copy


  • # 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链接,如下:

[objc] view plain copy


  • https://git.oschina.net/liuqiqiang/gitTest.git
  五,将本地的库链接到远程库

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

[objc] view plain copy

  • $ git remote add origin https://git.oschina.net/liuqiqiang/gitTest.git
  六.上传代码到远程库,上传之前最好先Pull一下,再执行命令: git pull origin master

  输出:

[objc] view plain copy

  • $ 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
  完成后输出:

[objc] view plain copy

  • $ 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
  即将代码成功提交到远程库!!!

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

  提交成功后:
DSC0001.jpg

  注意:操作的时候,指令不要输错了!!!!
  下面这个是输错了 orgin的输出:

[objc] view plain copy

  • 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的时候有如下输出:

[objc] view plain copy

  • $ 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
  分支管理
  新建分支

[objc] view plain copy

  • $ git branch newbranch
  查看分支


[objc] view plain copy

  • $ git branch
  输出:


[objc] view plain copy

  • * master
  •   newbranch
  *代表当前所在的分支
  切换分支

[objc] view plain copy

  • $ git checkout new branch
输出
[objc] view plain copy

  • Switched to branch 'newbranch'
  切换后可用git branch查看是否切换到当前分支


[objc] view plain copy

  • master
  • * newbranch
  

  提交改动到当前分支

[objc] view plain copy

  • $ git add .
  • $ git commit -a
  可使用git status查看提交状态

  接着切回主分支

[objc] view plain copy

  • $ git checkout master
  输出:


[objc] view plain copy

  • Switched to branch 'master'
  将新分支提交的改动合并到主分支上


[objc] view plain copy

  • $ git merge newbranch
  输出:


[objc] view plain copy

  • 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

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

[objc] view plain copy

  • $ git diff
  修改之后,再次提交即可;

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

[objc] view plain copy

  • $ git push -u origin master
  这时可能需要你输入你的github用户名和密码,按照提示输入即可;

  删除分支

[objc] view plain copy

  • $ git branch -D newbranch
  输出


[objc] view plain copy

  • Deleted branch newbranch (was 93a1347).
  分支管理相关资料:点击打开链接
  

  以上只是初级的最简单的指令操作,其他复杂的操作可自行摸索...

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434540-1-1.html 上篇帖子: Git 远程分支的pull与push 下篇帖子: git命令总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表