不卡AV在线|网页在线观看无码高清|亚洲国产亚洲国产|国产伦精品一区二区三区免费视频

學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux下git的安裝和使用

linux下git的安裝和使用

時間: 志藝942 分享

linux下git的安裝和使用

  在linux下僅使用了文本界面,所以安裝了個文本形式的git客戶來連接github。接下來是小編為大家收集的linux下git的安裝和使用,希望能幫到大家。

  linux下git的安裝和使用

  1. 安裝git

  我用的是centos系統(tǒng),在使用yum install git時,系統(tǒng)提示沒有找到git包。所以,僅能通過以下方法來安裝git。方法詳見:http://www.cnblogs.com/Neddy/archive/2011/02/28/1967548.html。以上方法中有一個問題:方法中給出的git的下載源http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz 似乎無效了,于是,我在網(wǎng)上的這里下載了個git的最新安裝包,安裝到了centos上。Linux下git的官方網(wǎng)址為:http://git-scm.com/download ,可能因為我網(wǎng)慢打不開,不知道讀者您那里如何。如果打不開,可以在網(wǎng)上其他地方找找安裝包,應(yīng)該可以找到的。

  2. 使用git連接github

  使用git連接github時,需要將linux下產(chǎn)生的一個ssh公鑰放到github上。具體步驟詳見:http://blog.sina.com.cn/s/blog_6b706e15010199p1.html。主要命令有:

1
ssh-keygen -t rsa -C"mail@mail.com"

  然后系統(tǒng)提示輸入文件保存位置等信息,連續(xù)敲三次回車即可,生成的SSH key文件保存在中~/.ssh/id_rsa.pub文件中。

  用文本編輯工具打開該文件,在linux下可以用cat命令顯示id_rsa.pub中的內(nèi)容(cat ~/.ssh/id_rsa.pub),讓后復(fù)制其內(nèi)容。

  接著拷貝.ssh/id_rsa.pub文件內(nèi)的所以內(nèi)容,將它粘帖到github帳號管理中的添加SSH key界面中。

  注意,使用vim讀取git_home/.ssh/id_rsa.pub中的公鑰內(nèi)容時,可能會有較多的空格和換行,復(fù)制到github網(wǎng)站上時必需刪除。所以建議使用cat來讀取ssh公鑰。將ssh公鑰成功加入github后,可使用命令ssh -T git@github.com來驗證是否成功。如果出現(xiàn)象:hi xxx. You've successfully authenticated, but GitHub does not provide shell access.則說明連接成功。

  非常不幸,我未能連接成功??墒褂妹顂sh -Tv git@github.com來查找failure的原因。通過詳細的debug過程,我發(fā)現(xiàn)象我把自己的ssh密鑰信息放到了/home/admin/.ssh/下,而測試時使用的賬戶是root,尋找ssh密鑰的路徑為root/.ssh,所以permission denied的啦。su到admin下,就可以連接成功啦~~

  3. 使用git與github管理代碼

  3.1 新建一個repository

  這里就使用github官網(wǎng)上的教程吧。請保證git的版本至少為1.7.10,否則可能無法成功。詳細如何使用,請參見:https://help.github.com/articles/set-up-git。linux下無法新建一個repo,只能對github中已有的repo進行修改。所以,當(dāng)要新建一個repo時,必須在github.com上新建,再通過linux下的git向此repo中新添內(nèi)容。

  3.2 修改repo中的代碼

  github的官網(wǎng)上也有修改repo代碼的教程。詳情請參見:https://help.github.com/articles/fork-a-repo。簡要步驟如下:

$git clone https://github.com/username/Spoon-Knife.git
$cd Spoon-Knife
$git add filename.py                          #添加文件到版本庫
$git commit -m 'add filename.py to src'               #提交,產(chǎn)生版本記錄,注意代碼依然在本地
$vim README.md                             #修改Spoon-Knife中的README.md文件內(nèi)容
$git commit -m 'modify the README.md'                #提交,產(chǎn)生版本記錄,注意代碼依然在本地
$git [remote] rm filename1.py                    #刪除repo中的filename1.py文件
$git commit -m 'delete filename1.py'                  #提交,產(chǎn)生版本記錄,注意代碼依然在本地
$git push origin                             #將修改提交到github上<br>

  3.3 常用git命令

linux下git的安裝和使用

  git help                                 #可查看git的常用命令

  git config --global user.name "Your Name Here"           #設(shè)置commit的署名

  git config --global user.email "your_email@example.com"      #設(shè)置commit的email

  git config [--local|--global|--system] --list/-l          #查看本地的global信息

  git config [--local|--global|--system] --unset[-all] user.name  #刪除user.name信息。如果user.name對應(yīng)多個值,可用unset-all來刪除

  git remote add XXX https://github.com/username/repo_name.git    #設(shè)置github的連接git clone git://github.com/your_account/aimed_repo.git       #復(fù)制一個repo到本地 git remote -v                               #查看本地設(shè)置的url連接信息 git status                                 #查看當(dāng)前工作的branch git branch                             #查看本地所有的branch git branch -a                           #查看遠程的所有分支 git branch -d branch_name                        #刪除本地branch_name這一分支 git push origin --delete branch_name                   #刪除名為branch_name的遠程分支 git checkout branch_name                         #切換到名為branch_name的分支上 git chechout -b branch_name                       #在本地新建一個名為branch_nam的分支 git diff test_branch_name                        #查看當(dāng)前branch與test_branch_name中代碼的區(qū)別 git mv filename newfilename                      #文件重命名 git push XXX branch_name                        #上傳指定的branch到遠端 git pull                                  #將遠程上的版本與本地版本進行合并,相當(dāng)于get fetch + git merge git reset --hard                             #將剛才進行的git pull所進行的操作取消,恢復(fù)本地版本合并前的原貌

linux下git的安裝和使用

  4. 如何刪除github上的repository

  github頁面上刪除repo的功能比較隱蔽,得在這里表一表。比如,想刪除了一個名為python的repo。則需先點擊進入“python”,單擊“Settings”,找到“Delete this repository”,確認刪除即可。注意,github上的repo刪除后就不能恢復(fù)了哦~~

linux下git的安裝和使用
linux下git的安裝和使用

  5. git clone/push時出現(xiàn)錯誤提示:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed while accessing ...

  這是由于ssl認證出問題引起的錯誤。有兩種簡單的解決方法:

  1. 使用命令,成功執(zhí)行后,便可正常使用git clone和git push了

git config --global http.sslVerify false

  2. 使用命令,但每次clone 和 push時都需要帶上env的部分。

env GIT_SSL_NO_VERIFY=true git clone https://github.com/XXXX/xxxxx.git

  6. git push時出現(xiàn)錯誤non-fast-forward時怎么辦?(來自:http://blog.csdn.net/chain2012/article/details/7476493)

  當(dāng)要push代碼到git時,出現(xiàn)提示:

  error:failed to push some refs to ...

  Dealing with “non-fast-forward” errors

  From time to time you may encounter this error while pushing:

  $ git push origin master

  To ../remote/

  ! [rejected] master -> master (non-fast forward)

  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.

linux下git的安裝和使用

  問題(Non-fast-forward)的出現(xiàn)原因在于:git倉庫中已經(jīng)有一部分代碼,所以它不允許你直接把你的代碼覆蓋上去。于是你有2個選擇方式:

  1,強推,即利用強覆蓋方式用你本地的代碼替代git倉庫內(nèi)的內(nèi)容

  git push -f

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

  $ git fetch

  $ git merge

  這2句命令等價于

  $ git pull

  可是,這時候又出現(xiàn)了如下的問題:

linux下git的安裝和使用

  上面出現(xiàn)的 [branch "master"]是需要明確(.git/config)如下的內(nèi)容

  [branch "master"]

  remote = origin

  merge = refs/heads/master

  這等于告訴git2件事:

  1,當(dāng)你處于master branch, 默認的remote就是origin。

  2,當(dāng)你在master branch上使用git pull時,沒有指定remote和branch,那么git就會采用默認的remote(也就是origin)來merge在master branch上所有的改變

  如果不想或者不會編輯config文件的話,可以在bush上輸入如下命令行:

  $ git config branch.master.remote origin

  $ git config branch.master.merge refs/heads/master

  之后再重新git pull下。最后git push你的代碼吧。it works now~


看了“linux下git的安裝和使用”還想看:

1.Linux系統(tǒng)上怎樣安裝Git

2.CentOS中Git客戶端怎么安裝

3.怎么在Linux下安裝配置Git服務(wù)

4.CentOS系統(tǒng)怎樣搭建Git版本控制服務(wù)器

2805898