zoukankan      html  css  js  c++  java
  • Git基本操作指南

    简单总结版本在第四节基础命令入门教程中呈现

    零、Git命令大全网址

    Book

    下面总结一下如何使用github更新文件,温馨提示:由于windows系统中的git会包含诸多的bug,建议使用Linux系统进行git操作。

    一、安装git客户端

    在 Linux 上安装

    如果你想在 Linux 上用二进制安装程序来安装 Git,可以使用发行版包含的基础软件包管理工具来安装。 如果以 Fedora 上为例,你可以使用 yum:

    $ sudo yum install git

    如果你在基于 Debian 的发行版上,请尝试用 apt-get:

    $ sudo apt-get install git

    在 Mac 上安装

    在 Mac 上安装 Git 有多种方式。 最简单的方法是安装 Xcode Command Line Tools。 Mavericks (10.9) 或更高版本的系统中,在 Terminal 里尝试首次运行 git 命令即可。 如果没有安装过命令行开发者工具,将会提示你安装。

    在 Windows 上安装

    在 Windows 上安装 Git 也有几种安装方法。 官方版本可以在 Git 官方网站下载。 打开 http://git-scm.com/download/win,下载会自动开始。 要注意这是一个名为 Git for Windows 的项目(也叫做 msysGit),和 Git 是分别独立的项目;更多信息请访问 http://msysgit.github.io/

    另一个简单的方法是安装 GitHub for Windows。 该安装程序包含图形化和命令行版本的 Git。 它也能支持 Powershell,提供了稳定的凭证缓存和健全的换行设置。 稍后我们会对这方面有更多了解,现在只要一句话就够了,这些都是你所需要的。 你可以在 GitHub for Windows 网站下载,网址为 http://windows.github.com

    二、GitHub账户绑定并更新文件

    在本地新建一个非中文的文件名如:Local,并把GitHub相应的项目clone到Local

    windows系统通过cmd进入Local文件夹,并开始clone

    git clone URL //clone命令

    登陆git命令

    git config --global user.name "username"   
    git config --global user.email "useremail"

    三、 将本地文件与项目的缓冲区绑定并提交

    在clone后的文件夹中进行修改,更改后的文件加入GitHub缓冲区 

    git add * 

    将文件加入GitHub项目的缓冲区

    git commit -m "first" // 将文件加入GitHub项目的缓冲区,并将缓冲区命名为first
    

     将GitHub缓冲区的内容更新到GitHub主页面

    git push // 更新操作
    

     相应的有pull操作,可以把当前文件夹绑定分支(下文有操作)的文件直接copy到本地

    git pull // 下载项目最新文件

    四、基础命令入门教程

    在熟悉了上述几种操作之后,汇总一下git的基本操作命令

    Git 全局设置:

    git config --global user.name "USERNAME"
    git config --global user.email "USEREMAIL"

    Git 仓库创建:

    mkdir Project
    cd Project
    git init
    touch README.md
    git add README.md (仅对README文档进行提交或更改)
    git add . (对全部的文档进行提交或更改) git commit
    -m "first commit" git remote add origin https://github.com/USERNAME/Project.git (如果仓库已经存在,则不用此步操作) git push -u origin master

    Git 修改已经提交代码:

    Step1: git init 

    Step2: git pull <url> // 这里url是目标仓库的链接 

    Step3: 本地修改已经下载的文件 

    Step4: git add . 

    Step5: git commit -m "Update" 

    Step6: git push 

    五、分支操作

    git branch NewBranch // 新建一个分支
    
    git checkout NewBranch // 选择当前分支为NewBranch
    
    git branch //查看当前分支
    
    git remote add NewBranch URL // 将新分支与URL链接,此处URL指的是GitHub上的项目地址
    
    git  push --set-upstream NewBranch NewBranch // 本地更新到GitHub对应项目分支的缓冲区中
    
    git checkout master // 选择主分支

    注意,一定要将本地文件与GitHub仓库链接。

    git合并分支(一看就懂)

    常见Git操作的报错及处理:

    很重要:如果经历以下错误之一,搞定后应重新执行 step 3!

    当输入

    git push -u origin master
    

     报错

    origin does not to be a git repository
    

    解决办法:

    git remote add origin URL
    
    git push -u origin master 

    push远程仓库时,经常报出下面的错误,导致操作失败,让我们来看看怎么解决。

    To github.com:zwkkkk1/chatroom.git
    ! [rejected] master -> master (non-fast-forward)
    error: failed to push some refs to 'git@github.com:zwkkkk1/chatroom.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    错误:non-fast-forward

    远程仓库:origin
    远程分支:master
    本地分支:master

    解决方案
    Git 已经提示我们,先用 git pull 把最新的内容从远程分支(origin/master)拉下来,然后在本地 merge,解决 conflict,再 push。

    不过,在 git pull 时,还有其他的错误,我们分别看看可能出现的错误。

    fatal: refusing to merge unrelated histories

    此项错误是由于本地仓库和远程有不同的开始点,也就是两个仓库没有共同的 commit 出现的无法提交。这里我们需要用到 --allow-unrelated-histories。也就是我们的 pull 命令改为下面这样的:

    git pull origin master --allow-unrelated-histories
    

    如果设置了默认分支,可以这样写

    git pull --allow-unrelated-histories
    

     There is no tracking information for the current branch.

    完整报错代码可能是这样的:

    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details.
    
      git pull <remote> <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
      git branch --set-upstream-to=origin/<branch> master

    原因是没有指定本地 master 分支和远程 origin/master 的连接,这里根据提示:

    git branch --set-upstream-to=origin/master master

    报错

    fatal: The current branch master has no upstream branch.

    按照提示输入以下代码,可以解决

    git push --set-upstream origin master

    报错

    warning: adding embedded git repository 

    当前目录下面有.git文件夹------默认是隐藏的,直接将.git文件夹掉,再重新git add .

    则不再有报警提示,按正常的上传步骤上传代码即可

    参考:

    廖雪峰老师git教程

    git - 简易指南

    git 无法push远程仓库 Note about fast-forwards

    git合并分支(一看就懂)

    git初次登陆使用

     

    本人计算机小白一枚,对编程有浓厚兴趣,在此贴出自己的计算机学习历程,还有很多不足,望多多指教! 读书后发现好多的内容与具体专业有偏差,没来得及完成,虽然“有时间我就会做...”是人生最大的谎言,但有时间我会继续搞定未完成的内容,有始有终,兴趣使然!
  • 相关阅读:
    使用vue-lazyload 加载图片遇到的坑
    nvm 配置安装全局nodejs
    原生 ajax 请求
    angular5 引入第三方插件
    ionic3 组件引用报错问题
    有1到10w这个10w个数,去除2个并打乱次序,如何找出那两个数
    判断数据类型
    统计字符串有多少字节
    php
    数组 、 字符串 简单去重
  • 原文地址:https://www.cnblogs.com/Robin5/p/11892033.html
Copyright © 2011-2022 走看看