[Github]Github命令行快速使用教程及常見問題解決
Git環境配置(Linux)
在Windows下的配置其實可以下載GithubDesktop或者直接用IDE自帶的版本控制,基本不需要命令行,純傻瓜操作,所以這裡只寫Linux的。
第一步:
生成ssh key,使用命令 「ssh-keygen -t rsa -C "youremail@youremail.com"」,youremail是你的email默認在用戶的家目錄下.ssh/id_rsa.pub文件裡面
第二步:
回到github,進入Account Settings,左邊選擇SSH Keys,Add SSH Key,title隨便填,粘貼key。
第三步:
測試ssh key是否成功,使用命令「ssh -T git@github.com」,如果出現You』ve successfully authenticated, but GitHub does not provide shell access 。這就表示已成功連上github。
如果出現「Agent admitted failure to sign using the key.Permission denied (publickey).」這個錯誤的話,在命令行下執行"ssh-add",這樣就可以了。
第四步:
配置Git的配置文件,username和email
git config --global user.name "your name" //配置用戶名git config --global user.email "your email" //配置email
clone至本地、push到Github
clone
git clone https://github.com/chenguolin/scrapy.gitgit pull origin master
push
git add xxxgit commit -mgit push origin master
代碼衝突常見解決方法
如果系統中有一些配置文件在伺服器上做了配置修改,然後後續開發又新添加一些配置項的時候,
在發布這個配置文件的時候,會發生代碼衝突:
error: Your local changes to the following files would be overwritten by merge:
protected/config/main.phpPlease, commit your changes or stash them before you can merge.如果希望保留生產伺服器上所做的改動,僅僅併入新配置項, 處理方法如下:
git stash
git pullgit stash pop然後可以使用Git diff -w +文件名 來確認代碼自動合併的情況.反過來,如果希望用代碼庫中的文件完全覆蓋本地工作版本. 方法如下:
git reset --hard
git pull其中git reset是針對版本,如果想針對文件回退本地修改,使用git checkout HEAD file/to/restore
英文版pull和push教程(備用)
In below section, I am writing step by step how I pushed the code in Github repo successfully
Follow the given below steps from where(Desktop/Server) you want to push the code into Github repo :
Step 1:
Clone the github repo into your system. (You should first create a repo in Github)
Get repo url at right hand sidebar of your repo page. Replace your github repo』s HTTPS url with https://github.com/USER-NAME/REPO-NAME.git
git clone https://github.com/USER-NAME/REPO-NAME.git
For EXAMPLE:
git clone https://github.com/sharadchhetri/testrepo.git
After cloning will be finished, you will find the directory name matching with the name of Github repo name you just cloned.
Step 2 : (Unnecessary)
Copy your code or files to REPO-NAME directory
For example, I have saved my codes in my desktop at /home/sharad/MyCode directory. Here, I am copying all codes and child directories into REPO-NAME directory .
cp -rvf /home/sharad/MyCode/* /home/sharad/testrepo
Step 3 :
Change to git cloned directory i.e REPO-NAME.
cd REPO-NAME
Step 4 :
Set Github account username and email id for authentication
git config --global user.email "your-email-id@example.com"git config --global user.name "Your-Github-UserName"
Step 5:
Set Github URL. (This section is actually solution of this problem)
git remote set-url origin https://Your-Github-UserNAme@github.com/Your-Github-UserNAme/REPO-NAME
For EXAMPLE.
git remote set-url origin https://sharadchhetri@github.com/sharadchhetri/testrepo.git
Step 6:
Now add all files and directory by using below given command inside REPO-NAME directory
(There is a dot in command, can you see that!)git add .
Step 7:
Now write comment on commit
git commit -m my first commit
Step 8:
Now push the code in Github repo.
Be ready with your github account password because it will ask.git push -u origin master
I hope the solution also work for you.
推薦閱讀:
※git標籤
※Git倉庫中的目錄名稱變了,能識別為重命名嗎?
※【Git操作系列】Git由淺入深之安裝與配置
※Git 在window下的搭建
※Git速查表匯總