1. 程式人生 > >終端提交代碼到碼雲

終端提交代碼到碼雲

one readme detail signed edit branch ria pes ups

終端提交代碼到碼雲

一.下載安裝Git

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

  1. $ git
  2. usage: git [--version] [--help] [-C <path>] [-c name=value]
  3. [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
  4. [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
  5. [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
  6. <command> [<args>]
  7. These are common Git commands used in various situations:
  8. start a working area (see also: git help tutorial)
  9. clone Clone a repository into a new directory
  10. init Create an empty Git repository or reinitialize an existing one
  11. work on the current change (see also: git help everyday)
  12. add Add file contents to the index
  13. mv Move or rename a file, a directory, or a symlink
  14. reset Reset current HEAD to the specified state
  15. rm Remove files from the working tree and from the index
  16. examine the history and state (see also: git help revisions)
  17. bisect Find by binary search the change that introduced a bug
  18. grep Print lines matching a pattern
  19. log Show commit logs
  20. show Show various types of objects
  21. status Show the working tree status
  22. grow, mark and tweak your common history
  23. branch List, create, or delete branches
  24. checkout Switch branches or restore working tree files
  25. commit Record changes to the repository
  26. diff Show changes between commits, commit and working tree, etc
  27. merge Join two or more development histories together
  28. rebase Forward-port local commits to the updated upstream head
  29. tag Create, list, delete or verify a tag object signed with GPG
  30. collaborate (see also: git help workflows)
  31. fetch Download objects and refs from another repository
  32. pull Fetch from and integrate with another repository or a local branch
  33. push Update remote refs along with associated objects
  34. ‘git help -a‘ and ‘git help -g‘ list available subcommands and some
  35. concept guides. See ‘git help <command>‘ or ‘git help <concept>‘
  36. to read about a specific subcommand or concept.
  37. macdeMacBook-Pro:~ Artron_LQQ$
如果未安裝,則會輸出:


  1. $ git
  2. The program ‘git‘ is currently not installed. You can install it by typing:
  3. sudo apt-get install git
按照提示輸入:sudo apt-get install git即可安裝!!或者到此處下載:git下載, pkg包下載完成,雙擊安裝。

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


  1. $ git --version
  2. git version 2.5.4 (Apple Git-61)
  3. macdeMacBook-Pro:~ Artron_LQQ$

當然,網上也有一些安裝git的途徑,可自行學習...

二.安裝後需要一些配置

配置用戶名和郵箱:


  1. $ git config --global user.name "Your Name"
  2. $ git config --global user.email "[email protected]"



使用 --global 修飾後設置的全局的用戶,如果設置單個項目的用戶,可cd到項目根目錄下,執行如下命令:


  1. $ git config user.name "Your Name"
  2. $ git config user.email "[email protected]"


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


  1. $ git config --list
  2. core.excludesfile=/Users/mac/.gitignore_global
  3. difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
  4. difftool.sourcetree.path=
  5. mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
  6. mergetool.sourcetree.trustexitcode=true
  7. http.postbuffer=524288000
  8. https.postbuffer=524288000
  9. user.email=你的郵箱@qq.com
  10. user.name=你的用戶名
  11. macdeMacBook-Pro:~ Artron_LQQ$

三.建立本地git倉庫

1. cd到你的項目目錄


  1. $ cd /Users/mac/Desktop/GitTest


2. 然後,輸入git命令:

  1. $ git init

輸出如下:

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

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

3.將項目的所有文件添加到緩存中:


  1. $ git add .
git add . (註意,後面有個點)表示添加目錄下所有文件到緩存庫,如果只添加某個文件,只需把 . 換成你要添加的文件名即可;

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

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

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


  1. $ git commit -m "添加項目"
  2. [master (root-commit) 3102a38] 添加項目
  3. 18 files changed, 1085 insertions(+)
  4. create mode 100644 GitTest.xcodeproj/project.pbxproj
  5. create mode 100644 GitTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata
  6. create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
  7. create mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/GitTest.xcscheme
  8. create mode 100644 GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/xcschememanagement.plist
  9. create mode 100644 GitTest/AppDelegate.h
  10. create mode 100644 GitTest/AppDelegate.m
  11. create mode 100644 GitTest/Assets.xcassets/AppIcon.appiconset/Contents.json
  12. create mode 100644 GitTest/Base.lproj/LaunchScreen.storyboard
  13. create mode 100644 GitTest/Base.lproj/Main.storyboard
  14. create mode 100644 GitTest/Info.plist
  15. create mode 100644 GitTest/ViewController.h
  16. create mode 100644 GitTest/ViewController.m
  17. create mode 100644 GitTest/main.m
  18. create mode 100644 GitTestTests/GitTestTests.m
  19. create mode 100644 GitTestTests/Info.plist
  20. create mode 100644 GitTestUITests/GitTestUITests.m
  21. create mode 100644 GitTestUITests/Info.plist

或者不添加註釋 git commit ,但是這樣會進入vim(vi)編輯器


  1. # Please enter the commit message for your changes. Lines starting
  2. # with ‘#‘ will be ignored, and an empty message aborts the commit.
  3. # On branch master
  4. # Changes to be committed:
  5. # modified: LQQCircleShowImage.xcodeproj/project.pbxproj
  6. # modified: LQQCircleShowImage/TableViewCell.m
  7. #
  8. "~/Desktop/LQQCircleShowImage/.git/COMMIT_EDITMSG" 8L, 292C
在這裏可以輸入更改信息,也可以不輸入,然後 按住 shift + : ,輸入wq 即可保存信息並退出vim編輯器;

四,建立遠程庫

在一些代碼托管平臺創建項目,例如github或者開源中國社區,這裏已開源中國社區為例;

創建項目後,會生成一個HTTPS鏈接,如下:


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


五,將本地的庫鏈接到遠程庫

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


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


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

輸出:

  1. $ git pull origin master
  2. warning: no common commits
  3. remote: Counting objects: 3, done.
  4. remote: Total 3 (delta 0), reused 0 (delta 0)
  5. Unpacking objects: 100% (3/3), done.
  6. From https://git.oschina.net/liuqiqiang/gitTest
  7. * branch master -> FETCH_HEAD
  8. * [new branch] master -> origin/master
  9. Merge made by the ‘recursive‘ strategy.
  10. README.md | 1 +
  11. 1 file changed, 1 insertion(+)
  12. create mode 100644 README.md

即pull成功,

七.接著執行:git push origin master

完成後輸出:


  1. $ git push origin master
  2. Counting objects: 34, done.
  3. Delta compression using up to 4 threads.
  4. Compressing objects: 100% (29/29), done.
  5. Writing objects: 100% (34/34), 15.63 KiB | 0 bytes/s, done.
  6. Total 34 (delta 3), reused 0 (delta 0)
  7. To https://git.oschina.net/liuqiqiang/gitTest.git
  8. 5e2dda1..537ecfe master -> master

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

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

技術分享圖片

提交成功後:

技術分享圖片

註意:操作的時候,指令不要輸錯了!!!!

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


  1. git pull orgin master
  2. fatal: ‘orgin‘ does not appear to be a git repository
  3. fatal: Could not read from remote repository.
  4. Please make sure you have the correct access rights
  5. and the repository exists.

正確的應該是origin!!

如果在push的時候有如下輸出:


  1. $ git push -u origin master
  2. To https://git.oschina.net/liuqiqiang/LQQCircleShowImage.git
  3. ! [rejected] master -> master (fetch first)
  4. error: failed to push some refs to ‘https://git.oschina.net/liuqiqiang/LQQCircleShowImage.git‘
  5. hint: Updates were rejected because the remote contains work that you do
  6. hint: not have locally. This is usually caused by another repository pushing
  7. hint: to the same ref. You may want to first integrate the remote changes
  8. hint: (e.g., ‘git pull ...‘) before pushing again.
  9. hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.

看提示可知道,需要先pull一下,即執行一次:git pull origin master

然後再執行:git push origin master

分支管理

新建分支

  1. $ git branch newbranch

查看分支


  1. $ git branch

輸出:

  1. * master
  2. newbranch

*代表當前所在的分支

切換分支

  1. $ git checkout new branch
輸出

  1. Switched to branch ‘newbranch‘

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

  1. master
  2. * newbranch

提交改動到當前分支


  1. $ git add .
  2. $ git commit -a

可使用git status查看提交狀態

接著切回主分支

  1. $ git checkout master

輸出:

  1. Switched to branch ‘master‘

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

  1. $ git merge newbranch

輸出:

  1. Updating cc73a48..93a1347
  2. Fast-forward
  3. GitTest.xcodeproj/project.pbxproj | 9 +++++++++
  4. .../UserInterfaceState.xcuserstate | Bin 0 -> 7518 bytes
  5. GitTest/test.h | 13 +++++++++++++
  6. GitTest/test.m | 13 +++++++++++++
  7. 4 files changed, 35 insertions(+)
  8. create mode 100644 GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
  9. create mode 100644 GitTest/test.h
  10. create mode 100644 GitTest/test.m

這裏我提交了兩個文件,即:test.h和test.m

如果合並後產生沖突,可輸入以下指令查看沖突:

  1. $ git diff

修改之後,再次提交即可;

接下來,就可以push代碼了:

  1. $ git push -u origin master

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

刪除分支

  1. $ git branch -D newbranch

輸出

  1. Deleted branch newbranch (was 93a1347).

from:忘了 哈

終端提交代碼到碼雲