1. 配置ssh

    • 打开命令行或者git bash
    • 输入
  2. 创建git库

    git init
  • 检查当前状态
    $ git status
    On branch master
    nothing to commit, working directory clean

working directory clean表示当前工作目录没有未被追踪或者已修改的文件,否则会将它们列出。这个命令会告诉我们当前在哪个分支。

在当前目录会生成一个.git文件夹

  1. 远程仓库
  • 添加远程仓库

    在GitHub或者码云上创建好仓库后,复制远程仓库的HTTPS地址或ssh地址,在本地文件夹输入:

    git remote add origin <HTTPS地址或ssh地址>

    查看远程库信息:

    git remote -v
  • 从远程仓库克隆

    git clone <HTTPS地址或ssh地址>
  • 推送修改到远程仓库

    • 暂存已修改文件:
      git  add <./特定文件名>

    . 表示当前目录,会将当前目录下修改的所有文件暂存起来;特定文件名则表示将特定文件暂存。

    • 检查已暂存以及尚未暂存的更改
      git diff

    该命令会对比目前工作目录(working directory)与暂存区域(stage area)的版本,然后显示尚未暂存的文件的变更

      git diff --cached

    上一个命令如果已经暂存了所有更改的话,就什么都不显示。而该命令可以显示已暂存的更改与最后一次commit的差异。

  • 将远程修改加载到本地仓库

    git commit -m <提交信息>
    • 跳过暂存环节,直接提交
      git commit -a -m <提交信息>
  1. 分支管理
  • 查看分支

    查看本地分支:

    git branch

    查看所有分支:

    git branch -a
  • 创建分支

    git branch <分支名>
  • 切换分支

    git checkout <分支名>

    创建并切换分支:

    git checkout -b <分支名>

    切换远程分支:

    git checkout -b <分支名> origin/<分支名>

    上面的命令作用是获取远程的分支,在本地命名并切换。

  • 合并分支

    如果要将dev分支的内容合并到master:

    git checkout master
    git merge dev

    上面的命令会丢掉分支信息,可以使用:

    git merge --no-ff dev

    它会禁用fast forward,使得日志中显示分支的merge信息。

    合并部分分支:

    git cherry-pick <commit 号>

    合并多个commit(用空格指定多个commit):

    git cherry-pick A B C D E F

    指定commit范围合并(两次版本间使用..连起来):

    git cherry-pick A..B

    这样可以从版本A(不包含)到B(包含)的版本pull到当前分支

    可以多段合并,同样用空格隔开:

    git cherry-pick A..B C..D E..F

    合并远程分支:

  • 删除分支

    git branch -d <要删除的分支名>

    强制删除:

    git branch -D <要删除的分支名>

    删除远程分支:

    git push origin :<branchName>
  • 解决冲突

    有时候提交、pull以及合并的时候会出现以下情况:

    $ git merge dev
    Auto-merging readme.md
    CONFLICT (content): Merge conflict in readme.md
    Automatic merge failed; fix conflicts and then commit the result.

    上面的信息中会标出冲突文件,打开文件会发现,冲突的地方会分别有<<<<<,======,>>>>>标记出不同分支的内容,需要手动修改这些地方,留下你觉得正确需要的修改,其它删除掉,<<<<<表示的是当前分支的内容,>>>>>表示的是要合并过来的分支的内容。

    修改完后在提交就可以了。

  • stash 操作

    stash操作可以将当前尚不需要暂存的修改隐藏起来:

    $ git stash
    Saved working directory and index state WIP on dev: cb5a443 modify the file
    HEAD is now at cb5a443 modify the file
    $ git status
    On branch dev
    nothing to commit, working directory clean

    列出隐藏起来的工作现场:

    $ git stash list
    stash@{0}: WIP on dev: cb5a443 modify the file

    恢复隐藏的工作现场

    $ git stash pop
    On branch dev
    Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory) modified: readme.md no changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{0} (5e674302787482fbdd0c3e6fb455a5aca102a9d2)

    pop命令在恢复的同时会将栈顶的stash的内容删除,调用git stash apply恢复后,stash内容并不会删除,需要调用git stash drop命令才能删除

    多次stash的话,可以先调用git stash list查看stash列表,然后调用git stash apply stash@{},这里的num指的是在stash栈中的index。

  • 推送修改到远程分支

    git add .
    git commit -m "..."
    git push origin <branchName>
  • 获取远程修改的几种方式

    git pull

    这个操作包含了fetch和merge,会创建多个临时分支,不是很好,可以这样:

    git pull --rebase

    当出现冲突时会:

    $ git pull --rebase origin dev
    From https://github.com/liberty2015/gitdemo
    * branch dev -> FETCH_HEAD
    First, rewinding head to replay your work on top of it...
    Applying: update the file
    Using index info to reconstruct a base tree...
    M readme.md
    Falling back to patching base and 3-way merge...
    Auto-merging readme.md
    CONFLICT (content): Merge conflict in readme.md
    error: Failed to merge in the changes.
    Patch failed at 0001 update the file
    The copy of the patch that failed is found in: .git/rebase-apply/patch When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

    当解决完冲突后:

    git rebase --continue

    或者跳过:

    git rebase --skip
  1. 标签管理

  2. 日志查询

    git log -<num>

    查询日志,num表示查询日志数量

    图形工具查询方式:

    gitk

参考网址:

https://ihower.tw/blog/archives/3843

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c74be33fbd884e71b570f2cc3c0d1dcf000

http://hungyuhei.github.io/2012/08/07/better-git-commit-graph-using-pull---rebase-and-merge---no-ff.html

https://kingofamani.gitbooks.io/git-teach/content/chapter_2/repo.html

git 基础学习笔记的更多相关文章

  1. 【C#编程基础学习笔记】4---Convert类型转换

    2013/7/24 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]4---Convert类型转换 ...

  2. 【C#编程基础学习笔记】6---变量的命名

    2013/7/24 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]6---变量的命名 ----- ...

  3. 1.C#基础学习笔记3---C#字符串(转义符和内存存储无关)

    技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com ------------------------------------- ...

  4. Java基础学习笔记总结

    Java基础学习笔记一 Java介绍 Java基础学习笔记二 Java基础语法之变量.数据类型 Java基础学习笔记三 Java基础语法之流程控制语句.循环 Java基础学习笔记四 Java基础语法之 ...

  5. git的学习笔记(二):git远程操作

    1.创建ssh key ssh-keygen -t rsa -C "your_email@example.com" 执行命令后会在用户的家目录生成.ssh的隐藏文件夹,文件夹里有公 ...

  6. Mysql数据库基础学习笔记

    Mysql数据库基础学习笔记 1.mysql查看当前登录的账户名以及数据库 一.单表查询 1.创建数据库yuzly,创建表fruits 创建表 ) ) ,) NOT NULL,PRIMARY KEY( ...

  7. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  8. Java基础学习笔记(一)

    Java基础学习笔记(一) Hello World 基础代码学习 代码编写基础结构 class :类,一个类即一个java代码,形成一个class文件,写于每个代码的前端(注意无大写字母) XxxYy ...

  9. C#RabbitMQ基础学习笔记

    RabbitMQ基础学习笔记(C#代码示例) 一.定义: MQ是MessageQueue,消息队列的简称(是流行的开源消息队列系统,利用erlang语言开发).MQ是一种应用程序对应用程序的通信方法. ...

随机推荐

  1. 【译】3 ways to define a JavaScript class

    本文真没啥难点,我就是为了检验我英语水平退化了没哈哈虽然我英语本来就渣翻译起来也像大白话.将原文看了一遍也码完翻译了一遍差不多一个小时,其中批注部分是自己的理解如有疏漏或误解还请指出感激不尽呐,比如J ...

  2. WebDriver API 实例详解(一)

    一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...

  3. Singapore retailer will release this adidas NMD R1

    Select spots are restocking the adidas NMD Singapore this Friday, Feb 24th featuring three different ...

  4. Polya

    using namespace std; typedef long long LL; const int MAXN = 1e3 +10; const LL MOD = (LL)1 << 6 ...

  5. SecureCRT 会话空闲时超时退出处理

    参考文章:http://www.cnblogs.com/xuxm2007/archive/2011/04/21/2023611.html http://yunwei.blog.51cto.com/38 ...

  6. Spring整合Quartz定时发送邮件

    功能描述:刚开始接触Quartz,试着用Quartz整合spring实现每隔一分钟发送一封邮件连续发送10次 核心jar: 邮件发送:commons-email-1.2.jar mail.jar(必须 ...

  7. inline用法详解

    (一)inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把min()指定为内联. inline int min(int first, int ...

  8. poj_3071 Football(概率dp)

    直接上状态转移方程: 记dp[i][j]为第i轮比赛,第j个队伍获胜的概率. 那么初始状态下,dp[0][j]=1://也就是第0轮比赛全都获胜 d[i][j]=sum(d[i-1][j]*d[i-1 ...

  9. 重器--biomart

    biomart 重器 biomaRt工具包的作用在于它可以轻松地完成的在多个生物学数据库上繁琐地检索,获取相关数据在不同数据库间的关联.

  10. 51nod 1225 余数的和 数学

    1225 余数之和 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 F(n) = (n % 1) + (n % 2) + (n % 3) + ... ...