git遠程操作

clone

root@ve:/home/nanyt# git clone ssh://[email protected]/home/gittest
Cloning into 'gittest'...
[email protected]'s password: 
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
Checking connectivity... done.

與遠端倉庫保持一致

root@ve:/home/nanyt/gittest# ls -al
total 16
drwxr-xr-x 3 root root 4096 Oct  9 19:51 .
drwxr-xr-x 6 root root 4096 Oct  9 19:51 ..
drwxr-xr-x 8 root root 4096 Oct  9 19:51 .git
-rw-r--r-- 1 root root   22 Oct  9 19:51 README
root@ve:/home/nanyt/gittest# cat README 
init file
modify file
root@ve:/home/nanyt/gittest# git log
commit 5fa67a97a2b1a813036950155ef56cfdf0037de9
Author: Leon Nan <[email protected]>
Date:   Mon Oct 10 03:45:24 2016 +0800

    modify file

commit 0f3e0e50082bab03bd36188c1f41b11133454e91
Author: Leon Nan <[email protected]>
Date:   Mon Oct 10 03:45:01 2016 +0800

    init file

在本地修改文件前需要修改本地的用戶名和郵箱

root@ve:/home/nanyt/gittest# git config user.name "Leon Nan Remote"
root@ve:/home/nanyt/gittest# git config user.email [email protected]

修改文件

root@ve:/home/nanyt/gittest# echo "modify file remote" >> README
root@ve:/home/nanyt/gittest# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README

no changes added to commit (use "git add" and/or "git commit -a")

增加文件並交付

root@ve:/home/nanyt/gittest# git add -A
root@ve:/home/nanyt/gittest# git commit -m "modify file remote"
[master 82783ae] modify file remote
 1 file changed, 1 insertion(+)

push到遠端

root@ve:/home/nanyt/gittest# git push origin master  
[email protected]'s password: 
Counting objects: 5, done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
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 ssh://[email protected]/home/gittest
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://[email protected]/home/gittest'

返回失敗,原因是遠端倉庫不是bare的,也就是說遠程repo有工作區,且已經被checkout某個branch,這種情況下git不允許你push,因爲如果你要push的代碼跟遠程代碼有conflict,在你這邊是沒有辦法resolve的。

解決方案1:修改遠端git接受設置後再次push

[root@localhost gittest]# git config receive.denyCurrentBranch ignore
root@ve:/home/nanyt/gittest# git push origin master            
[email protected]'s password: 
Counting objects: 5, done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://[email protected]/home/gittest
   5fa67a9..82783ae  master -> master
root@ve:/home/nanyt/gittest#

解決方案2:將遠端倉庫變成bare的

[root@localhost gittest]# git init --bare
Initialized empty Git repository in /home/gittest/
root@ve:/home/nanyt/gittest# git push origin master                   
[email protected]'s password: 
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: warning: updating the current branch
To ssh://[email protected]/home/gittest
   82783ae..402c001  master -> master

參考文檔:

http://blog.sina.com.cn/s/blog_5d49e36701016zbu.html

http://ruby-china.org/topics/12631

http://blog.csdn.net/kuaiguixs/article/details/52094692

http://blog.csdn.net/haomcu/article/details/8174805




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章