Skip to content

Instantly share code, notes, and snippets.

@xiongxin
Last active September 9, 2018 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiongxin/87b1b2461ef19b93c2bbe8a3106085bf to your computer and use it in GitHub Desktop.
Save xiongxin/87b1b2461ef19b93c2bbe8a3106085bf to your computer and use it in GitHub Desktop.
GIT版本管理使用笔记
  1. 开发环境 release分支
  2. 测试环境 release/development分支
  3. 预发布环境 release分支
  4. 生产环境 master分支

git-config

  • 用户配置
    • git config --global user.name "John EverThing"
    • git config --global user.email test@qq.com
  • 配置基本
    • --local 【默认,高优先级别】:只影响本仓库 .git/config
    • --global 【中优先级】:影响到所有当前用户的git仓库 ~/.gitconfig
    • --system 【低优先级】:影响到全系统的git仓库 /etc/gitconfig

git-init 初始化项目

git status 对状态的跟踪

状态

git文件状态

git add 添加文件内容到暂存区(同时文件被跟踪)

从暂存区删除git-rm

  • git rm --cached : 仅从暂存区删除
  • git rm : 从暂存区与工作目录删除
  • git rm $(git ls-files --deleted) : 删除所有被跟踪,但是在工作目录被删除的文件

工作目录与暂存区

PS D:\Codes\test> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        package.json

通过上面的命令行参数可以看出我们在修改README.md文件时,仅仅时修改了工作目录的文件,没有将其放置到暂存区,需要手动提交到暂存区

暂存区

  • 物品:文件
  • 货架:工作目录
  • 购物车:暂存区
  • 购买:提交内容

git commit 将暂存区内容创建一个提交记录,提交到提交区

  • git commit -m 'first commit'
  • git commit -a -m 'first commit' 直接提交

git log 查看提交记录

  • git log --oneline
  • git log --color --graph

git diff

  • git diff : 工作目录与暂存区的差异
  • git diff --cached [] : 暂存区与末次提交差异,默认为HEAD
  • git diff : 工作目录与某次提交的差异

撤销本地修改

  • git checkout -- : 丢弃修改,将文件从暂存区复制到工作目录(及从add状态恢复到工作目录)

撤销暂存区修改

  • git reset HEAD 将文件内容从上次提交复制到暂存区,会出现工作目录与暂存区文件不一致,需要重新添加到暂存区
  • git checkout HEAD -- 将内容从上次提交复制到工作目录

撤销全部改动

总结

tim 20180909131815

git branch 分支的增删查改都靠它

  • git branch 创建分支
  • git branch -d 删除分支
  • git branch -v 显示分支信息

git checkout 通过移动HEAD检出版本,可用于分支切换

  • git checkout 切换分支
  • git checkout -b 创建并切换分支

git reset 将当前分支回退到历史某个版本

  • git reset --mixed 默认
    • 将当前内容复制到暂存区
  • git reset --soft
    • 工作目录和暂存区不会变化
  • git reset --hard
    • 内容复制到工作目录

git stash

git merge 合并分支

  • git merge next master

tim 20180909154053

解决merge冲突

  • 修改冲突文件
  • git add .
  • git commit -m 'resolve'

merge fast-forward

  • merge 默认是fast-forward模式 tim 20180909155217

  • git merge next master --no-ff :不适用fast-fowrd模式 tim 20180909155426

git rebase : 修剪提交历史极限,俗称 变基

  • 千万不要在共有分支使用rebase
  • 在开发分支使用 git rebase master 在master线性重演当前分支的commit历史
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment