您的位置:首页 > 其它

git使用操作

2015-09-18 00:13 411 查看
  在我们用git来玩github,可能我们是这样操作的:

  1. 回到github首页,点击页面右下角“New Repository”

  填写项目信息:

  project name: hello world

  description : my first project

  点击“Create Repository” ; 现在完成了一个项目在github上的创建。

  2. 我们需要使用git在本地创建一个相同的项目。

  设置提交的记录用户

git config --global user.name "用户名"
git config --global user.email "邮箱"
git config --list  //查看


  git创建项目

复制代码
$ makdir ~/hello-world    //创建一个项目hello-world
$ cd ~/hello-world    //打开这个项目
$ git init    //初始化
$ touch README  //简历README.md
$ git add README   //更新README文件
$ git commit -m 'first commit'//提交更新,并注释信息“first commit”
$ git remote add origin git@github.com:defnngj/hello-world.git   //连接远程github项目
$ git push -u origin master   //将本地项目更新到github项目上去
复制代码


  现在查看github上面的hello world 项目,是不是发现已经将本地中的README文件更新上来了。 :) 下面我们来了解一下git的玩法

一、git常见使用情况

  1.新项目

  创建 git 仓库:

git init //创建新项目
git add .
git commit -m "first commit"
git remote add origin https://git.oschina.net/sunzmit/test11.git git push -u origin master


  2.已有项目

git remote add origin https://git.oschina.net/sunzmit/test11.git git push -u origin master


二、git基本用法

  1.创建新仓库

git init      //创建新文件夹,打开,然后执行 git init以创建新的 git 仓库。


  2.检出仓库

执行如下命令以创建一个本地仓库的克隆版本:
git clone /path/to/repository
如果是远端服务器上的仓库,你的命令会是这个样子:
git clone username@host:/path/to/repository


  3.git config --list查看

git branch //查看当前分支
git branch -r //列出远程分支
git branch -a //列出所有分支

git branch branchName //创建分支
git checkout branchName //切换分支
git checkout -b branchName //创建并切换到分支

git checkout  //后面不跟任何参数,则就是对工作区进行检查
git checkout --filename //从暂存区中恢复文件(确保filename与branch名称不同)

git status //查看状态


  4.git clone 和 git remote

git clone <版本库的网址> <本地目录名>
git clone支持多种协议,除了HTTP(s)以外,还支持SSH、Git、本地文件协议等,下面是一些例子。

$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

SSH协议还有另一种写法
$ git clone [user@]example.com:path/to/repo.git/

=========================================

git remote
git remote -v  //查看远程主机的网址
git remote show <主机名> //查看该主机的详细信息
git remote add <主机名> <网址> //添加远程主机
git remote rm <主机名>  //删除远程主机
git remote rename <原主机名> <新主机名> //重命名远程主机


  5.git pull 和 git push

$ git pull <远程主机名> <远程分支名>:<本地分支名>
$ git push <远程主机名> <本地分支名>:<远程分支名>
from         to

git pull origin master:master
取回origin主机的master分支,与本地的master分支合并

git push origin master:master
推送本地的master分支,与origin主机的master分支合并

git pull origin master
如果远程分支是与当前分支合并,则冒号后面的部分可以省略。

git push origin master
本地的master分支推送到origin主机的master分支。如果后者不存在,则会被新建

git pull origin
本地的当前分支自动与对应的origin主机”追踪分支”(remote-tracking branch)进行合并。
追踪分支 是 远程的同名分支

git push origin
当前分支与远程分支之间存在追踪关系,则本地分支和远程分支都可以省略

git pull
当前分支自动与唯一一个追踪分支进行合并

git push
当前分支只有一个追踪分支,那么主机名都可以省略


  6.git merge 和 git rebase

git merge
用"pull"命令把"origin"分支上的修改拉下来并且和你的修改合并;
结果看起来就像一个新的"合并的提交"(merge commit):

//使用 rebase 合并
$ git checkout mywork
$ git rebase origin
这些命令会把你的"mywork"分支里的每个提交(commit)取消掉,
并且把它们临时 保存为补丁(patch)(这些补丁放到".git/rebase"目录中),
然后把"mywork"分支更新 到最新的"origin"分支,
最后把保存的这些补丁应用到"mywork"分支上

在rebase的过程中,也许会出现冲突(conflict). 在这种情况,
Git会停止rebase并会让你去解决 冲突;在解决完冲突后,
用"git-add"命令去更新这些内容的索引(index), 然后,你无需执行 git-commit,只要执行:

$ git rebase --continue
这样git会继续应用(apply)余下的补丁。

在任何时候,你可以用--abort参数来终止rebase的行动,并且"mywork" 分支会回到rebase开始前的状态。
$ git rebase --abort


  7.git remote 查看当前的远程库

$ git remote -v
bakkdoor  git://github.com/bakkdoor/grit.git
cho45     git://github.com/cho45/grit.git
defunkt   git://github.com/defunkt/grit.git
koke      git://github.com/koke/grit.git
origin    git@github.com:mojombo/grit.git


  8.git status 检查当前文件状态

   要确定哪些文件当前处于什么状态,可以用
git status
命令

$ vim README
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

README

nothing added to commit but untracked files present (use "git add" to track)


  9.git rm 删除从文件缓存区、本地目录中移除

git rm file
将file从文件缓存区、本地目录中移除

git rm file --cached
只从缓存区移除,保存本地目录中的

git rm -r folder 删除文件夹


三、git问题解决QA

  1.但是在提交代码以后发现有冲突,提示如下:    

error: failed to push some refs to ‘git@git.oschina.net:bruin/post.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.


  原因是没有将远程库pull到本地

  $git pull https://git.oschina.net/bruin/post.git
  然后再push到远程库

  $git push https://git.oschina.net/bruin/post.git
  冲突解决!

 

  2.但是在提交代码以后发现有冲突,提示如下:   

git: 'credential-osxkeychain' is not a git command. See 'git --help'.
git: 'credential-osxkeychain' is not a git command. See 'git --help'.
To https://username@bitbucket.org/username/data.git ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://username@bitbucket.org/username/data.git'
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Merge the remote changes (e.g. 'git pull') before pushing again. See


  $git pull --rebase origin master 合并

  3.本地目录手动删除(未采用git rm),commit导致远程仓库与本地仓库不一致问题(本地删除没有提交导致)

  解决方案:

git status  //检查当前文件状态
git rm -r floder  //删除手动删除,状态里面显示的文件


  4.本地拷贝工作区缓存

git checkout file


  5.忽略某些文件

  项目中经常会生成一些Git系统不需要追踪(track)的文件。典型的是在编译生成过程中 产生的文件或是编程器生成的临时备份文件。当然,你不追踪(track)这些文件,可以 平时不用"git add"去把它们加到索引中。 但是这样会很快变成一件烦人的事,你发现 项目中到处有未追踪(untracked)的文件; 这样也使"git add ." 和"git commit -a" 变得实际上没有用处,同时"git status"命令的输出也会有它们。

  你可以在你的顶层工作目录中添加一个叫".gitignore"的文件,来告诉Git系统要忽略 掉哪些文件,下面是文件内容的示例:

# 以'#' 开始的行,被视为注释.
# 忽略掉所有文件名是 foo.txt 的文件.
foo.txt
# 忽略所有生成的 html 文件,
*.html
# foo.html是手工维护的,所以例外.
!foo.html
#  忽略所有.o 和 .a文件.
*.[oa]


  我们通过 git bash工具 touch .gitignore 建立忽略文件。

  demo例子:

.DS_Store
Thumbs.db
db.json
*.log
# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
#public/
.deploy*/
# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules
public
.o2


  

  

参考资料:

  git - 简明指南

  window上使用GIT的个人经验(入门级)

  http://blog.csdn.net/fyh2003/article/details/6869804

  git心得 :http://segmentfault.com/a/1190000002896037

  git学习总结

  markdown语法

  git-u参数的理解

  git经典教程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: