Git

作者: BenXian | 来源:发表于2017-04-01 17:22 被阅读40次

[TOC]

Schematic diagram

1.log

(1)查看提交记录:

$ git log --pretty=oneline  #简单输出日志
$ git log git log <file>    # 查看该文件每次提交记录 
$ git log -p <file>   # 显示版本历史,以及版本间的内容差异 
$ git log -p -2       # 查看最近两次详细修改内容的diff 
$ git log --stat      # 查看提交统计信息 
$ git log --since="6 hours"  # 显示最近6小时提交 
$ git log --before="2 days"  # 显示2天前提交 
$ git log -1 HEAD~3          # 显示比HEAD早3个提交的那个提交 
$ git log -1 HEAD^^^ git reflog    # 查看操作记录

(2)显示某次提交的内容:

$ git show <commit id>

2.push

(1).推送新建branch 到远程:

$ git push origin seconddev -u

3.回滚

(1).HEAD 代表当前commit版本 , HEAD^ 代表上一个commit版本

(2).添加、提交、删除、找回,重置修改文件:

$ git checkout --$ git reset --hard 需要小心,会抛弃工作区修改

$ git checkout  -- <file>   # 抛弃工作区修改 
$ git checkout  .           # 抛弃工作区修改 
$ git checkout HEAD <file>  # 抛弃工作目录区中文件的修改 
$ git add <file>      # 将工作文件修改提交到本地暂存区 
$ git add .           # 将所有修改过的工作文件提交暂存区 
$ git rm <file>       # 从版本库中删除文件 
$ git rm <file> --cached  # 从版本库中删除文件,但不删除文件 
$ git reset <file>    # 从暂存区恢复到工作文件 
$ git reset -- .      # 从暂存区恢复到工作文件 
$ git reset --hard  HEAD^ # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改 
$ git reset --hard <commit id>  # 恢复到某一次提交的状态 
$ git reset HEAD <file> # 抛弃暂存区中文件的修改 
$ git commit <file> 
$ git commit . 
$ git commit -a           # 将git add, git rm和git ci等操作都合并在一起做 
$ git commit -am "some comments" 
$ git commit --amend      # 修改最后一次提交记录 
$ git revert <$id>    # 恢复某次提交的状态,恢复动作本身也创建了一次提交对象 
$ git revert HEAD     # 恢复最后一次提交的状态

(3).记录每一次命令:

$ git reflog

3.对比、查看文件:

 $ git diff HEAD -- file #查看工作区和版本库里面最新版的区别
 $ git diff <file>     # 比较当前文件和暂存区文件差异 
 $ git diff git diff <$id1> <$id2>      # 比较两次提交之间的差异 
 $ git diff <branch1>..<branch2>   # 在两个分支之间比较  
 $ git diff --staged   # 比较暂存区和版本库差异 
 $ git diff --cached   # 比较暂存区和版本库差异 
 $ git diff --stat     # 仅仅比较统计信息

4.删除

(1).删除版本库里面的文件:

$ git rm --file

5.添加远程库

(1).本地库与远程库关联:

$ git remote add origin git@github.com:github账户名/仓库名.git

(2).把本地的推送到远程同步并关联跟踪:

$ git push -u origin master

6.分支

(1).not fast-forward merge:

$ git merge --no-ff -m 'commit mark' dev

(2).指定本定dev分支与远程dev链接(提示“no tracking information”时用):

$ git branch --set-upstream branch-name origin/branch-name

(3).delete the local branch -bendev:

$ git branch -d bendev

(4).查看、切换、创建和删除分支:

$ git branch -r           # 查看远程分支 
$ git branch -v           # 查看各个分支最后提交信息 
$ git branch -a           # 列出所有分支 
$ git branch --merged     # 查看已经被合并到当前分支的分支 
$ git branch --no-merged  # 查看尚未被合并到当前分支的分支 
$ git branch <new_branch> # 基于当前分支创建新的分支 
$ git branch <new_branch>  <start_point>      # 基于另一个起点(分支名称,提交名称或则标签名称),创建新的分支 
$ git branch -f <existing_branch>  <start_point> # 创建同名新分支,覆盖已有分支 
$ git branch -d <branch>  # 删除某个分支 
$ git branch -D <branch>  # 强制删除某个分支 (未被合并的分支被删除的时候需要强制) 
$ git checkout <branch>         # 切换到某个分支 
$ git checkout -b <new_branch>  # 创建新的分支,并且切换过去 
$ git checkout -b <new_branch> <branch>       # 基于branch创建新的new_branch 
$ git checkout -m <existing_branch> <new_branch>  # 移动或重命名分支,当新分支不存在时 
$ git checkout -M <existing_branch> <new_branch>  # 移动或重命名分支,当新分支存在时就覆盖 
$ git checkout $id               # 把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除 
$ git checkout $id -b <new_branch>       # 把某次历史提交记录checkout出来,创建成一个分支

(5).分支合并和rebase:

$ git merge <branch>                  # 将branch分支合并到当前分支 
$ git merge origin/master --no-ff     # 不要Fast-Foward合并,这样可以生成merge提交 
$ git merge --no-commit <branch>      # 合并但不提交 
$ git merge --squash <branch>         # 把一条分支上的内容合并到另一个分支上的一个提交 
$ git rebase master <branch>          # 将master rebase到branch,相当于: 
$ git checkout <branch> && git rebase master && git checkout master && git merge <branch>

(6).远程分支管理:

$ git pull                         # 抓取远程仓库所有分支更新并合并到本地 
$ git pull --no-ff                 # 抓取远程仓库所有分支更新并合并到本地,不要快进合并 
$ git fetch origin                 # 抓取远程仓库所有更新 
$ git fetch origin remote-branch:local-branch #抓取remote-branch分支的更新 
$ git fetch origin --tags          # 抓取远程上的所有分支 
$ git checkout -b <new-branch> <remote_tag> # 抓取远程上的分支 
$ git merge origin/master          # 将远程主分支合并到本地当前分支 
$ git checkout --track origin/branch     # 跟踪某个远程分支创建相应的本地分支 
$ git checkout -b <local_branch> origin/<remote_branch>  # 基于远程分支创建本地分支,功能同上 
$ git push                         # push所有分支 
$ git push origin master           # 将本地主分支推到远程主分支 
$ git push -u origin master        # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库) 
$ git push origin <local_branch>   # 创建远程分支, origin是远程仓库名 
$ git push origin <local_branch>:<remote_branch>  # 创建远程分支 
$ git push origin :<remote_branch>  #先删除本地分支(git br -d <branch>),然后再push删除远程分支//Push an existing repository from the command linegit remote add origin https://github.com/用户名/项目.git
$ git push -u origin master

7.stash

(1).pop 出satsh的内容并且删掉stash里面的(以下两种方法一样效果):

a. $ git stash pop

b.

 $ git stash apply    # 恢复暂存的内容    
 $ git stash drop   # 删除暂存区

8.远程

(1).产看远程情况:

$ git remote -v

9.修改git config

(1).列举所有配置:

$ git config -l

(2).log:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

(3).username && email:

$ git config --global user.name "Your Name"$ git config --global user.email "email@example.com"

(4).其他配置:

$  git config --global color.ui true
$  git config --global alias.co checkout 
$  git config --global alias.ci commit 
$  git config --global alias.st status 
$  git config --global alias.br branch

(5).用户配置信息:

git配置文件在~/.gitconfig,使用$ cat .gitconfig即可浏览用户配置信息。

(6).钥位置:

/Users/you/.ssh/

相关文章

网友评论

      本文标题:Git

      本文链接:https://www.haomeiwen.com/subject/ulziottx.html