zoukankan      html  css  js  c++  java
  • Git 操作

    全部设置完毕,以后更新时候的操作:

    git status

    git add .

    git commit –m "new natter "  //对你更新或修改了哪些内容做一个描述。

     git remote -v  //查看你当前项目远程连接的是哪个仓库地址。

     git push -u origin master  //将本地的项目提交到远程仓库中。

    1,首先找一个代码托管网址,像gitcafe,注册一个用户名yourname。

    2,在gitcafe上新建一个空的项目,写好名称,比如Clang。

    3,在本地安装好git,linux上的话很简单,基本上默认安装了。win上面推荐用cygwin模拟linux环境,直接下载安装git软件包。(或者是用msysGit也行,我就是用这个的)。

    4,设置好本地的ssh密钥:

            $ cd ~/. ssh 检查本机的ssh密钥

            如果提示:No such file or directory 说明你是第一次使用git。

            如果不是第一次使用,请执行下面的操作,清理原有ssh密钥。

            $ mkdir key_backup

            $ cp id_rsa* key_backup    //备份其实(当时没看懂)

            $ rm id_rsa*

            生成新的密钥:

            ssh-keygen –t rsa –C “yourmaill@yourmaili.com”

            注意: 此处的邮箱地址,你可以输入自己的邮箱地址。在回车中会提示你输入一个密码,这个密码会在你提交项目时使用,如果为空的话提交项目时则不用输入。这个设置是防止别人往你的项目里提交内容。

            打开本地的.ssh/id_rsa.pub文件。此文件里面内容为刚才生成人密钥。

            登陆gitcafe系统。点击账户设置--->SSH公钥管理 ---> 添加新的公钥,把你本地生成的密钥复制到里面(key文本框中), 点击 保存 就ok了

            第一次在本地设置git时须:

            $ git config --global user.name "yourname"//gitcafe上的用户名

            $ git config --global user.email "yourmaill@yourmaili.com"//填写自己的邮箱

    5,选一个本地的项目存放位置,比如/home文件夹。

            $ git init     //就会将该文件夹变成一个git仓库(repository)

    6,在home下新建你的项目Clang文件夹,进入Clang文件夹,新建一个README.MD文件,(这新建过程可以用命令,也可以直接在windows下操作)然后使用如下命令:

            $ git status   //查看当前项目下所有文的状态,如果第一次,你会发现都红颜色的,因为它还没有交给git/gitcafe管理。

            $ git add .   //(.)点表示当前目录下的所有内容,交给git管理,也就是提交到了git的本地仓库。

            Ps:git的强大之处就是有一个本地仓库的概念,在没有网络的情况下可以先将更新的内容提交到本地仓库。

            $ git commit –m "new natter "  //对你更新或修改了哪些内容做一个描述。

            $ git remote add origin git@gitcafe.com:yourname/Clang.git

             //如果是gitlab就会是   git@gitlab.com:yourname/Clang.git

            //如果你是第一次提交项目,这一句非常重要,这是你本地的当前的项目与远程的哪个仓库建立连接。

            Ps: origin可以改为别人的名字,但是在你下一次push(提交)时,也要用你修改之后的名字。

            $ git remote -v  //查看你当前项目远程连接的是哪个仓库地址。

            $ git push -u origin master  //将本地的项目提交到远程仓库中。

    7,以上步骤就是你将新建的Clang项目推送到gitcafe托管的步骤,(对于gitlab也是一样的)。当你在别的主机上想要下载这个项目时,首先还是先设置好ssh公钥并在网站上添加,然后:

            $git clone git@gitcafe.com:yourname/Clang

            //项目编会克隆到你的当前主机上

    8,如果想删除项目中的一个文件,比如Clang文件夹下的a.c :

            $cd Clang/

            $git rm a.c

            $gir commit -m "delete a.c"

            $git push origin master

    9,如果你想使本地的Clang项目与网站上最新的项目同步:

            $ cd Clang/

            $ git fetch origin    //取得远程更新,这里可以看做是准备要取了

            $ git merge origin/master  //把更新的内容合并到本地分支/master

    --------------------------------------------------------------

    ! [rejected] master -> master (non-fast-forward)(有推荐视频)

    http://blog.csdn.net/lujinjian605894472/article/details/8443403

    分类: git & github

    当我们向github做push的时候经常会被rejected,解决方法有pull和rebase两种,这一集里我们讨论一下这两种方式的异同。

    推荐视频:http://happycasts.net/episodes/10?autoplay=true

    当要push代码到git时,出现提示:

    error:failed to push some refs to ...

    Dealing with “non-fast-forward” errors
    From time to time you may encounter this error while pushing:

    [plain] view plaincopy
     
    1. $ git push origin master
    2. To ../remote/
    3. ! [rejected] master -> master (non-fast forward)
    4. error: failed to push some refs to '../remote/'
    [plain] view plaincopy
     
    1. $ git push origin master  
    2. To ../remote/  
    3.  ! [rejected]        master -> master (non-fast forward)  
    4. error: failed to push some refs to '../remote/'  
    To prevent you from losing history, non-fast-forward updates were rejected
    Merge the remote changes before pushing again. See the 'non-fast forward'
    section of 'git push --help' for details.
    This error can be a bit overwhelming at first, do not fear. Simply put, git cannot make the change on the remote without losing commits, so it refuses the push. Usually this is caused by another user pushing to the same branch. You can remedy this by fetching and merging the remote branch, or using pull to perform both at once.
    In other cases this error is a result of destructive changes made locally by using commands like git commit --amend or git rebase. While you can override the remote by adding --force to the push command, you should only do so if you are absolutely certain this is what you want to do. Force-pushes can cause issues for other users that have fetched the remote branch, and is considered bad practice. When in doubt, don’t force-push.

    问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:

    1,强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

    git push -f

    2,先把git的东西fetch到你本地然后merge后再push

    $ git fetch

    $ git merge

    这2句命令等价于
    [plain] view plaincopy
     
    1. $ git pull
    [plain] view plaincopy
     
    1. $ git pull  

    可是,这时候又出现了如下的问题:

    上面出现的 [branch "master"]是需要明确(.git/config)如下的内容
    [branch "master"]
    remote = origin

    merge = refs/heads/master

    这等于告诉git2件事:

    1,当你处于master branch, 默认的remote就是origin。

    2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变

    如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:

    [plain] view plaincopy
     
    1. $ git config branch.master.remote origin
    2. $ git config branch.master.merge refs/heads/master
    [plain] view plaincopy
     
    1. $ git config branch.master.remote origin  
    2. $ git config branch.master.merge refs/heads/master  

    之后再重新git pull下。最后git push你的代码吧。it

  • 相关阅读:
    Shiro笔记(三)shiroFilter拦截器配置原则
    Shiro笔记(二)Shiro集成SpringMVC的环境配置
    Shiro笔记(一)Shiro整体介绍
    javaNIO的总结
    Redis的工作流程
    Nginx的配置安装和使用
    Linux下java开发环境配置总结
    php 基础知识 post 和get 两种传输方式的区别
    php 高级 多台web服务器共享session的方法
    php 基础知识 SESSION 和 COOKIE 的区别
  • 原文地址:https://www.cnblogs.com/codex/p/4567389.html
Copyright © 2011-2022 走看看