git 笔记

git 日常用到的命令,记录一下。

git简介

git是一款免费并且开源的分布式版本控制系统,也是现在最先进的分布式管理系统。用于处理项目。

开启工作区

// 一般来说项目至少有 readme.md .gitignore lisense  3个文件
git init 初始化git仓库 

git clone url 复制一个仓库 
git help 查看帮助 

配置

在git中,我们使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:仓库级别 local 【优先级最高】、用户级别 global【优先级次之】、系统级别 system【优先级最低】

git config --(local/global/system) -l 查看仓库配置
git config --(local/global/system) -e 编辑仓库配置
git config user.name "tom"
git config user.email "hhh@qq.com"
git config --global alias.ci commit
git config --global alias.a "add -A"
// 别名配置 
git config --global alias.a 'add -A' 
git config --global alias.b branch  
git config --global alias.c  "commit -m" 
git config --global alias.ac  "commit -a -m" 
git config --global alias.ck checkout 
git config --global alias.st status

git add

在提交之前,Git有一个暂存区(staging area),可以放入新添加的文件或者加入新的改动. commit时提交的改动是上一次加入到staging area中的改动,而不是我们disk上的改动.
git add .
会递归地添加当前工作目录中的所有文件.

git add index.js  将index.js修改提交到本地暂存区
git add -A  提交所有变化
git add -u  提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add .  提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件

撤销修改

// 用暂存区的修改覆盖当前
git checkout demo.txt 

// 用上次提交覆盖当前修改 
git reset demo.txt   

// 回退文件到某个版本
git checkout 5dc1bc214 templates/publish_advertisement.html

 提交版本

git add -A
git commit -m "第一次提交"

// 跳过使用暂存区域,自动把所有跟踪文件一起提交 
git commit -a -m "第二次提交"

提交到远程仓库

一个git可以有多个remote仓库

// 添加远程仓库
git remote add alibaba https://github.com/paulboone/ticgit
// 拉取更新
git fetch alibaba master
// 推送更新
git push alibaba master 
// 远程仓库重新命名
git remote rename alibaba tecent 
// 删除仓库 
git remote rm tencent 

 版本回退

git reflog //所有版本历史

git reset --hard hash 

git log美化

// 详细配置
const content = 
" %H 提交对象(commit)的完整哈希字串
 %h 提交对象的简短哈希字串
 %T 树对象(tree)的完整哈希字串
 %t 树对象的简短哈希字串
 %P 父对象(parent)的完整哈希字串
 %p 父对象的简短哈希字串
 %an 作者(author)的名字
 %ae 作者的电子邮件地址
 %ad 作者修订日期(可以用 --date= 选项定制格式)
 %ar 作者修订日期,按多久以前的方式显示
 %cn 提交者(committer)的名字
 %ce 提交者的电子邮件地址
 %cd 提交日期
 %cr 提交日期,按多久以前的方式显示
 %s 提交说明"

// log美化
git config --global alias.lg "log --no-merges --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"

// stash list 美化
git config --global alias.shs '
list --pretty=format:"%C(red)%h%C(reset) - %C(dim yellow)(%C(bold magenta)%gd%C(dim yellow))%C(reset) %<(70,trunc)%s %C(green)(%cr) %C(bold blue)<%an>%C(reset)"'

冲突显示 vim 编辑

`Please enter a commit message to explain why this merge is necessary,
especially if it merges an updated upstream into a topic branch。`
先按esc 
:wq 推出 

stash

一般作用: 代码提交之前,暂存修改,merge其他人修改后,弹出stash,可以防止代码冲突

git stash  // 保存本地更改
git stash save -a "messeag" //
git stash list 
git stash pop --index stash@{0}
git stash apply stash@{id} //和pop一样,但是会保存stash记录 
git stash clear // 清空
git stash drop // 删上一条

分支

// 创建dev并且切换到dev
git checkout -b dev 

// 克隆远程分支创建本地dev
git checkout -b dev origin/dev 

// 删除分支
git branch -D bambaoo

// 拉远程特定分支
git clone -b dev ${url}

// 查看当前跟踪的分支
git br -vv

cherry-pick

pick其中的一个分支的一次提交,合并到当前分支 

git cherry-pick 08ru289 

修改提交信息

git commit --amend -m "update"

git认证失败 储存密码

git config --system --unset credential.helper

//长期存储密码:
git config –global credential.helper store

github项目里的静态页面

github 项目新建gh-pages分支,可以显示静态页面,在项目的settings里可以找到网址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wwwarewow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值