git使用操作

  在我们用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 [email protected]: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 [email protected]:/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 [[email protected]]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

三、git问题解决QA

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

error: failed to push some refs to ‘[email protected]: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://[email protected]/username/data.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to ‘https://[email protected]/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  合并

 

  

参考资料:

  git - 简明指南

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

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

时间: 2024-08-06 03:44:00

git使用操作的相关文章

Git远程操作详解

Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Git远程操作. git clone git remote git fetch git pull git push 本文针对初级用户,从最简单的讲起,但是需要读者对Git的基本用法有所了解.同时,本文覆盖了上面5个命令的几乎所有的常用用法,所以对于熟练用户也有参考价值. 一.git clone 远程操作

Git大法好——2.Git本地操作指令详解

Git大法好--2.Git本地操作指令详解 引言 上节给大家讲解了有关于Git的一些概念,Git的引入,Git的四个组成部分,Git文件的状态,以及 Git的下载安装:前面也讲过Git和SVN有个明显的差别就是,Git可以不需要网络就可以进行版本 控制,这是因为Git中每个电脑都拥有一个本地的版本库,而远程的仓库仅仅是作为我们交换修改 的一个工具!即使失去这个工具,我们也可以干活,只是交换修改不方便罢了,假如是SVN,远程 服务器挂了-所以,我们使用Git的时候大部分时间都是在进行Git的一些本

git 入门操作指令

git add [commit file list] 将已经修改的代码添加索引 git commit -m [commit message] 将已经索引的代码修改提交至本地的库 git push origin [branch_name] 将分支 branch_name 的代码推送至服务器 git pull origin [branch_name] 获取分支 branch_name上的最新代码 git pull --rebase origin [branch_name] 将当前 branch 从

Git 更新操作

修改现有函数 Tom 执行克隆操作后,看到新的文件string.c,他想知道这个文件到存储库?目的是什么?于是,他执行 git 日志命令. [[email protected] ~]$ git clone [email protected]:project.git 上面的命令会产生以下结果. Initialized empty Git repository in /home/tom/project/.git/ remote: Counting objects: 6, done. remote:

git 常用操作集锦

创建仓库 新建普通仓库: [email protected]:~/workspace/git$ git init Reinitialized existing Git repository in /home/jxdong/workspace/git/.git/ 新建 bare 仓库: [email protected]:~/workspace/git.git$ git init --bare Initialized empty Git repository in /home/jxdong/wor

【Git】Git远程操作详解

Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Git远程操作. git clone git remote git fetch git pull git push 本文针对初级用户,从最简单的讲起,但是需要读者对Git的基本用法有所了解.同时,本文覆盖了上面5个命令的几乎所有的常用用法,所以对于熟练用户也有参考价值. 一.git clone 远程操作

Git远程操作(附重要原理图)

原文出处: 阮一峰 Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Git远程操作. git clone git remote git fetch git pull git push 本文针对初级用户,从最简单的讲起,但是需要读者对Git的基本用法有所了解.同时,本文覆盖了上面5个命令的几乎所有的常用用法,所以对于熟练用户也有参考价值. 一.git

git报错:&#39;fatal:remote origin already exists&#39;怎么处理?附上git常用操作以及说明。

git添加远程库的时候有可能出现如下的错误, 怎么解决? 只要两步: 1.先删除 $ git remote rm origin 2.再次执行添加就可以了. ----------------------------------------------git常用操作------------------------------------------------ 说明,以下整理来自廖雪峰大神的<git教程>. 各位童鞋要下载git但是网速不给力的,可以从这里下载:https://pan.baidu.

Git基础操作

配置秘钥 1.检查本机有没有秘钥 检查~/.ssh看看是否有名为d_rsa.pub和id_dsa.pub的2个文件. $ ~/.sshbash: /c/Users/lenovo/.ssh: Is a directory 2.如果没有就创建新的秘钥 $ ssh-keygen -t rsa -C "注册Github用的邮箱" $ ssh-keygen -t rsa -C "注册Github用的邮箱" 按照提示一路输入即可. ssh-keygen -t rsa -C &q

Git远程操作

Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Git远程操作. git clone git remote git fetch git pull git push 本文针对初级用户,从最简单的讲起,但是需要读者对Git的基本用法有所了解.同时,本文覆盖了上面5个命令的几乎所有的常用用法,所以对于熟练用户也有参考价值. 一.git clone 远程操作的第一步,通常是从远程主机克隆一个版本库,这时就要用到git clone