github簡單命令


1.安裝
yum install -y githtml

2.配置賬戶(github.com註冊)
git config --global user.name goozgk
git config --global user.email goozgk@qq.comgit

3.建立一個新的倉庫repo
mkdir -p /work/git_repo
cd /worl/git_repo
git init # 初始化github

4.編寫程序
vim test.pyshell

5.查看狀態
git status
[root@localhost git_repo]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.py
nothing added to commit but untracked files present (use "git add" to track)vim

6.暫存 – git add服務器

[root@localhost git_repo]# git add test.py
[root@localhost git_repo]# git add -A # if you want to commit all the file in the current dir.
[root@localhost git_repo]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test.py
#
[root@localhost git_repo]#less


7.提交 – git commit 到本地倉庫(master)中。
[root@localhost git_repo]# git commit -m "create test.py"
[master (root-commit) 8fe112e] Mon Jul 11 07:31:36 PDT 2016
1 files changed, 4 insertions(+), 0 deletions(-)
create mode 100755 test.py
[root@localhost git_repo]#ssh

 

8.連接遠端倉庫 – git remote add
git remote add origin https://github.com/goozgk/work.git
# 一般主遠端倉庫被稱爲origin; 能夠有其餘倉庫,另起名字便可
# git remote add <remoteRepositoryName> <remoteRepositroyURL>ide

9.上傳到服務器 – git push
git push origin master
# git push 遠端倉庫名字(origin) 分支名字(master)測試

[root@localhost git_repo]# git push origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/goozgk/work.git/info/refs

fatal: HTTP request failed

失敗。。。

改成使用ssh登錄,實際上github暫不支持http的讀寫權限
ssh-keygen -t rsa 建立祕鑰對(參考http://www.cnblogs.com/goozgk/p/5663453.html)
複製id_rsa.pub的內容到github.com本身項目主頁settings->Deploy Keys中

設置並確認
git remote -v
git remote set-url origin ssh://git@github.com/goozgk/work.git
git remote -v

格式:
git remote set-url <name> https://yourusername@github.com/<repo>.git # https暫不可用沒法驗證
git remote set-url <name> ssh://git@github.com/<username>/<repo>.git

[root@localhost .ssh]# ssh -T goozgk@github.com
Hi goozgk/work! You've successfully authenticated, but GitHub does not provide shell access.

能夠了!

[root@localhost git_repo]# git push
To ssh://git@github.com/goozgk/work.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://git@github.com/goozgk/work.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
[root@localhost git_repo]#


緣由:origin分支有更新,不容許提交,應先把origin fetch回本地,merge後提交
----------------------------------------------------------
git fetch origin
# Fetches updates made to an online repository
git merge origin YOUR_BRANCH_NAME(master) [git rebase #注意,和git merge有區別的!]
# Merges updates made online with your local work

OR

git pull origin YOUR_BRANCH_NAME(master) # 將遠端倉庫內容pull回本地並merge
# Grabs online updates and merges them with your local work

 

----------------------------------------------------------
git push -f
# 強推,會破壞remote倉庫內容,不建議使用
----------------------------------------------------------

git push origin master

成功!!!


10.克隆倉庫 – git clone
放在Github上的開源項目,人們能夠看到你的代碼。能夠使用 git clone進行下載到本地。
# git clone https://github.com/goozgk/work.git
本地也會建立一個新的倉庫,並自動將github上的分支設爲遠端分支。
[root@localhost work]# git remote -v
origin https://github.com/goozgk/work.git (fetch)
origin https://github.com/goozgk/work.git (push)

。。。惋惜github.com不支持https上傳修改,你也沒有ssh KEY, 就是說你只能看,而不能隨意修改別人的代碼。只須要把你本身的公鑰key部署到github.com中的project中就能夠了。。。廢話。。


It's not allow to use the same KEY to manipulate 2 or more Projects int the Github.com,ちょっとおかしいね!
It's also not allow 2 accounts to use the same key to access one Project。。。沒有驗證如何控制的。。


11.分支

建立新分支
git branch <bra1>

切換到新分支
git checkout <bra1>

刪除分支
git branch -d <BranchName>

查看當前分支
git branch
git show-branch

切換到各個分支後,新建的文件,各個分支均可以看到的!!!(提交以前),可是在某個分支上commit後,只有這個分支能夠看到!!!

在分支上測試完成後,git branch master切換回master分支,執行
git merge <branchName>; git branch -d <branchName>

12.查看log
git log
git log --oneline

查看具體提交了什麼
git show <id number> #前幾位就行,只要不會衝突

比較兩次提交的不一樣
git diff [commit-from]..[commit-to]

13.回滾某個文件到以前的版本
git checkout 09bd8cc1 hello.txt
# 格式git checkout <id number> <fileFullPath>

14.複雜指令
git commmit --amend #打回最新提交到暫存區
git revert HEAD #打回最新提交到暫存區
git revert <id number> #打回提交<id number>到暫存區

15.配置 .gitignore
在項目根目錄建立.gitignore文件,在文件中列出不須要提交的文件名,文件夾名,每一個一行,.gitignore文件須要提交,就像普通文件同樣

16.本地建兩個repository,模擬本地和遠端

[root@localhost work]# git clone ssh://git@github.com/goozgk/work.git
Initialized empty Git repository in /work/work/.git/
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 13 (delta 2), reused 9 (delta 1), pack-reused 0
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (2/2), done.
[root@localhost work]# cd work/
[root@localhost work]# git push
Everything up-to-date
[root@localhost work]# git remote -v
origin ssh://git@github.com/goozgk/work.git (fetch)
origin ssh://git@github.com/goozgk/work.git (push)
[root@localhost work]#
[root@localhost work]#至此,建立github.com和本地(remote <>local)模式的普通系統

[root@localhost work]#
[root@localhost work]# cd ..
[root@localhost work]# mkdir a;cd a
[root@localhost a]# git clone /work/work/.git/ . #github.com < /work/work/.git < /work/a/.git 系統創建,看似完美!
Initialized empty Git repository in /work/a/.git/
[root@localhost a]# ll
total 12
-rw-r--r-- 1 root root 6 Jul 18 07:13 README.md
-rwxr-xr-x 1 root root 26 Jul 18 07:13 test1.py
-rwxr-xr-x 1 root root 26 Jul 18 07:13 test.py
[root@localhost a]# git push #提交也ok
Everything up-to-date
[root@localhost a]# git remote -v #確認也沒問題
origin /work/work/.git/ (fetch)
origin /work/work/.git/ (push)
[root@localhost a]# git branch
* master
[root@localhost a]#
[root@localhost a]#
[root@localhost a]#
[root@localhost a]# ll
total 12
-rw-r--r-- 1 root root 6 Jul 18 07:13 README.md
-rwxr-xr-x 1 root root 26 Jul 18 07:13 test1.py
-rwxr-xr-x 1 root root 26 Jul 18 07:13 test.py
[root@localhost a]# touch a1 #建立新文件
[root@localhost a]# git push
Everything up-to-date
[root@localhost a]# git add -A
[root@localhost a]# git commit -m "add a1 on branch a" #提交,ok
[master 2c43137] add a1 on branch a
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a1
[root@localhost a]# git push #遠程push失敗,不推薦此種方式!會引發混亂不一致。還沒具體深究。。。。。。
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 306 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /work/work/.git/
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to '/work/work/.git/'
[root@localhost a]# 

 
相關文章
相關標籤/搜索