linux 下使用github


 Linux下Git和GitHub環境的搭建

1.創建Github帳號  (name@server.com)

2.安裝git

[root@cloud ~]# yum install git -y

3.生成ssh key,復制公鑰

[root@ray ansible]# ssh-keygen -t rsa -C 'name@server.com'

[root@ray .ssh]# cat ~/.ssh/id_rsa.pub

4.登陸github setting-->ssh and gpg keys-->new ssh key-->粘貼公鑰

5.測試ssh key是否部署成功

[root@ray ansible]# ssh -T git@github.com

6.配置git

[root@cloud ~]# git config --global core.editor vim
[root@ray ansible]# git config --global user.eamil 'name@server.com'  # 郵箱
[root@ray ansible]# git config --global user.name 'Raylively'         # 用戶名

# 查看配置
[root@cloud ~]# git config --list
[root@cloud ~]# cat ~/.gitconfig

 

工作區: 編寫程序的目錄

# 創建全新的工作目錄
[root@cloud ~]# git init py3

# 使用已經存在的目錄,在目錄下執行如下命令
[root@cloud py3]# git init .

# 查看版本庫.git
[root@cloud py3]#  ls -A  

# 查看狀態
[root@cloud py3]# git status

# 添加文件到暫存區
[root@cloud py3]# git add files   # 加入指定文件
[root@cloud py3]# git add .   # 加入全部文件

 # 刪除暫存區文件
 [root@cloud py3]# git rm --cached files

# 提交
[root@cloud py3]# git commit 
[root@cloud py3]# git commit -m 'second commit'

 # 查看提交歷史
[root@cloud py3]# git log

暫存區:保存工作區文件到版本庫之間的緩沖地帶

版本庫:保存每次提交的代碼。(工作區中的.git目錄就是版本庫)

 

分支管理

# 創建一個本地分支: git branch <新分支名字>
[root@cloud py3]# git branch b1

# 將本地分支同步到GitHub上面: git push <本地倉庫名> <新分支名>

#切換到新建立的分支: git checkout <新分支名>
[root@cloud py3]# git checkout b1


# 合並分支到master
[root@cloud py3]# git checkout master
[root@cloud py3]# git merge b1



#為你的分支加入一個新的遠程端: git remote add <遠程端名字> <地址>

#查看當前倉庫有幾個分支: git branch
[root@cloud py3]# git branch


#從本地刪除一個分支: git branch -d <分支名稱>
[root@cloud py3]# git branch -d b1


#同步到GitHub上面刪除這個分支: git push <本地倉庫名> :<GitHub端分支>

 

切換到某一版本(如果需要修改,創建一個分支)

[root@cloud py3]# git log
[root@cloud py3]# git checkout <版本序列>

 

 

2 利用Git從本地上傳到GitHub

  1. 進入要所要上傳文件的目錄, 輸入命令 git init
  2. 創建一個本地倉庫origin,使用命令 git remote add origin git@github.com:your_name/yourRepo.git,your_name是你的GitHub的用戶名,yourRepo是你要上傳到GitHub的倉庫
  3. 比如你要添加一個文件xxx到本地倉庫,使用命令 git add xxx,可以使用 git add . 自動判斷添加哪些文件
  4. 然后把這個添加提交到本地的倉庫,使用命令 git commit -m "說明這次的提交"
  5. 最后把本地倉庫origin提交到遠程的GitHub倉庫,使用命令 git push origin master

3 從GitHub克隆項目到本地

  1. 到GitHub的某個倉庫,然后復制右邊的那個(HTTPS clone url)
  2. 回到要存放的目錄下,使用命令 git clone https://github.com/your_name/yourRepo.git,your_name是你的GitHub的用戶名,yourRepo是你要clone的倉庫
  3. 如果本地的版本不是最新的,可以使用命令 git fetch origin,origin是本地倉庫
  4. 把更新的內容合並到本地分支,可以使用命令 git merge origin/master
  5. 如果你不想手動去合並,那么你可以使用: git pull <本地倉庫> master 這個命令來拉去最新版本並自動合並

4 GitHub的分支管理

創建分支

  1. 創建一個本地分支: git branch <新分支名字>
  2. 將本地分支同步到GitHub上面: git push <本地倉庫名> <新分支名>
  3. 切換到新建立的分支: git checkout <新分支名>
  4. 為你的分支加入一個新的遠程端: git remote add <遠程端名字> <地址>
  5. 查看當前倉庫有幾個分支: git branch

刪除分支

  1. 從本地刪除一個分支: git branch -d <分支名稱>
  2. 同步到GitHub上面刪除這個分支: git push <本地倉庫名> :<GitHub端分支>

5 常見錯誤

如果出現報錯為ERROR: Repository not found.fatal: The remote end hung up unexpectedly則代表你的 origin 的url 鏈接有誤,可能是創建錯誤,也可能是這個 git@github.com:xxx/new-project.git url 指定不正確。重新創建。

 

 

命令行下:
echo "# Blog" >> README.md

git init git add README.md

git commit -m "first commit"

git remote add origin https://github.com/Raylively/Blog.git

git push -u origin master

 

修改/etc/ssh/ssh_config文件的配置,以后則不會再出現此問題

最后面添加:

StrictHostKeyChecking no

UserKnownHostsFile /dev/null

 

# 克隆版本庫
git clone git@github.com:Raylively/q_a.git

 

# 上傳文件

git add filename

git commit -m "add filename"
git push origin master

 

首先進入你的master文件夾下, Git Bash Here ,打開命令窗口

$ git --help                                      # 幫助命令

 

$ git pull origin master                    # 將遠程倉庫里面的項目拉下來

$ dir                                                # 查看有哪些文件夾

$ git rm -r --cached target              # 刪除target文件夾
$ git commit -m '刪除了target'        # 提交,添加操作說明

 

 

fatal: 遠程 origin 已經存在。

此時只需要將遠程配置刪除,重新添加即可;

git remote rm origin

git remote add origin https://github.com/***/WebCrawlers.git

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM