您的位置:首页 > 其它

git 实用积累

2015-01-23 21:22 225 查看
https://wiki.bnl.gov/dayabay/index.php?title=Synchronizing_Repositories

http://www.cppblog.com/kuchao/archive/2009/12/18/103462.html

从trunk svn 上面取数据(保存到everest 服务器上面):

extra/git/ #git svn clone http://svn.ea.mot.com/dev/bsg/ELYMUS_work
每日更新:

extra/git/ELYMUS_work# git svn rebase

copy 数据到本地,从everest上面;

#git clonet chkq76@everest://extra/git/ELYMUS_work

从everest上面更新数据:

#git pull

SVN 是集中式的,而 Git 是分布式的;因此,Git 不会提供一个逐渐变大的修订号,因为不存在 “最新的修订”。它仍然使用唯一的修订 ID;只不过唯一修订 ID 本身并没有 SVN 修订号那么有用。

1, 在本地创建一个git 副本

从svn 创建一个副本

#sudo yum install git-svn

[leo@localhost mot_develop]$ git svn clone http://svn.ea.mot.com/dev/bsg/branches/DEV_bcc_extdvr
从git 服务器创建一个副本

[leo@localhost mot_develop]$git clone chkq76@shen.ap.mot.com:/extra/git/trunk trunk_timeshift

[leo@localhost mot_develop]$ cd trunk_timeshift/

[leo@localhost trunk_timeshift]$ git clone chkq76@shen.ap.mot.com:/extra/git/makesystem makesystem

[leo@localhost trunk_timeshift]$ git clone chkq76@shen.ap.mot.com:/extra/git/common/kattscripts/common common/kattscripts

git-clone 可利用各种网络协议访问远端机器中的 Git 仓库,从中导出完整的工作树到本地。在上述示例中, 通过 SSH 协议访问了shen.ap.mot.com 机器上的 chkq76 账户的/extra/git/trunk仓库并进行导出,从而在当前目录下建立了trunk_timeshift工作树。

2, 图形化显示 版本等相关信息

可以查看version; version tree; 某个version 的具体更改信息;

[leo@localhost mot_develop]$ cd trunk_timeshift/

[leo@localhost trunk_timeshift]$ gitk &

3, 创建一个新的branch

[leo@localhost trunk_timeshift]$git branch DEV_MY_TIMESHIFT 36c4b714b8b38029ed1782bbd020

4, 切换到某一个分支或者版本:

切换到branch: [leo@localhost trunk_timeshift]$git checkout DEV_MY_TIMESHIFT

切换到version: [leo@localhost trunk_timeshift]$git checkout caaac0f4ae67f588910774a8c8e1b1b575ebf7e1

切换到当前主分支: [leo@localhost trunk_timeshift]$git checkout master

5, 查看分支:

所有所有分支情况:git branch -a

查看本地分支:#git branch

6, 删除一个分支;删除一个branch

[leo@localhost trunk_timeshift]$git branch -d DEV_MY_TIMESHIFT

7, 查看历史:

最近操作历史: [leo@localhost trunk_timeshift]$ git log -1

查看所有历史:$git log

查看所有分支的历史:#git log master

8,merge

查看状态:

# git status

revert到某个状态:

#git reset是指将当前head的内容重置,不会留log信息。

#git reset HEAD filename 从暂存区中移除文件

#git reset –hard HEAD~3 会将最新的3次提交全部重置,就像没有提交过一样。

#git reset –hard commit (38679ed709fd0a3767b79b93d0fba5bb8dd235f8) 回退到 38679ed709fd0a3767b79b93d0fba5bb8dd235f8 版本

根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:

#git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息

#git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可

#git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容

#git reset --hard HEAD

Undo a commit, making it a topic branch

$ git branch topic/wip (1)

$ git reset --hard HEAD~3 (2)

$ git checkout topic/wip (3)

(1). You have made some commits, but realize they were premature to be in the "master" branch. You want to continue polishing them in a topic

branch, so create "topic/wip" branch off of the current HEAD.

(2). Rewind the master branch to get rid of those three commits.

(3). Switch to "topic/wip" branch and keep working.

Undo a merge or pull

$ git pull (1)

Auto-merging nitfol

CONFLICT (content): Merge conflict in nitfol

Automatic merge failed; fix conflicts and then commit the result.

$ git reset --hard (2)

$ git pull . topic/branch (3)

Updating from 41223... to 13134...

Fast-forward

$ git reset --hard ORIG_HEAD (4)

(1) Try to update from the upstream resulted in a lot of conflicts; you were not ready to spend a lot of time merging right now, so you decide

to do that later.

(2). "pull" has not made merge commit, so "git reset --hard" which is a synonym for "git reset --hard HEAD" clears the mess from the index file

and the working tree.

(3). Merge a topic branch into the current branch, which resulted in a fast-forward.

(4). But you decided that the topic branch is not ready for public consumption yet. "pull" or "merge" always leaves the original tip of the

current branch in ORIG_HEAD, so resetting hard to it brings your index file and the working tree back to that state, and resets the tip of the

branch to that commit.

从 local-DEV_bcc_extdvr merge 到 DEV_CHKQ76_timeshift分支:

#git merge local-DEV_bcc_extdvr

# git mergetool

9, 图形化界面比较

比较trunk的 36c4b714b8b38029ed1782bbd02081a388084453标签与 local-DEV_bcc_extdvr的比较:

# git diff 36c4b714b8b38029ed1782bbd02081a388084453 local-DEV_bcc_extdvr | kompare -o -&

文字比较: git diff 36c4b714b8b38029ed1782bbd02081a388084453 local-DEV_bcc_extdvr

#git diff 4316b5bf2122e00b2b18eb9bee51d3c38c3e8e21 8732386be75de76ad5a04e1f98044d5a0b68a537 | kompare -o -&

# git diff 4316b5bf2122e00b2b18eb9bee51d3c38c3e8e21 8732386be75de76ad5a04e1f98044d5a0b68a537 >>test.tttttttttt

10, 用meld比较源代码: 比较trunk@186332 local-DEV_bcc_extdvr@head

copy local-DEV_bcc_extdvr@head code into some diretory:

[leo@leo trunk]$git branch local-DEV_bcc_extdvr

[leo@leo trunk]$cp -fr * /mnt/sdb1/code_compare/local-DEV_bcc_extdvr

copy trunk@186332 code into some diretory:

[leo@leo trunk]$ git checkout master

[leo@leo trunk]$ git log

commit 36c4b714b8b38029ed1782bbd02081a388084453

Author: pwr783 <pwr783@42af1f72-66f0-0310-a2c5-8546161d1ea4>

Date: Wed May 25 15:03:37 2011 +0000

Merge DEV_trunk_fixes: PCKreatv fix, remove Helvetica Neue, clean up bcmxxxx.

MERGE(http://svn.ea.mot.com/dev/bsg/trunk@r186306, http://svn.ea.mot.com/dev/bsg/branches/DEV_trunk_fixes@r186315).
Smoketests: ok.

git-svn-id: http://svn.ea.mot.com/dev/bsg/trunk@186332 42af1f72-66f0-0310-a2c5-8546161d1ea4

[leo@leo trunk]$ git checkout 36c4b714b8b38029ed1782bbd02081a388084453

[leo@leo trunk]$ cp -fr * /mnt/sdb1/code_compare/mot_dev/trunk_186332

begin compare code:

[leo@leo code_compare]$meld local-DEV_bcc_extdvr trunk_186332 &

11 git svn(git-svn):

#sudo yum install git-svn

use svn-git download code from svn repo:

[leo@localhost mot_develop]$ git svn clone http://svn.ea.mot.com/dev/bsg/branches/DEV_bcc_extdvr
update to new version svn:

[leo@leo trunk]$ git svn fetch

[leo@leo trunk]$ git svn

Usage: git svn <command> [options] [arguments]

Available commands:

blame Show what revision and author last modified each line of a file

branch Create a branch in the SVN repository

clone Initialize and fetch revisions

create-ignore Create a .gitignore per svn:ignore

fetch Download new revisions from SVN

log Show commit logs

mkdirs recreate empty directories after a checkout

rebase Fetch and rebase your working directory

show-ignore Show svn:ignore listings

12, SVN and GIT 使用比较:

12.1 SVN 使用; 在 SVN 下放置一个目录

让我们从一个简单的示例开始:使用 SVN 跟踪目录的内容。将需要一个 SVN 服务器,当然,还需要一个文件目录,以及该服务器上的一个帐户,并能够通过至少一种方式进行提交。在开始之前,首先添加并提交目录:

清单 1. 在 SVN 下设置一个目录

% svn co http://svnserver/...some path here.../top

% cd top

% cp -r ~/my_directory .

% svn add my_directory

% svn commit -m 'added directory'

这些操作有什么作用?现在,您可以从这个目录中获得已提交文件的最新版本,重命名它们,创建新的文件或目录,将更改提交到现有文件,等等:

清单 2. SVN 下的基本文操作

# get latest

% svn up

# what's the status?

% svn st

# delete files

% svn delete

# rename files (really a delete + add that keeps history)

% svn rename

# make directory

% svn mkdir

# add file

% svn add

# commit changes (everything above, plus any content changes)

% svn commit

我不会详细解释这些命令,但是需要牢记它们。要获得有关这些命令的帮助,只需要输入 svn help COMMAND,Subversion 将显示一些基本帮助;可以从参考手册中了解更多。

12.2 GIT 使用;

12.2.1: server端设置:

bash-4.1$ mkdir git_repo

bash-4.1$ cd git_repo/

bash-4.1$ git config --global user.name "leosu"

bash-4.1$ git config --global user.email "bamboolsu@gmail.com"

bash-4.1$ vim ~/.gitconfig

bash-4.1$ pwd

/extra/chkq76/git_repo

初始化git库: bash-4.1$ git init

通过添加任一个测试文件,创建第一个branch:

bash-4.1$ vim test.c

bash-4.1$ git add test.c

bash-4.1$ git commit -m "my test add"

12.2.2: client端设置:

[leo@leo git_repo]$ pwd

/mnt/sdb1/git_res

[leo@leo git_res]$ git clone chkq76@everest:/extra/chkq76/git_repo

[leo@leo git_repo]$ mkdir etc_bak

[leo@leo git_repo]$ mkdir my_home_bak

[leo@leo git_repo]$ cp -fr ~/bin my_home_bak/

[leo@leo git_repo]$cp -fr ~/.vim my_home_bak/

[leo@leo git_repo]$cp -fr /etc/hosts etc_bak/

[leo@leo git_repo]$ cp -fr /etc/yum.repos.d etc_bak/

[leo@leo git_repo]$ git add my_home_bak etc_bak[leo@leo git_repo]$ git commit -m "etc add"

提交本地(本地的version 或者HEAD)变更到服务器(origin代表clone命令的来源处)的(注:1,此时服务器端不能checkout master branch【或者说你要提交的branch不能被被人checkout】,否则不能提交成功; 2,此时假如服务器你要提交的branch服务器端有修改,你必须先git pull 到最新的version, 然后才能进行push操作):

[leo@leo git_repo]$ git push origin HEAD

[leo@leo git_repo]$rm -fr *checkout 出当前分支所有的资源: [leo@leo git_repo]$git checkout *

13, git 命令积累:

git-log 命令可以查看当前项目的日志 #git log #git log -num

如果你想看一下每一次版本的大致变动情况, 可使用以下命令: # git log --stat --summary

如果我们将项目版本号用作 git-show 命令的参数,即可查看该次项目版本的更新细节:[leo@leo git_bak_repo]$ git show f4badba9a5ef5aec0e24889594d420468b81a538

$ git show f4ba # 一般只使用版本号的前几个字符即可

$ git show HEAD # 显示当前分支的最新版本的更新细节

$ git show HEAD^ # 查看 HEAD 的父版本更新细节

$ git show HEAD^^ # 查看 HEAD 的祖父版本更新细节

$ git show HEAD~4 # 查看 HEAD 的祖父之祖父的版本更新细节

$ git tag v0.1 dfb02

$ git show

$ git gc #####git-gc - Cleanup unnecessary files and optimize the local repository

帮助: $ man git-reset

14, 删除git中的分支信息

在使用git进行协作开发时,我们经常需要将自己的修改生成patch发给被人,但是在修改代码的过程中我们进行了很多次的提交,如何生成从最初的代码状态到最终代码状态的patch呢?下面要介绍的功能是应对这中情况。

现假设我们git软件仓库中的分支情况如下:

a-->b-->c

也就是说我们的代码从状态a修改到状态b,进行一次提交,然后再修改到状态c,进行一次提交。这时我们已经肯定由a到c的修改是正确的,不再需要状态b了,并且要把从a到c的变化生成一个patch发送给别人。如果直接打包的话会生成两个path,那么如何生成一个patch呢,这时就需要git-reset命令。

首先给状态a创建一个tag,假设名称为A,

#

然后执行

#git-reset --soft A

这样我们的软件仓库就变为

a

状态b和状态c都已经被删除了,但是当前的代码并没有被改变,还是状态c的代码,这时我们做一次提交,软件仓库变成下面的样子:

a-->d

状态d和状态c所对应的代码是完全相同的,只是名字不同。现在就可以生成一个patch打包发给别人了。

删除当前工作目录中 不在版本控制中的文件

很多时候,我们会做个build,然后呢,工作目录中多了一堆不需要的文件,文件夹,看着特别

别扭。也许,你会说,没关系啊,我早就把这些生成的中间文件,文件夹写到gitignore里面去

了,这些多余的货色,不会影响到我的工作,也不会把仓库搞乱啊。恩,是有道理。但是,就像

眼里多了一个沙子一样,总是想要揉掉它的。git clean就可以用来做这件事情,干净利落,比one

更加赞。
git st
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   abc.txt
#   test/
nothing added to commit but untracked files present (use "git add" to track)

git clean -f -d
# Removing abc.txt
# Removing test/

git st
# On branch master
nothing to commit, working directory clean


啊,她还是原来的她,不乱了。

清理远程分支

如果在github上贡献了自己的代码,并且在github上删除了一个分支,那么,在本地,还是能看到

这个分支存在。你这个时候去push会失败,我们需要prune一下远程分,把不存的分支信息从本地删除。

可以通过git remote prune <remote>来实现。
git branch -a
# * develop
#   master
#   remotes/origin/HEAD -> origin/master
#   remotes/origin/develop
#   remotes/origin/gh-pages
#   remotes/origin/master

git push origin :develop
# error: unable to delete 'develop': remote ref does not exist
# error: failed to push some refs to 'git@github.com:pengfei-xue/beego.git'

git remote prune origin
# Pruning origin
# URL: git@github.com:pengfei-xue/beego.git
#  * [pruned] origin/develop
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: