git

此文有转载有原创,如有侵权,联系管理员

这里只总结git 作为client的用法, 有机会再总结作为server端的搭建 (ubuntu)

1: 安装 

sudo apt-get install git

git --version  // check

2: clong 

eg: 

1: creat git storage

  • mkdir test
  • cd test
  • git init 

root@ubuntu64-casa:/home# mkdir test
root@ubuntu64-casa:/home# cd test
root@ubuntu64-casa:/home/test# ls -la
total 8
drwxr-xr-x 2 root root 4096 Sep 8 12:32 .
drwxr-xr-x 14 root root 4096 Sep 8 12:32 ..
root@ubuntu64-casa:/home/test# git init
Initialized empty Git repository in /home/test/.git/
root@ubuntu64-casa:/home/test# ls -la
total 12
drwxr-xr-x 3 root root 4096 Sep 8 12:33 .
drwxr-xr-x 14 root root 4096 Sep 8 12:32 ..
drwxr-xr-x 7 root root 4096 Sep 8 12:33 .git
root@ubuntu64-casa:/home/test# more .git/
branches/ config description HEAD hooks/ info/ objects/ refs/
root@ubuntu64-casa:/home/test# more .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
root@ubuntu64-casa:/home/test# tree
.

0 directories, 0 files
root@ubuntu64-casa:/home/test# tree -a
.
└── .git
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
├── heads
└── tags

10 directories, 13 files

2: clone

root@ubuntu64-casa:/home/test# git clone --help

NAME  git-clone - Clone a repository into a new directory

SYNOPSIS
git clone [--template=<template_directory>]
[-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
[-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
[--dissociate] [--separate-git-dir <git dir>]
[--depth <depth>] [--[no-]single-branch]
[--recursive | --recurse-submodules] [--] <repository>
[<directory>]

EXAMPLES
· Clone from upstream:

$ git clone git://git.kernel.org/pub/scm/.../linux.git my-linux
$ cd my-linux
$ make

· Make a local clone that borrows from the current directory, without checking things out:

$ git clone -l -s -n . ../copy
$ cd ../copy
$ git show-branch

· Clone from upstream while borrowing from an existing local directory:

$ git clone --reference /git/linux.git \
git://git.kernel.org/pub/scm/.../linux.git \
my-linux
$ cd my-linux

· Create a bare repository to publish your changes to the public:

$ git clone --bare -l /home/proj/.git /pub/scm/proj.git

3: 基本用法:

  • git基本命令
1)git add 将想要快照的内容写入缓存区
2)git status -s "AM" 状态的意思是,这个文件在我们将它添加到缓存之后又有改动
3)git commit -m '第一次版本提交' -m选项添加备注信息
4)git clone url 使用 git clone 拷贝一个 Git 仓库到本地
5)git diff 查看执行 git status 的结果的详细信息
  尚未缓存的改动:git diff
  查看已缓存的改动: git diff --cached
  查看已缓存的与未缓存的所有改动:git diff HEAD
  显示摘要而非整个 diff:git diff --stat
6)git commit -a 跳过git add 提交缓存的流程 
7)git reset HEAD 用于取消已缓存的内容
8)git rm file 
  git rm 会将条目从缓存区中移除。这与 git reset HEAD 将条目取消缓存是有区别的。
  "取消缓存"的意思就是将缓存区恢复为我们做出修改之前的样子。
  默认情况下,git rm file 会将文件从缓存区和你的硬盘中(工作目录)删除。
9)git mv 重命名磁盘上的文件 如 git mv README README.md
10)git push -u origin master 提交代码
 
  • git 分支管理
1)创建分支命令 git branch (branchname) 列出分支 git branch
2)切换分支命令 git checkout (branchname)
3)合并分支 git merge (branchname)
4)创建新分支并立即切换到该分支下 git checkout -b (branchname)
5)删除分支命令 git branch -d (branchname)
ps:状态 uu 表示冲突未解决 可以用 git add 要告诉 Git 文件冲突已经解决
  • 查看日志版本
git log 命令列出历史提交记录
git log --oneline 查看历史记录的简洁的版本
git log --oneline --graph 查看历史中什么时候出现了分支、合并
  • 标签
为软件发布创建标签是推荐的。这个概念早已存在,在 SVN 中也有。你可以执行如下命令创建一个叫做 1.0.0 的标签:
git tag 1.0.0 1b2e1d63ff
1b2e1d63ff 是你想要标记的提交 ID 的前 10 位字符。可以使用下列命令获取提交 ID:
git log
你也可以使用少一点的提交 ID 前几位,只要它的指向具有唯一性
  • 提取远程仓库代码
1)git fetch  从远程仓库下载新分支与数据
2))git pull  从远端仓库提取数据并尝试合并到当前分支
  • git分支
git-flow主要有5中分支:master、hotfix、release、develop、feature

4: 使用的一些技巧

git checkout . #本地所有修改的。没有的提交的,都返回到原来的状态
git stash #把所有没有提交的修改暂存到stash里面。可用git stash pop回复。
git stash list
git reset --hard HASH #返回到某个节点,不保留修改。
git reset --soft HASH #返回到某个节点。保留修改
 
git clean -df #返回到某个节点 git clean 参数 -n 显示 将要 删除的 文件 和 目录 -f 删除 文件 -df 删除 文件 和 目录
 
 
  • 如果一个文件不小心被删除了,可以有两种方法恢复:
1、需要记住所需恢复文件的名字和版本号(commit id)
      Git checkout commit_id -- file_name
如果不加commit_id,那么git checkout -- file_name 表示恢复文件到本地版本库中最新的状态。
2、
2、不需要记住所需恢复的文件名字。
采用git ls-files命令
要查看删除的文件: git ls-files --deleted
使用命令checkout来恢复:git checkout -- file_name
如果要恢复多个被删除的文件,可以使用批处理命令:
git ls-files -d | xargs git checkout --
如果要恢复被修改的文件,命令:git ls-files -m | xargs git checkout --
 
 
  • 想从server 删掉,但是本地想保存,继续用
 
当我们需要删除暂存区或分支上的文件, 同时工作区也不需要这个文件了, 可以使用
git rm file_path
当我们需要删除暂存区或分支上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制, 可以使用
git rm --cached file_path
file_path 为文件路径
 

 

转载于:https://www.cnblogs.com/dundun/p/7494033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值