Git 常見管理命令

簡單快速的重要參考:

Git一分鐘上手: 1. http://blog.sojingle.net/programming/vcs/use-git-manage-individual-sourcecode

使用Git進行小項目代碼管理 2. http://www.enjoyrails.com/wikis/Git%E4%B8%80%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B


新機器上配置與assembla的連接:

Here are some instructions that may help and will also help you locate the key on your local system if you are using Windows.

1. Set your global settings first:
git config—global user.name “les***” 
git config—global user.email “les***@gmail.com” 
Your email address must be same as in Assembla

2. Generating a new key:
ssh-keygen -t rsa -C “les***@gmail.com” 
Do not give it a filename. Just hit enter.
Creates a public and private key. Will ask for passphrase but you don’t need one. Just hit enter.

3. In Windows, the key will appear at C:\Users\name\.ssh on default

不輸入文件,也不輸入密碼吧
Copy public key (rsa_id.pub) into your Profile on Assembla at:
https://www.assembla.com/user/edit/edit_git_set…
Best way to do this is to open it in Notepad and save it as rsa_id.txt without making any
changes. Then upload it to your Assembla profile.

4. 在assembla的 http://www.assembla.com/user/edit/edit_git_settings界面導入:id_rsa.pub



幾個重要的參考站點:

1. http://gitref.org/

2. http://www.kernel.org/pub/software/scm/git/docs


0. git remote add origin [email protected]:***.git

0. git push

參考: http://www.kernel.org/pub/software/scm/git/docs/git-show-ref.html

git push origin master

Find a ref that matches master in the source repository (most likely, it would find refs/heads/master), and update the same ref (e.g.refs/heads/master) in origin repository with it. If master did not exist remotely, it would be created.

git push origin HEAD

A handy way to push the current branch to the same name on the remote.

git push origin master:satellite/master dev:satellite/dev

Use the source ref that matches master (e.g. refs/heads/master) to update the ref that matches satellite/master (most probablyrefs/remotes/satellite/master) in the origin repository, then do the same for dev and satellite/dev.

git push origin HEAD:master

Push the current branch to the remote ref matching master in the origin repository. This form is convenient to push the current branch without thinking about its local name.

git push origin master:refs/heads/experimental

Create the branch experimental in the origin repository by copying the current master branch. This form is only needed to create a new branch or tag in the remote repository when the local name and the remote name are different; otherwise, the ref name on its own will work.

git push origin :experimental

Find a ref that matches experimental in the origin repository (e.g. refs/heads/experimental), and delete it.


另外,重要的事情:

git push -u origin branch_1.2:DEVELOP/admin/branch_1.2

-u "Upstream" would refer to the main repo that other people will be pulling from, e.g. your GitHub repo. The -u option automatically sets that upstream for you, linking your repo to a central one. That way, in the future, Git "knows" where you want to push to and where you want to pull from, so you can use git pull or git push without arguments. A little bit down, this article explains and demonstrates this concept.



1. git reset 回退

First, you must know there are three states in Git management


Working directory -----(add)--->    Index    ------(commit)---------->  Head 

最最常用的:

取消剛剛的最近的一個commit,但是不覆蓋你當前文件: git reset --mixed HEAD^

參考:http://blog.wu-boy.com/2010/08/git-%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%EF%BC%9A%E5%88%A9%E7%94%A8-git-reset-%E6%81%A2%E5%BE%A9%E6%AA%94%E6%A1%88%E3%80%81%E6%9A%AB%E5%AD%98%E7%8B%80%E6%85%8B%E3%80%81commit-%E8%A8%8A%E6%81%AF/


reset命令有3種方式:

  1. git reset –mixed:此爲默認方式,不帶任何參數的git reset,即時這種方式,它回退到某個版本,只保留源碼,回退commit和index信息

  2. git reset –soft:回退到某個版本,只回退了commit的信息,不會恢復到index file一級。如果還要提交,直接commit即可

  3. git reset –hard:徹底回退到某個版本,本地的源碼也會變爲上一個版本的內容

此文之前,強調一下Git HEAD的作用:

You can think of the HEAD as the "current branch". When you switch branches with git checkout, the HEAD revision changes to point to the tip of the new branch.

You can see what HEAD points to by doing:

cat .git/HEAD

In my case, the output is:

$ cat .git/HEAD
ref: refs/heads/master

It is possible for HEAD to refer to a specific revision that is not associated with a branch name. This situation is called a detached HEAD.


以下是一些reset的示例,來自網絡,忘了出處了,歡迎留意之處一下:

摺疊複製代碼

  1. #回退所有內容到上一個版本  

  2. git reset HEAD^  

  3. #回退a.py這個文件的版本到上一個版本  

  4. git reset HEAD^ a.py  

  5. #向前回退到第3個版本  

  6. git reset –soft HEAD~3  

  7. #將本地的狀態回退到和遠程的一樣  

  8. git reset –hard origin/master  

  9. #回退到某個版本  

  10. git reset 057d  

  11. #回退到上一次提交的狀態,按照某一次的commit完全反向的進行一次commit  

  12. git revert HEAD  

如果我們某次修改了某些內容,並且已經commit到本地倉庫,而且已經push到遠程倉庫了

這種情況下,我們想把本地和遠程倉庫都回退到某個版本,該怎麼做呢?

前面講到的git reset只是在本地倉庫中回退版本,而遠程倉庫的版本不會變化

這樣,即時本地reset了,但如果再git pull,那麼,遠程倉庫的內容又會和本地之前版本的內容進行merge

這並不是我們想要的東西,這時可以有2種辦法來解決這個問題:

  1. 直接在遠程server的倉庫目錄下,執行git reset –soft 10efa來回退。注意:在遠程不能使用mixed或hard參數

  2. 在本地直接把遠程的master分支給刪除,然後再把reset後的分支內容給push上去,如下:

    摺疊複製代碼

    1. #新建old_master分支做備份  

    2. git branch old_master  

    3. #push到遠程  

    4. git push origin old_master:old_master  

    5. #本地倉庫回退到某個版本  

    6. git reset –hard bae168  

    7. #刪除遠程的master分支  

    8. git push origin :master  

    9. #重新創建master分支  

    10. git push origin master  

在刪除遠程master分支時,可能會有問題,見下:

摺疊複製代碼

  1. $ git push origin :master  

  2. error: By default, deleting the current branch is denied, because the next  

  3. error: 'git clone' won't result in any file checked out, causing confusion.  

  4. error:  

  5. error: You can set 'receive.denyDeleteCurrent' configuration variable to  

  6. error: 'warn' or 'ignore' in the remote repository to allow deleting the  

  7. error: current branch, with or without a warning message.  

  8. error:  

  9. error: To squelch this message, you can set it to 'refuse'.  

  10. error: refusing to delete the current branch: refs/heads/master  

  11. To [email protected]:gitosis_test  

  12.  ! [remote rejected] master (deletion of the current branch prohibited)  

  13. error: failed to push some refs to '[email protected]:gitosis_test'  

這時需要在遠程倉庫目錄下,設置git的receive.denyDeleteCurrent參數

摺疊複製代碼

  1. git receive.denyDeleteCurrent warn  

然後,就可以刪除遠程的master分支了

雖然說有以上2種方法可以回退遠程分支的版本,但這2種方式,都挺危險的,需要謹慎操作……



2. git merge

current branch: testb

Need to merge from trunk,  just: git merge trunk

This will merge trunk to current testb

參考: http://fsjoy.blog.51cto.com/318484/245081


3. git merge 1 file 選擇合併(或者拉取)單個文件

Command from the above link:

#You are in the branch you want to merge to
git checkout <branch_you_want_to_merge_from> <file_paths...>

leslin@ubtServer:/data/flume-truck/flume$ git branch 

  FLUME-1240
* FLUME-1240_1
  trunk

leslin@ubtServer:/data/flume-truck/flume$ git checkout FLUME-1240 bin/flume-ng 
leslin@ubtServer:/data/flume-truck/flume$ git status 
# On branch FLUME-1240_1
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   bin/flume-ng
#

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