您的位置:首页 > 编程语言

git新手使用指南

2014-11-05 15:21 330 查看
在你安装好git,并且注册过gitoschina或是github账号之后,我们开始来往远程项目仓库中推送我们的第一个项目文件

(如果你需要git开发指南说明书的话,请到我上传的资源中下载;)http://download.csdn.net/detail/mr_oorange/8231581

提示:Git文档忽略机制 
工作目录中有一些文件是不希望接受Git 管理的,譬如程序编译时生成的中间文件等等。Git 提供了文档忽略机制,可以将工作目录中不希望接受Git 管理的文档信息写到同一目录下的.gitignore 文件中。 

在项目根目录下建立 .gitignore 文件
过滤文件夹设置:

mtk/       表示过滤这个文件夹

过滤文件设置:

指定过滤某种类型的文件:

*.zip

*.rar

*.via

*.tmp

*.err

指定过滤某个文件:

/mtk/do.c

/mtk/if.h

首先来到在你需要push的文件那个目录下

 Git初始化配置 
 配置使用git仓库的人员姓名 
       git config --global user.name "Your Name Comes Here" 

配置使用git仓库的人员email 
       git config --global user.email ****@***.com(eg:git config --global user.email****@163.com)

开始干正事了。。。。

1.git init git(

初始化
Git
仓库
初始化
Git
仓库
初始化配置)2.git add +文件名 eg:git add  main.c(git add .*表示添加当前目录下全部文件)
3.git commit -m "说明"eg:git commit -m"first commit"

4.git remote add origin +远程仓库的链接 eg:git remote add origin https://git.oschina.net/Ooorange/*****.git(ps:第一次推送的时候需要此步骤,以后不用此)
5.git push -u origin master  (ps:此时会提示你输入帐户名和密码(密码在输入的时候不可见))

到此完成推送

(-------------------------------------------割-----------------------------------------------)

6.如果想获取其它分支信息,需要使用”git branch –r”来查看所有分支

7.如果需要将远程的其它分支代码也获取过来,可以使用命令:git checkout -b 本地分支名 远程分支名

8.错误提示:fatal: remote origin already exists.

解决办法:$ git remote rm origin

错误提示:error: failed to push som refs to ........

解决办法:$ git pull origin master //先pull 下来 再push 上去

 

在使用git 处理对android的修改的过程之中总结的.但不完善

 

Git push

$ git push origin test:master         // 提交本地test分支作为远程的master分支

$ git push origin test:test              // 提交本地test分支作为远程的test分支

如果想删除远程的分支呢?类似于上面,如果:左边的分支为空,那么将删除:右边的远程的分支。

 

$ git push origin :test              // 刚提交到远程的test将被删除,但是本地还会保存的,不用担心。

举个例子:

       git push origin  :origin/Android_HK_K501_77-W916 //由于:前面为空所以本来在服务器上面存在的分支origin/Android_HK_K501_77-W916和Android_HK_K501_77-W916现在就只有一个了,因为分支origin/Android_HK_K501_77-W916被删除了,但是本地的还在

 

常见错误:

1.error:failed to push some refs to ...

当要push代码到git时,出现提示:

error:failed to push some refs to ...

Dealing with “non-fast-forward” errors

From time to time you may encounter this error while pushing:

$ git push origin master 

To ../remote/ 

 ! [rejected]        master -> master (non-fast forward) 

error: failed to push some refs to '../remote/' 

To prevent you from losing history, non-fast-forward updates were rejected

Merge the remote changes before pushing again.  See the 'non-fast forward'

section of 'git push --help' for details.

 

问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不允许你直接把你的代码覆盖上去。于是你有2个选择方式:

 

强推,即利用强覆盖方式用你本地的代码替代git仓库内的内容

git push -f

 

2. 先把git的东西fetch到你本地然后merge后再push

$ git fetch

$ git merge

这2句命令等价于

$ git pull 

可是,这时候又出现了如下的问题:

上面出现的 [branch "master"]是需要明确(.git/config)如下的内容

[branch "master"]

    remote = origin

    merge = refs/heads/master

这等于告诉git2件事:

1,当你处于master branch, 默认的remote就是origin。

2,当你在master branch上使用git pull时,没有指定remote和branch,那么git就会采用默认的remote(也就是origin)来merge在master branch上所有的改变

如果不想或者不会编辑config文件的话,可以在bush上输入如下命令行:

$ git config branch.master.remote origin 

$ git config branch.master.merge refs/heads/master 

之后再重新git pull下。最后git push你的代码吧。

 

错误信息:error: object file .git/objects/c6/884991eaac39417e314faa6685061eab18909d is empty

fatal: loose object c6884991eaac39417e314faa6685061eab18909d (stored in .git/objects/c6/884991eaac39417e314faa6685061eab18909d) is corrupt

解决方法:

1  rm .git/objects/c6/884991eaac39417e314faa6685061eab18909d

 

2 .git fsck --full

 

 

3.git reflog



(如果你需要git开发指南说明书的话,请到我上传的资源中下载;)http://download.csdn.net/detail/mr_oorange/8231581

----------------------------------------------------------------------------------以下转自:http://www.cnblogs.com/wang_yb/p/3867221.html-------------------------------------------------------------


GIT 的常规操作

常规操作也是我自己平时常用的几个命令, 学自于 pro git 这本书中


git 配置文件

git的配置文件位置

针对所有用户:/etc/gitconfig

针对当前用户: ~/.gitconfig

查看配置的方法
git config --list


修改配置的方法
git config --global user.name "wangyubin"  (修改的是~/.gitconfig)
git config --system user.name "wangyubin"  (修改的是/etc/gitconfig)



git 基本使用

clone现有仓库
git clone URL  (URL支持git,ssh,http,https等各种协议)


git中文件的各个状态
unstaged - git仓库中没有此文件的相关记录

modified - git仓库中有这个文件的记录,并且此文件当前有改动

staged - 追加,删除或修改的文件被暂时保存,这些追加,删除和修改并没有提交到git仓库

commited - 追加或修改的文件被提交到本地git仓库(git仓库中大部分都是这种文件,所以git status不显示这些文件)

查看git仓库中各文件状态
git status


初始化一个仓库
git init

在当前文件夹下生成.git目录,完成初始化,此时此文件夹下的所有文件处于unstaged状态

追加文件
git add test.c

test.c的文件变为staged状态,其他文件还是unstaged状态
5.1 追加文件的结果1 - 恢复为原先状态(变为unstaged)
git rm --cache test.c

5.2 追加文件的结果2 - 提交到git仓库(变为commited)
git commit -m "my message"


修改文件
echo "aaa"  >> test.c

test.c的状态变为modified
6.1 修改文件的结果1
git add test.c  (暂时保存修改的内容,即变为staged)

下面有2条路可以选择:

6.1.1 取消刚才的暂时保存
git reset test.c  (状态变回modified)

6.2.2 将暂存的修改提交到git仓库
git commit -m "my message"

6.2 修改文件的结果2
git checkout test.c  (将test.c恢复为git仓库中的最新版本,即变为commited状态,test.c的内容和5.2节一样)


删除文件

7.1 从git仓库和磁盘上删除文件
git rm test.c  (当前目录中删除了test.c,在git仓库中暂时删除了test.c,相当于staged状态)

7.1.1 从git仓库中删除test.c
git commit -m "my message"  (git仓库以后不再维护test.c)

7.1.2 删错了,恢复刚才的操作
git reset HEAD test.c  (恢复到删除前的状态,当前目录中已删除的test.c也恢复了,test.c仍文commited状态)

7.2 仅从git仓库中删除文件
git rm --cache test.c (当前目录中没有删除了test.c,仅在git仓库中暂时删除了test.c,相当于staged状态)

7.2.1 从git仓库中删除test.c
git commit -m "my message"  (git仓库以后不再维护test.c,但是当前目录中仍然有test.c)

7.2.2 删错了,恢复刚才的操作
git reset HEAD test.c  (和7.1.2一样)

7.3 误删除后的恢复

如果删除了一个文件,并且commit之后发现删错了。也可以恢复,
git log  (查看各次的提交信息)
git checkout commit号  (恢复到未删除前的commint号,此时删除的文件也恢复到磁盘上了)
git checkout master  (备份好删除的文件后,再回到最新状态)



git 远程仓库

查看远程仓库

1.1 简单查看-所有仓库
git remote (只能查看远程仓库的名字)

1.2 查看更多内容-所有仓库
git remote -v (远程仓库的名字及git地址)

1.3 查看单个仓库的信息
git remote show [remote-name]


新建远程仓库
git remote add [shortname] 
2.3 后期追加tag
git log --pretty=oneline (查看所有的commit号)
git tag -a [tag-name] [commit号前几位即可]


删除标签
git tag -d [tag-name]


提交标签到远程仓库
git push [remote-name] --tags
ex. git push origin --tags



git 分支

查看和切换分支
git branch (查看所有的分支及当前处于哪个分支)
git branch -v (查看所有的分支的详细信息)
git branch --merged (查看已经合并的分支)
git branch --no-merged (查看还没合并的分支)
git checkout [branch-name] (切换到某个分支)


新建分支
git branch [branch-name]  (新建一个分支)
git branch -b [branch-name] (新建一个分支并切换到这个分支上)


合并分支
git merge [branch-name]
ex. 将分支btest合并到主分支master
git checkout master
git merge btest

merge时有冲突的文件会列出来,需要手动合并
将冲突手动解决后,再次用 git status来查看是否还有 unmerged的文件。

如果没有冲突的文件,就可以 git commit 来提交这次合并了。

删除分支
git branch -d [branch-name]
或者 git branch -D [branch-name] (强制删除某个还未合并的分支)


远程分支相关

5.1 新建远程分支
git checkout [local_branch] (首先进入想要上传的分支)

git remote add [remote_repo] [remote_branch]

(这里的[remote_branch]是远程分支的名字,一般和[local_branch]同名,

[remote_repo]是远程仓库的名字)

2 向远程分支推送数据
git push [remote_repo] [remote_branch]

3 删除远程分支
git push [remote_repo] :[remote_branch] (注意远程分支前有个":")

合并分支的另一个方法:衍和

衍和可以简化master上的提交记录,使得代码可以方便的回退,

但是在公共仓库上用衍和有一定的风险。

衍和我基本用不上,这里就不赘述了。


服务器创建 git 仓库, 并将其作为远程仓库

其实 git 是分布式的 SCM. 并不存在谁是服务器, 谁是客户端的问题, 这里所说的服务器上的git仓库, 指的是多人合作开发时, 共用的, 作为最终发布版本的 git 仓库.

这个 git 仓库就相当于你在 github 上建的仓库, 会将你在各个电脑上做的代码等提交到上面进行统一管理.

服务端 (远程 git 仓库)

生成用于git服务的账户 (一般就用git)
groupadd gpxxx
useradd -m -g gpxxx gitxxx


初始化服务端的git 仓库
cd ~/
mkdir git-repo
cd git-repo
mkdir test.git
cd test.git
git --bare init


客户端 (本地 git 仓库)

新建本地git 仓库
cd ~/gitlocal
mkdir test
cd test
git init


初始化本地仓库
touch README
git add README
git commit -m 'first commit for init'


设置git用户信息
git config --global user.name "wangyubin"
git config --global user.email "xxx@xxx.com"


关联远程仓库
git remote add origin gituser@<server address>:~/test.git/


将本地仓库提交到远程
git push origin master



git 使用中遇到的一些问题


git pull 时, 远程文件与本地文件有冲突

如果远程的仓库被其他人更新了, 并且更新的内容与我自己本地编辑的内容有冲突. 这时执行 git pull 可能有如下message:
Auto-merging path/to/conflict-file
CONFLICT (content): Merge conflict in path/to/conflict-file
Automatic merge failed; fix conflicts and then commit the result.

用文本编辑器 vim 或者 emacs 之类的来编辑冲突的文件 path/to/conflict-file, 冲突的地方有类似如下的显示
<<<<<<< HEAD
App_Log.logger.debug(u'开始时间: ' + utils.datetime2str(datetime.datetime.now()))
file = request.FILES.get('file-xxx')
App_Log.logger.debug(u'结束时间: ' + utils.datetime2str(datetime.datetime.now()))

=======
file = request.FILES.get('xxxx')
>>>>>>> 3602514cc2bf1b3a64470b31ad79e07fe372add5

=====
 之上的
<<<<<<< HEAD 是本地的内容
=====
 之下的 >>>>>>>
3602514cc2bf1b3a64470b31ad79e07fe372add5 是远程的内容(这个commit号每次都会不同)

根据实际情况, 删除多余的内容(包括===== >>>>> <<<<<< 之类的), 修改冲突的地方, 如果以本地的代码为准的话, 会得到如下结果:
App_Log.logger.debug(u'开始时间: ' + utils.datetime2str(datetime.datetime.now()))
file = request.FILES.get('file-xxx')
App_Log.logger.debug(u'结束时间: ' + utils.datetime2str(datetime.datetime.now()))

然后 git commit -am '提交的信息' 就解决了冲突.

最后, 也可以将本地的修改同步到远程 git 仓库: git push


git pull 时, 本地还有未commit 的文件

从远程仓库更新时, 假使本地还有没commit的文件A, 远程仓库的A文件却被修改了. 此时进行 git pull 时有如下信息:
6a707cc..f93575d  master     -> origin/master
Updating 6a707cc..f93575d
error: Your local changes to the following files would be overwritten by merge:
apps/myapp/utils.py
Please, commit your changes or stash them before you can merge.
Aborting

此时, 如果不想将本地文件commit(可能只是临时的修改), 但是又像将远程的仓库更新下来, 可以这样:
$ git stash    # 先将自己的改变保存起来
Saved working directory and index state WIP on master: 6a707cc ...
HEAD is now at 6a707cc ...
$ git pull     # 从远程仓库更新
Updating 6a707cc..f93575d
... ...
$ git stash pop   # 将自己的修改合并到更新后的代码中

最后一步如果有冲突, 再参照上一节中解决冲突的步骤, 用文本编辑器修改冲突文件.


git 分支合并时的冲突

正在开发的分支和主分支的编辑了同一个文件时, 在主分支上进行 merge 的时候可能会产生冲突.

以下构造一个冲突的示例:
$ git branch test  # 创建一个分支 test, 但是没有进入test分支, 此时还在 master 分支上.
$ vim xxxx         # 编辑 master 分支上的一个已有的文件
$ git commit -am 'xxx message'  # 提交 master 分支的修改
$ git checkout test    # 切换到 test 分支
$ vim xxxx             # 编辑之前在 master 上编辑的文件, 可以编辑同一个地方, 造成冲突
$ git commit -am 'xxx message'  # 提交 test 分支的修改
$ git checkout master           # 切换到 master 分支
$ git merge test                # 将 test 分支合并到 master 分支, 由于上面编辑了同一文件, 这里会产生冲突
Auto-merging xxxx
CONFLICT (content): Merge conflict in xxxx
Automatic merge failed; fix conflicts and then commit the result.

最后, 参照上一节中解决冲突的步骤, 用文本编辑器修改冲突文件.


通过 git 提取补丁

提取的补丁的方法有多种:
$ git format-patch -1     # 提取本次 commit 和上次 commit 之间的不同, 并生成patch文件
$ git format-patch -2     # 提取本次 commit 和 上上次 commit 之间的不同, 并生成patch文件
$ git format-patch commit号1 commit号2  # 提取2次commit号之间的不同, 并生成patch文件 (commit号可以通过 git log 来查看)
$ git format-patch tag1 tag2            # 提取2次tag之间的不同, 并生成patch文件 (tag可以通过 git tag 来查看)


通过 git 提取指定版本的源码

这个功能在部署的时候比较有用.
$ git archive --format=tar --prefix="tagxx/" tagxx > ../tagxx.tar  # 获取 tagxx 的源码, 加了 --prefix 的作用是在最终的 tagxx.tar 中加了一层文件夹 tagxx

上面的 tagxx 也可以是 commit号
 

更多:[url=http://angryqueens.com/]http://angryqueens.com/" target=_blank>
ex. git remote add mc git://www.host.com/gitdir/mycode.git[/code]

修改远程仓库
git remote rename [oldnanme] [newname]


删除远程仓库
git remote rm [remote-name]


远程仓库的数据

5.1 获取数据
git fetch [remote-name] (获取仓库的所有更新,但是不自动合并当前分支)
git pull (获取仓库的所有更新, 并且自动合并到当前分支)

5.2 上传数据
git push [remote-name] [branch-name]
ex. git push origin master



git 标签

列出标签

1.1 查看所有tag
git tag

1.2 查看某个tag
git show [tag-name]


新建标签

2.1 轻量级tag
git tag [tag-name]

2.2 带标注的tag
git tag -a [tag-name] -m "tag message"

2.3 后期追加tag
git log --pretty=oneline (查看所有的commit号)
git tag -a [tag-name] [commit号前几位即可]


删除标签
git tag -d [tag-name]


提交标签到远程仓库
git push [remote-name] --tags
ex. git push origin --tags



git 分支

查看和切换分支
git branch (查看所有的分支及当前处于哪个分支)
git branch -v (查看所有的分支的详细信息)
git branch --merged (查看已经合并的分支)
git branch --no-merged (查看还没合并的分支)
git checkout [branch-name] (切换到某个分支)


新建分支
git branch [branch-name]  (新建一个分支)
git branch -b [branch-name] (新建一个分支并切换到这个分支上)


合并分支
git merge [branch-name]
ex. 将分支btest合并到主分支master
git checkout master
git merge btest

merge时有冲突的文件会列出来,需要手动合并
将冲突手动解决后,再次用 git status来查看是否还有 unmerged的文件。

如果没有冲突的文件,就可以 git commit 来提交这次合并了。

删除分支
git branch -d [branch-name]
或者 git branch -D [branch-name] (强制删除某个还未合并的分支)


远程分支相关

5.1 新建远程分支
git checkout [local_branch] (首先进入想要上传的分支)

git remote add [remote_repo] [remote_branch]

(这里的[remote_branch]是远程分支的名字,一般和[local_branch]同名,

[remote_repo]是远程仓库的名字)

2 向远程分支推送数据
git push [remote_repo] [remote_branch]

3 删除远程分支
git push [remote_repo] :[remote_branch] (注意远程分支前有个":")

合并分支的另一个方法:衍和

衍和可以简化master上的提交记录,使得代码可以方便的回退,

但是在公共仓库上用衍和有一定的风险。

衍和我基本用不上,这里就不赘述了。


服务器创建 git 仓库, 并将其作为远程仓库

其实 git 是分布式的 SCM. 并不存在谁是服务器, 谁是客户端的问题, 这里所说的服务器上的git仓库, 指的是多人合作开发时, 共用的, 作为最终发布版本的 git 仓库.

这个 git 仓库就相当于你在 github 上建的仓库, 会将你在各个电脑上做的代码等提交到上面进行统一管理.

服务端 (远程 git 仓库)

生成用于git服务的账户 (一般就用git)
groupadd gpxxx
useradd -m -g gpxxx gitxxx


初始化服务端的git 仓库
cd ~/
mkdir git-repo
cd git-repo
mkdir test.git
cd test.git
git --bare init


客户端 (本地 git 仓库)

新建本地git 仓库
cd ~/gitlocal
mkdir test
cd test
git init


初始化本地仓库
touch README
git add README
git commit -m 'first commit for init'


设置git用户信息
git config --global user.name "wangyubin"
git config --global user.email "xxx@xxx.com"


关联远程仓库
git remote add origin gituser@<server address>:~/test.git/


将本地仓库提交到远程
git push origin master



git 使用中遇到的一些问题


git pull 时, 远程文件与本地文件有冲突

如果远程的仓库被其他人更新了, 并且更新的内容与我自己本地编辑的内容有冲突. 这时执行 git pull 可能有如下message:
Auto-merging path/to/conflict-file
CONFLICT (content): Merge conflict in path/to/conflict-file
Automatic merge failed; fix conflicts and then commit the result.

用文本编辑器 vim 或者 emacs 之类的来编辑冲突的文件 path/to/conflict-file, 冲突的地方有类似如下的显示
<<<<<<< HEAD
App_Log.logger.debug(u'开始时间: ' + utils.datetime2str(datetime.datetime.now()))
file = request.FILES.get('file-xxx')
App_Log.logger.debug(u'结束时间: ' + utils.datetime2str(datetime.datetime.now()))

=======
file = request.FILES.get('xxxx')
>>>>>>> 3602514cc2bf1b3a64470b31ad79e07fe372add5

=====
 之上的
<<<<<<< HEAD 是本地的内容
=====
 之下的 >>>>>>>
3602514cc2bf1b3a64470b31ad79e07fe372add5 是远程的内容(这个commit号每次都会不同)

根据实际情况, 删除多余的内容(包括===== >>>>> <<<<<< 之类的), 修改冲突的地方, 如果以本地的代码为准的话, 会得到如下结果:
App_Log.logger.debug(u'开始时间: ' + utils.datetime2str(datetime.datetime.now()))
file = request.FILES.get('file-xxx')
App_Log.logger.debug(u'结束时间: ' + utils.datetime2str(datetime.datetime.now()))

然后 git commit -am '提交的信息' 就解决了冲突.

最后, 也可以将本地的修改同步到远程 git 仓库: git push


git pull 时, 本地还有未commit 的文件

从远程仓库更新时, 假使本地还有没commit的文件A, 远程仓库的A文件却被修改了. 此时进行 git pull 时有如下信息:
6a707cc..f93575d  master     -> origin/master
Updating 6a707cc..f93575d
error: Your local changes to the following files would be overwritten by merge:
apps/myapp/utils.py
Please, commit your changes or stash them before you can merge.
Aborting

此时, 如果不想将本地文件commit(可能只是临时的修改), 但是又像将远程的仓库更新下来, 可以这样:
$ git stash    # 先将自己的改变保存起来
Saved working directory and index state WIP on master: 6a707cc ...
HEAD is now at 6a707cc ...
$ git pull     # 从远程仓库更新
Updating 6a707cc..f93575d
... ...
$ git stash pop   # 将自己的修改合并到更新后的代码中

最后一步如果有冲突, 再参照上一节中解决冲突的步骤, 用文本编辑器修改冲突文件.


git 分支合并时的冲突

正在开发的分支和主分支的编辑了同一个文件时, 在主分支上进行 merge 的时候可能会产生冲突.

以下构造一个冲突的示例:
$ git branch test  # 创建一个分支 test, 但是没有进入test分支, 此时还在 master 分支上.
$ vim xxxx         # 编辑 master 分支上的一个已有的文件
$ git commit -am 'xxx message'  # 提交 master 分支的修改
$ git checkout test    # 切换到 test 分支
$ vim xxxx             # 编辑之前在 master 上编辑的文件, 可以编辑同一个地方, 造成冲突
$ git commit -am 'xxx message'  # 提交 test 分支的修改
$ git checkout master           # 切换到 master 分支
$ git merge test                # 将 test 分支合并到 master 分支, 由于上面编辑了同一文件, 这里会产生冲突
Auto-merging xxxx
CONFLICT (content): Merge conflict in xxxx
Automatic merge failed; fix conflicts and then commit the result.

最后, 参照上一节中解决冲突的步骤, 用文本编辑器修改冲突文件.


通过 git 提取补丁

提取的补丁的方法有多种:
$ git format-patch -1     # 提取本次 commit 和上次 commit 之间的不同, 并生成patch文件
$ git format-patch -2     # 提取本次 commit 和 上上次 commit 之间的不同, 并生成patch文件
$ git format-patch commit号1 commit号2  # 提取2次commit号之间的不同, 并生成patch文件 (commit号可以通过 git log 来查看)
$ git format-patch tag1 tag2            # 提取2次tag之间的不同, 并生成patch文件 (tag可以通过 git tag 来查看)


通过 git 提取指定版本的源码

这个功能在部署的时候比较有用.
$ git archive --format=tar --prefix="tagxx/" tagxx > ../tagxx.tar  # 获取 tagxx 的源码, 加了 --prefix 的作用是在最终的 tagxx.tar 中加了一层文件夹 tagxx

上面的 tagxx 也可以是 commit号
 

更多:[url=http://angryqueens.com/]http://angryqueens.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git 文档 管理 工作 github