首页 > 程序开发 > 综合编程 > 其他综合 >

git的使用(上传项目到github)

2016-12-23

git的使用(上传项目到github):先在github网站上create 一个 repository,在GitBash上写指令第一次提交项目文件,进入当前项目的根路径 (也就是我要上传的项目文件所在路径)。

git的使用(上传项目到github):先在github网站上create 一个 repository,在GitBash上写指令第一次提交项目文件,进入当前项目的根路径 (也就是我要上传的项目文件所在路径)。

$ cd D:/android/application/AppStore

结果显示:
stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore

初始化仓库

用git init 指令初始化

$ git init

结果显示

Initialized empty Git repository in D:/android/application/AppStore/.git/
    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)

添加远程仓库

git@github.com:harrain/android-app-store.git是github网站上新建的仓库的ssh。 origin是起的别名,后面会用到。

$ git remote add origin git@github.com:harrain/android-app-store.git

可以用git remote -v 查看当前连接的是哪个远程库

$ git remote -v

结果显示:

origin  git@github.com:harrain/android-app-store.git (fetch)
    origin  git@github.com:harrain/android-app-store.git (push)

获取远程库分支最新情况

要上传的那一分支,获取其最新情况,使本地与远程同步(这里都是master分支)

$ git pull origin master

提示输入ssh密码

Enter passphrase for key '/c/Users/stephen/.ssh/id_rsa':

直接输入密码,回车。(Bash环境下,输入密码是不显示的。所以,输完回车就可以)

结果显示:pull回本地的过程信息
remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From github.com:harrain/android-app-store
     * branch            master     -> FETCH_HEAD
     * [new branch]      master     -> origin/master

向本地暂存区添加文件

添加所有文件,add . 就可以。后面是有点的哦

$ git add .

结果显示:
正常的编码替换

warning: LF will be replaced by CRLF in .idea/compiler.xml.
    The file will have its original line endings in your working directory.

提交信息,保存仓库的历史记录

$ git commit -m “Firlst commit”

结果显示:
[master 5e55359] Firlst commit
     196 files changed, 9773 insertions(+)
     create mode 100644 .gradle/2.14.1/taskArtifacts/cache.properties
     create mode 100644 .gradle/2.14.1/taskArtifacts/cache.properties.lock

可以用git status 查看仓库状态

$ git status

结果显示:

On branch master
nothing to commit, working tree clean

推送至远程仓库

$ git push origin master

提示输入密码
Enter passphrase for key '/c/Users/stephen/.ssh/id_rsa':
输入密码,回车后,开始显示上传过程信息
Counting objects: 250, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (223/223), done.
    Writing objects: 100% (250/250), 13.32 MiB | 18.00 KiB/s, done.
    Total 250 (delta 40), reused 0 (delta 0)
    remote: Resolving deltas: 100% (40/40), done.
    To github.com:harrain/android-app-store.git
       2e473d2..5e55359  master -> master

完毕

第二次提交

初始化

$ git init

结果显示:
Reinitialized existing Git repository in D:/android/application/AppStore/.git/

获取远程仓库分支最新情况

不需要再添加远程仓库了

$ git pull origin master

依然要输密码
Enter passphrase for key '/c/Users/stephen/.ssh/id_rsa':
输密码回车,结果显示
From github.com:harrain/android-app-store
     * branch            master     -> FETCH_HEAD
    Already up-to-date.

添加文件

$ git add README-ZN.md

提交信息

$ git commit -m “不完整版本的介绍”

显示结果
[master ca420d6] 不完整版本的介绍
     1 file changed, 13 insertions(+)
     create mode 100644 README-ZN.md

推送

$ git push origin master

输密码回车
Enter passphrase for key '/c/Users/stephen/.ssh/id_rsa':
显示
Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 827 bytes | 0 bytes/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
    To github.com:harrain/android-app-store.git
       5e55359..ca420d6  master -> master

完毕

下面说我前几次git上传出错的情况

其实就是我先往暂存区添加并提交了文件,再添加的远程仓库,然后pull获取最新。 这就是最大的错误了OMG

切记一定要先pull,使本地与远程仓库同步之后,再向本地暂存区add和commit,然后再push.

下面是我的错误提交指令记录
stephen@DESKTOP-CTGHSNT MINGW64 ~
    $ cd D:/android/application/AppStore

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore
    $ git init
    Initialized empty Git repository in D:/android/application/AppStore/.git/

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git add .
    warning: LF will be replaced by CRLF in .idea/compiler.xml.
    The file will have its original line endings in your working directory.

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git commit -m "根据不完整的资源写出的 待完善版本"
    [master (root-commit) 571e51d] 根据不完整的资源写出的待待完善版本
     196 files changed, 9773 insertions(+)
     create mode 100644 .gradle/2.14.1/taskArtifacts/cache.properties

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git remote add origin https://github.com/harrain/android-app-store.git

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git push -u origin master
    To https://github.com/harrain/android-app-store.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/harrain/android-app-store.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.

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git pull origin master
    warning: no common commits
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From https://github.com/harrain/android-app-store
     * branch            master     -> FETCH_HEAD
     * [new branch]      master     -> origin/master
    fatal: refusing to merge unrelated histories

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git push -u origin master
    To https://github.com/harrain/android-app-store.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'https://github.com/harrain/android-app-store.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.

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git push --set-upstream origin master
    To https://github.com/harrain/android-app-store.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'https://github.com/harrain/android-app-store.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.

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git pull origin master
    From https://github.com/harrain/android-app-store
     * branch            master     -> FETCH_HEAD
    fatal: refusing to merge unrelated histories

    stephen@DESKTOP-CTGHSNT MINGW64 /d/android/application/AppStore (master)
    $ git push origin master
    To https://github.com/harrain/android-app-store.git
     ! [rejected]        master -> master (non-fast-forward)
    error: failed to push some refs to 'https://github.com/harrain/android-app-store.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.
相关文章
最新文章
热点推荐