Git

开篇想了很久才决定要写什么,作为一名 Web 开发人员,第一必备的技能应该就是 git 了。没什么好说的,记录git一些常用命令,以备查阅。

git 命令

  1. cmd
    最常用的命令

    1
    2
    3
    4
    5
    git add
    git commit
    git status -sb // 参数 sb 很有用~!
    git diff
    git push
  2. git 的状态
    git init

    1
    2
    3
    4
    5
    6
    $ git init  //这是一个空的仓库
    Initialized empty Git repository in
    $ git status -sb // 此时状态为 未跟踪,此时创建文件后为 红色 M
    On branch master
    Initial commit
    Untracked files:xxxx

git add

1
2
3
4
5
6
7
8
9
修改文件后
$ git status -sb // 修改文件在工作区,未提交到 stage。 此时为绿色 M
On branch master
Changes not staged for commit:

$ git add ./*
$ git status // 从工作区 add 到 stage 区
xxxx
Changes to be committed:

git commit

1
2
3
$ git commit -m "xxx"

// 从 stage 提交到 master

git checkout

1
2
3
git checkout --xx //丢弃xx文件工作区修改。.让这个文件回到最近一次

git checkout dev //切换分支

分支:

1
2
3
4
5
6
git branch; // 查看当前分支
git branch dev; 创建分支
git checkout dev ;切换分支
git checkout -b dev; 创建并且换
git merge dev; 先处在master 然后合并分支dev到master
git merge --no-ff -m "merge with no-ff" dev //最好使用这条,禁用Fast forward

log

1
git log --graph --pretty=oneline --abbrev-commit

stash 雪藏

1
2
// 历史将为编辑好工作区缓存去来
git stash git stash list git stash apply git stash drop git stash pop

git remote -v

1
git remote -v //查看远端库

tag // 易于团队版本号查询,目前用不上

  1. 解决冲突
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

git diff // 查看不同
git log --pretty=oneline
git reset --hard HEAD^ //退回上一个版本
git reset --hard 1094a //退回固定版本

git reflog
git checkout --xxx //直接丢弃工作区的修改

//当你不但改乱了工作区某个文件的内容,还添加到了stage区时(add),想丢弃修改,分两步,第一步用命令git reset HEAD <file>,就回到了场景1,第二步按场景1操作。

git branch dev; git checkout dev ;git checkout -b dev //创建并切换分支
git merge dev; git merge --no-ff -m "merge with no-ff" dev // 合并分支, 在master分支下,合并dev

git log --graph --pretty=oneline --abbrev-commit
git stash git stash list git stash apply git stash drop git stash pop
git remote -v
  1. github

生成本地钥匙和锁

1
ssh-keygen -t rsa -b 4096 -C "email.address"

~/.ssh 中 生成 钥匙:id_rsa 与 锁 id_rsa.pub。

将 锁 id_rsa.pub 上传到github。

尝试连接github,用本地的 钥匙 去开 github 上的锁

1
ssh -T git@github.com

连接成功后,会将github IP 保存到 known_hosts 中。

至此, 本地与github建立连接。

git 本地配置

1
git config XXXX  // 看文档吧。一辈子配置不了几次。

完。