傻瓜入门: step by step : https://try.github.io/levels/1/challenges/1

一本书: https://git-scm.com/book/en/v2

竟然有中文版 O.O: https://git-scm.com/book/zh/v2/

git实在是太复杂了,学呀学呀学不懂。只能一点点记下来,省得每次想好久。。。。

0. 新环境里的第一次配置:

[root@D128 Src]# git config --global user.name "Cao Tong"
[root@D128 Src]# git config --global user.email "tong.caotong@gmail.com"

0.1  generate ssh key

[root@D128 Src]# ssh-keygen 

0.2  add your pub key to your ssh repo server, like github

0.3  下载代码

[root@D128 Src]# git clone git@github.com:intel/hyperscan.git

1. 丢弃所有修改

  两个命令一起用。

/home/tong/Src/copyleft/libdssl [git::master *] [tong@T7] [:]
> sudo git clean -xdf
/home/tong/Src/copyleft/libdssl [git::master *] [tong@T7] [:]
> git reset --hard

2.  删除一个本地分支:

  git branch -d <branch-name>

/home/tong/Src/thirdparty/github/msgpack-c [git::master] [tong@T7] [:]
> git branch -l
list
* master /home/tong/Src/thirdparty/github/msgpack-c [git::master] [tong@T7] [:]
> git branch -d list
Deleted branch list (was c50fbe4c). /home/tong/Src/thirdparty/github/msgpack-c [git::master] [tong@T7] [:]
> git branch -l
* master

3.  创建git server

  在CentOS 7 环境下:

  参考: https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%8D%8F%E8%AE%AE

  3.1  安装:

[root@S205 log]# yum install git

  3.2  初始化仓库

[root@S205 log]# su - git
Last login: Mon Jun :: CST on pts/
[git@S205 ~]$ mkdir anthropoid.git
[git@S205 ~]$ cd anthropoid.git/
[git@S205 anthropoid.git]$ git init --bare
Initialized empty Git repository in /home/git/anthropoid.git/
[git@S205 anthropoid.git]$ ll
total
drwxrwxr-x git git Jun : branches
-rw-rw-r-- git git Jun : config
-rw-rw-r-- git git Jun : description
-rw-rw-r-- git git Jun : HEAD
drwxrwxr-x git git Jun : hooks
drwxrwxr-x git git Jun : info
drwxrwxr-x git git Jun : objects
drwxrwxr-x git git Jun : refs

  3.3  增加公钥,授予权限

  3.4  设置git shell

[root@S205 ~]# which git-shell >> /etc/shells
[root@S205 ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
/usr/bin/git-shell
[root@S205 ~]# chsh git
Changing shell for git.
New shell [/bin/bash]: /usr/bin/git-shell
Shell changed.
[root@S205 ~]# su - git
Last login: Mon Jun :: CST from 192.168.50.20 on pts/
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
[root@S205 ~]#

  3.5  在原项目里创建新分支

/home/tong/Src/copyleft/potatos [git::master *] [tong@T7] [:]
> git branch anthropoid /home/tong/Src/copyleft/potatos [git::master *] [tong@T7] [:]
> git branch
anthropoid
hally
* master /home/tong/Src/copyleft/potatos [git::master *] [tong@T7] [:]
> git checkout anthropoid
Switched to branch 'anthropoid' /home/tong/Src/copyleft/potatos [git::anthropoid *] [tong@T7] [:]

   3.6  push第一个版本

/home/tong/Src/copyleft/potatos [git::master *] [tong@T7] [:]
> git push origin anthropoid:master
X11 forwarding request failed on channel
Counting objects: , done.
Delta compression using up to threads.
Compressing objects: % (/), done.
Writing objects: % (/), 34.86 KiB | bytes/s, done.
Total (delta ), reused (delta )
To s205:/home/git/anthropoid.git
* [new branch] anthropoid -> master

   3.7  从新仓库clone

/home/tong/Src/copyright/onescorpion [tong@T7] [:]
> git clone git@s205:/home/git/anthropoid.git
Cloning into 'anthropoid'...
X11 forwarding request failed on channel
remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta )
Receiving objects: % (/), 34.86 KiB | bytes/s, done.
Resolving deltas: % (/), done.

4.  使本地仓库的仓库内容与某标签一致。

检出标签
在 Git 中你并不能真的检出一个标签,因为它们并不能像分支一样来回移动。 如果你想要工作目录与仓库中特定的标签版本完全一样,
可以使用 git checkout -b [branchname] [tagname] 在特定的标签上创建一个新分支: $ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'
当然,如果在这之后又进行了一次提交,version2 分支会因为改动向前移动了,那么 version2 分支就会和 v2.0.0 标签稍微有些不同,这时就应该当心了。

参考: https://git-scm.com/book/zh/v2/Git-%E5%9F%BA%E7%A1%80-%E6%89%93%E6%A0%87%E7%AD%BE

5.  让文件恢复到上一次提交时的样子

git checkout -- [file] 

6.  查看/获取远程分支

> git branch -a
> git checkout -b frame_feature origin/frame_feature

7.   export 导出

https://git-scm.com/docs/git-archive

https://stackoverflow.com/questions/160608/do-a-git-export-like-svn-export

好像是不行的。有人说用rsync:

rsync -a ./FROM/ ./TO --exclude='.*'
/home/tong/Src/thirdparty/pktgen-dpdk [git::pktgen-3.3.] [tong@T7] [:]
> git archive --format=tar.gz -o pktgen-3.3..tar.gz HEAD

8.  git add 空文件夹

  git add 不能添加空文件夹。没有这个功能。 一般是在空文件夹下面放一个文件 .gitkeep

https://git.wiki.kernel.org/index.php/GitFaq#Can_I_add_empty_directories.3F

9.  git 重命名仓库

  9.1

[root@S205 git]# mv anthropoid.git/ crisp.git

  9.2

/home/tong/Src/copyright/onescorpion [tong@T7] [:]
> mv anthropoid crisp

  9.3

/home/tong/Src/copyright/onescorpion/crisp [git::develop *] [tong@T7] [:]
> git remote -v
origin git@s205:/home/git/anthropoid.git (fetch)
origin git@s205:/home/git/anthropoid.git (push) /home/tong/Src/copyright/onescorpion/crisp [git::develop *] [tong@T7] [:]
> git remote set-url origin git@s205:/home/git/crisp.git /home/tong/Src/copyright/onescorpion/crisp [git::develop *] [tong@T7] [:]
> git remote -v
origin git@s205:/home/git/crisp.git (fetch)
origin git@s205:/home/git/crisp.git (push) /home/tong/Src/copyright/onescorpion/crisp [git::develop *] [tong@T7] [:]
>

10. 分支合并指定文件 / 从其他分支提取指定文件

git checkout source_branch <paths>...

http://ryanhoo.github.io/blog/2014/07/11/checkout-files-from-another-branch/

http://jasonrudolph.com/blog/2009/02/25/git-tip-how-to-merge-specific-files-from-another-branch/

无历史信息!

11.  clone时,指定具体的tag.

git clone --branch v1. git@github.com:hyperrealm/libconfig.git libconfig-v1.

glibc下载代码的指引, 不知道是不是相同的需求:

git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout --track -b local_glibc-2.26 origin/release/2.26/master

https://www.gnu.org/software/libc/sources.html

12. 重写最近一次提交的日志

[::] tong@T7 /home/tong/Src/copyright/ ()
> git commit --amend

13. git archive 的用法

https://www.kernel.org/pub/software/scm/git/docs/git-archive.html

https://stackoverflow.com/questions/5995301/how-do-i-pipe-a-git-clone-to-archive-tar-or-gzip

git archive --format=tar --remote=git@server:repo.git master | tar -xf -
git archive --format=tar.gz -o pktgen-3.3..tar.gz HEAD

14 git checkout 到指定版本

┬─[tong@T7:~/Src/]─[:: PM]
╰─>$ git checkout --detach 0e4dcf6cd69c2b9ca37bafd1a43dfede0f17542f
HEAD is now at 0e4dcf6... fix bug: msgpack soa dns rr
┬─[tong@T7:~/Src/]─[:: PM]
╰─>$ git branch
* (HEAD detached at 0e4dcf6)
master
┬─[tong@T7:~/Src/]─[:: PM]
╰─>$

15 git 从指定版本退回

┬─[tong@T7:~/Src/copyright/strongswan]─[05:46:41 PM] 
╰─>$ git revert 4e77d9939af9ac6f06b95516399ed302d0df10fe
┬─[tong@T7:~/Src/copyright/strongswan]─[05:46:41 PM]
╰─>$ git push

16 提交之后有一些内容忘记了提交

例如,你提交后发现忘记了暂存某些需要的修改,可以像下面这样操作:

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
最终你只会有一个提交 - 第二次提交将代替第一次提交的结果。

17 临时分支到某个tag

root@D128 ~/S/t/pktgen-dpdk.git# git checkout --detach pktgen-3.5.
HEAD is now at 46f697c... fix typo
root@D128 ~/S/t/pktgen-dpdk.git# git branch
* (detached from pktgen-3.5.)
master

18 清理工作目录

# git clean -fd

19  取出某个commit 作为patch,并patch到当前代码里

root@D128 ~/S/c/j/j# git format-patch - 9ca41ea4a56bfcab0fb6fcb6eea5ecb0a9e5103e
-nlb_sched.patch
root@D128 ~/S/c/j/j# git am -nlb_sched.patch
Applying: nlb_sched: . unbind scheduler while service is really freeing. . support release_later feature for 'scheduler data'.

am会增加commit log。只接受修改可使用apply

root@D128 ~/S/c/j/j# git apply -nlb_sched.patch

20  查看git目录下的全部不在仓库里的数据

git status --ignored -u

清除他们

git clean -x -f -d

21 push用法

$ git pull <远程主机名> <远程分支名>:<本地分支名>
$ git pull origin next:master

22 删除远端分支

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

git push origin :test_branch

 

[skill][git] git 常用操作记录的更多相关文章

  1. Git的常用命令记录

    Git的常用命令记录 1.与远程仓库建立连接,即关联一个远程库 git remote add origin git@server-name:path/repo-name.git; 2.查看当前分支  ...

  2. centos下升级git版本的操作记录

    在使用git pull.git push.git clone的时候,或者在使用jenkins发版的时候,可能会报类似如下的错误: error: The requested URL returned e ...

  3. centos6下升级git版本的操作记录

    编译go_ethereum的时候出现了错误 然后发现是自己的git没有升级成功  因为编译需要高版本的git版本  所以会编译不成功  之后执行 root@uatjenkins01 ~]# git - ...

  4. 转:centos下升级git版本的操作记录

    https://www.cnblogs.com/kevingrace/p/8252517.html 在使用git pull.git push.git clone的时候,或者在使用jenkins发版的时 ...

  5. 《Git的常用操作》

    Git的常用操作: git checkout -b 本地分支 #创建本地的分支—本地分支,并切换到该分支下. git branch --set-upstream-to=origin/远程分支 本地分支 ...

  6. git介绍-常用操作(一)

    Table of Contents 1  系列文章 2  git说明 3  git常用命令 3.1  基本操作 3.2  远程操作 4  查看git的配置 4.1  查看已配置项 4.2  其他配置 ...

  7. Hbase常用操作记录

    Hbase常用操作记录 Hbase 创建表 查看表结构 修改表结构 删除表 创建表 语法:create <table>, {NAME => <family>, VERSI ...

  8. jenkins中通过git发版操作记录

    之前说到的jenkins自动化构建发版是通过svn方式,今天这里介绍下通过git方式发本的操作记录. 一.不管是通过svn发版还是git发版,都要首先下载svn或git插件.登陆jenkins,依次点 ...

  9. Git的常用操作

    $ git log //查看commit记录 $ git add <file> //添加文件到commit中 .代表所有改动的文件 $ git commit -m 'meesage' // ...

随机推荐

  1. iOS中如何创建一个滑出式导航面板(1)

    本文将介绍如何创建类似Facebook和Path iOS程序中的滑出式导航面板. 向右滑动 滑出式设计模式可以让开发者在程序中添加常用的导航功能,又不会浪费屏幕上宝贵的空间.用户可以在任意时间滑出导航 ...

  2. (1) Mysql高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意 ...

  3. 【emWin】例程十一:GIF图像显示

    介绍: 本例程介绍gif格式图像显示的方法以及在GMT70,iCore3_ADP,7寸液晶模块.4.3寸液晶模块, VGA模块上的移植. 实验指导书及代码包下载: 链接:http://pan.baid ...

  4. 【WPF】自定义形状的按钮Button

    需求:做一个如下图所示的多边形按钮. <!-- 特殊形状的按钮 --> <Grid> <Polygon Points="0,0 140,0 190,42 140 ...

  5. Qt读写ini文件

    一 背景 1 ini文件介绍 .ini 文件是Initialization File的缩写,即初始化文件. 除了windows现在很多其他操作系统下面的应用软件也有.ini文件,用来配置应用软件以实现 ...

  6. 树莓派集群实践——nfs

    1.安装 apt-get install nfs-common nfs-kernel-server 省略(sudo apt-get install portmap  --->install rp ...

  7. Android短信监听实现,及Android4.4之后短信机制变更

    前阵子公司有一个项目,简单的监听短信应用,功能只有如下两个: 1.监听短信并获取短信内容上传服务器: 2.从服务器获取短信内容,发送出去    按照传统的思路,监听短信我们有两种方式:第一种是使用广播 ...

  8. Invoke和BeginInvoke理解

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate,至于委托的本质请参考我的另一随笔:对.net事件的看法. 一.为什么Control类提供了Invoke和Begin ...

  9. LostRoutes项目日志——在main.js中添加多分辨率适配

    初始的Cocos2d-JS项目中的main.js代码的内容为: /** * A brief explanation for "project.json": * Here is th ...

  10. 腾讯云极速配置NodeJS+LNMP运行环境

    版权声明:本文由吴逸翔原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/848754001487150669 来源:腾云阁 h ...