利用git提交代碼

 

1、首先須要下載gitgit

  查看電腦是否安裝git,打開終端,輸入git,回車若是輸出以下,則表明已安裝了gitgithub

若是未安裝,則會輸出:json

 

 

按照提示輸入:sudo apt-get install git便可安裝!!或者到此處下載:git下載, pkg包下載完成,雙擊安裝。vim

 

輸入命令:git --version 可查看當前git版本xcode

 

二.安裝後須要一些配置緩存

 配置用戶名和郵箱:app

 

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

使用 --global 修飾後設置的全局的用戶,若是設置單個項目的用戶,可cd到項目根目錄下,執行以下命令:編輯器

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

使用命令:git config --list 可查看當前用戶信息以及其餘的一些信息post

$ 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倉庫fetch

1. cd到你的項目目錄

$ cd /Users/cjk/Desktop/myShop

2. 而後,輸入git命令:

$ git init  

輸出以下:

$ git init  
Initialized empty Git repository in /Users/cjk/Desktop/GitTest/.git/  

建立了一個空的本地倉庫.

3.將項目的全部文件添加到緩存中:

$ git add .  

git add . (注意,後面有個點)表示添加目錄下全部文件到緩存庫,若是隻添加某個文件,只需把 . 換成你要添加的文件名便可;

4.將緩存中的文件Commit到git庫

git commit -m "添加你的註釋,通常是一些更改信息"

下面是第一次提交時的輸出:

$ 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)編輯器

# 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連接,以下:

 

 

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

五,將本地的庫連接到遠

終端中輸入: git remote add origin HTTPS連接

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

六.上傳代碼到遠程庫,上傳以前最好先Pull一下,再執行命令: git pull origin master

輸出:

$ 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

完成後輸出:

$ 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

  即將代碼成功提交到遠程庫!!!

注:若是pull以後出現 「 refusing to merge unrelated histories 」這句,就證實你合併pull兩個不一樣的項目

出現的問題如何去解決fatal: refusing to merge unrelated histories

我在Github新建一個倉庫,寫了License,而後把本地一個寫了好久倉庫上傳。

先pull,由於兩個倉庫不一樣,發現refusing to merge unrelated histories,沒法pull

由於他們是兩個不一樣的項目,要把兩個不一樣的項目合併,git須要添加一句代碼,在git pull,這句代碼是在git 2.9.2版本發生的,最新的版本須要添加--allow-unrelated-histories

假如咱們的源是origin,分支是master,那麼咱們 須要這樣寫git pull origin master --allow-unrelated-histories須要知道,咱們的源能夠是本地的路徑

接着到你的遠程庫查看,提交前:

提交成功後:

注意:操做的時候,指令不要輸錯了!!!!

下面這個是輸錯了 orgin的輸出:

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的時候有以下輸出:

$ 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

分支管理

新建分支

$ git branch newbranch

查看分支

$ git branch

輸出:

* master
  newbranch

*表明當前所在的分支

切換分支

$ git checkout new branch

輸出

Switched to branch 'newbranch'

切換後可用git branch查看是否切換到當前分支

master
* newbranch

提交改動到當前分支

$ git add .
$ git commit -a

可以使用git status查看提交狀態

接着切回主分支

$ git checkout master

輸出:

Switched to branch 'master'

將新分支提交的改動合併到主分支上

$ git merge newbranch

輸出:

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

若是合併後產生衝突,可輸入如下指令查看衝突:

$ git diff

修改以後,再次提交便可;

接下來,就能夠push代碼了:

$ git push -u origin master

這時可能須要你輸入你的github用戶名和密碼,按照提示輸入便可;

刪除分支

$ git branch -D newbranch

輸出

Deleted branch newbranch (was 93a1347).

 

以上就是最簡單的github操做了,也是在網上看着學的,注意在實際操做中多加練習,代碼這東西,剛開始橋的多了也就記下了!

相關文章
相關標籤/搜索