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
#

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