[Git]使用Command Line(終端)提交代碼到遠(yuǎn)程庫

一. 下載安裝Git

查看電腦是否安裝git,打開終端,輸入git,回車如果輸出如下,則代表已安裝了git

$ 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$   

如果未安裝,則會(huì)輸出:

$ 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 可查看當(dāng)前git版本:

$ git --version  
git version 2.5.4 (Apple Git-61)  

當(dāng)然,網(wǎng)上也有一些安裝git的途徑.

二. 配置用戶

配置用戶名和郵箱:

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

使用 --global 修飾后設(shè)置的全局的用戶,如果設(shè)置單個(gè)項(xiàng)目的用戶,可cd到項(xiàng)目根目錄下,執(zhí)行如下命令:

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

使用命令:git config --list 可查看當(dāng)前用戶信息以及其他的一些信息

$ 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=你的用戶名     

三. 建立本地git倉庫

1. cd到你的項(xiàng)目目錄

$ cd /Users/mac/Desktop/GitTest  

PS: 鼠標(biāo)左鍵將文件夾拖拽到終端內(nèi),可自動(dòng)生成該文件夾路徑

2. 然后,輸入git命令:

$ git init 

輸出如下:

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

這樣就創(chuàng)建了一個(gè)空的本地倉庫.

然后,將需要托管的項(xiàng)目文件移動(dòng)到當(dāng)前文件夾內(nèi);

3. 將項(xiàng)目的所有文件添加到緩存中:

$ git add .

git add . (注意,后面有個(gè)點(diǎn))表示添加目錄下所有文件到緩存庫,如果只添加某個(gè)文件,只需把 . 換成你要添加的文件名即可;

$ git add People.h

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

git commit -m "添加你的注釋,一般是一些更改信息"

下面是第一次提交時(shí)的輸出:

$ git commit -m "添加項(xiàng)目"  
[master (root-commit) 3102a38] 添加項(xiàng)目  
 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 ,但是這樣會(huì)進(jìn)入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編輯器;

四. 建立遠(yuǎn)程庫

在一些代碼托管平臺(tái)創(chuàng)建項(xiàng)目,例如github或者開源中國社區(qū),這里以開源中國社區(qū)為例;
創(chuàng)建項(xiàng)目后,會(huì)生成一個(gè)HTTPS鏈接,如下:

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

五. 將本地的庫鏈接到遠(yuǎn)程庫

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

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

六.上傳代碼到遠(yuǎn)程庫

上傳之前最好先Pull一下,再執(zhí)行命令: 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成功;

七. 接著執(zhí)行: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  

即將代碼成功提交到遠(yuǎn)程庫!!!
如果在push的時(shí)候有如下輸出:

$ 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一下,即執(zhí)行一次:git pull origin master
然后再執(zhí)行:git push origin master

八.分支管理

這里只給出相關(guān)指令
新建分支

$ git branch newbranch

查看分支

$ git branch 

輸出:

* master  
  newbranch 

*代表當(dāng)前所在的分支

切換分支

$ git checkout newbranch  

輸出:

$ Switched to branch 'newbranch' 

切換后可用git branch查看是否切換到當(dāng)前分支

master  
* newbranch 

提交改動(dòng)到當(dāng)前分支

$ git add .  
$ git commit -a 

可使用git status查看提交狀態(tài)

接著切回主分支

$ git checkout master 

輸出:

$ Switched to branch 'master' 

將新分支提交的改動(dòng)合并到主分支上

$ 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  

這里我提交了兩個(gè)文件,即:test.h和test.m
如果合并后產(chǎn)生沖突,可輸入以下指令查看沖突:

$ git diff 

修改之后,再次提交即可;
接下來,就可以push代碼了:

$ git push -u origin master 

這時(shí)可能需要你輸入你的github用戶名和密碼,按照提示輸入即可;
刪除分支

$ git branch -D newbranch 

輸出

Deleted branch newbranch (was 93a1347). 

分支管理相關(guān)資料:點(diǎn)擊打開鏈接

(完)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容