Git 学习

基础命令

1. git --version 查看机器上安装的git的版本

2. git config  配置git的配置文件, git配置级别主要有以下3类:

git config --local   仓库级别, 优先级最高,仓库级别对应的配置文件是当前仓库下的.git/config

git config --global 用户级别,优先级次之, 用户级别对应的配置文件是用户宿主目录下的~/.gitconfig 

git config --system 系统级别, 优先级最低, 系统级别对应的配置文件是git安装目录下的 /etc/gitconfig

查看仓库配置 

git config [--local|--global|--system] -l 

添加仓库配置 (以用户名和邮箱以作为标识)

 git config --global user.name "your name"    
 git config --global user.emial "your email"

3. git checkout / revert / reset 

Undoing Commits & Changes

4. git branch and merge 

Git Branching - Basic Branching and Merging 

5. git stash and clean

Git Stashing and Cleaning

How to extract a single file or diff a file from a git stash ?

$ git diff stash@{0}^1 stash@{0} -- <filename>

Explanation: A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created. So you can treat stash (e.g. stash@{0} is first / topmost stash) as a merge commit, and use stash@{0}^1 means the first parent of the given stash, which as stated in the explanation above is the commit at which changes were stashed away. Likewise, you can use git checkout to check a single file out of the stash:

$ git checkout stash@{0} -- <filename>

6. 可视化git提交历史和时间线

git log --graph
git log --graph --oneline

7. 更新.gitignore

.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的, 需要

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

8. git diff 

用git diff 来比较项目中任意两个版本的差异

# 显示当前工作目录与上次提交之间的所有差别
git diff HEAD
 
# 显示当前工作目录下的lib目录与上次提交之间的差别
git diff HEAD -- ./lib

# 显示当前工作目录与另外一个分支(e.g., dev)的差别
git diff dev

# 显示两个分支间的差异
git diff master..test 

9. git stash 

# 缓存本地未提交的改变
git stash 

# 列出所有stashes 
git stash list
 
# 恢复某一个stash
git stash apply # 最近的一个
git stash apply stash@{1} # 指定的一个

# 删除一个stash 
git stash drop  # 最近的一个
git stash drop stash@{2} # 指定的一个
git stash clear # 所有的

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值