Git 常用命令

96 阅读2分钟

Git 基础

Git Basic

Git 工作流图示

Git work picture

28857727-07d688ece4545111.png

Git 基础命令

Git basic command

git config --global user.name "Emma"

git config --global user.email "emma@xx.com"

git init                        // init .git

git log

git reflog                      // all history log, including reset etc.

git diff                        // 工作区与暂存区的区别

28857727-93f60a4bf07bb455.png

28857727-6a738ff3ab8aaf95.png

Git 常用命令

Common commands

  1. 提交代码

Push code

git status                    // 查看当前状态

git add [file-name]          // 暂存文件

git commit -m "msg"          // 提交到本地仓库

git pull --rebase            // 更新代码

git push                      // 提交到远程仓库

  1. 合并代码

Merge code

git cherry-pick [commit-hash]    // “拿”其他提交hash到自己的分支上

git merge [branch-name] --no-ff  // 合并其他分支到当前分支,在git graph上,会有一个凸起的折线,如下图

28857727-b595780da7295bb2.png

git rebase [branch-name]        // 通过rebase的形式合并其他分支的代码到当前分支,在git graph上,合并之后的代码树是一条直线,如下图

28857727-8eb292f1310792dd.png

  1. 清理、重命名

Clear or rename branch

git remote prune origin      // 清理掉远程已经不存在的本地分支

git branch -a                // 查看所有分支

git branch -m [old-branch] [new-branch] // 分支重命名

git branch -d   // 删除本地分支(分支未合并且未被推送到远端会删除失败,见下图)

git branch -D   // 强制删除本地分支

28857727-e5e49d491e79f804.png

  1. 切换分支

Checkout branch

git branch -a      // 查看所有分支

git branch -r      // 查看所有远程分支

git branch -l      // 查看所有本地分支

git checkout   // 切换分支

  1. 回滚代码

Rollback code

git reset --hard    // code disappear

git reset --soft    // need re-commit

git reset          // need re-add,等价于 git reset --mixed

git reset HEAD^    // 撤回到上一个节点

git reset HEAD~1    // 等价于HEAD^

git revert [hash]        // 反做某一个hash的提交, 会自动commit

git revert -n [hash]      // 撤销某一个hash的提交,但是不想直接去除掉提交记录,可以反做某一个hash,但需要执行git commit -m "XX" 提交反做

git revert HEAD5..HEAD2 // 反做倒数第五个提交到倒数第三个提交直接的代码,前闭后开区间

28857727-f5a44d4b95f3078d.png

如何与远程仓库建立关联

Establish connection with remote

本地没有目录,克隆一个远程仓库。

Create a new repository

git clone http://xxx/xx.git

cd xx

touch README.md

git add README.md

git commit -m "add README"

git push -u origin master

已有本地文件夹

Existing folder

cd existing_folder

git init

git remote add origin http://xxx/xx.git

git add .

git commit -m "Initial commit"

git push -u origin master

已关联一个远程仓库

Existing Git repository

cd existing_repo

git remote rm origin  // if exist remote, clear origin first

git remote add origin http://xxx/xx.git

git push -u origin --all

git push -u origin --tags

Git 提交快捷工具

Git cz

可以去github看cz-cli github

安装

install

npm install -g commitizen

npm install -g cz-conventional-changelog

在用户根目录下创建.czrc。

In /home/your work directory/.czrc add below sentence

windows home: C:\Users\Emma

{ "path": "cz-conventional-changelog" }

image.png

附录

官方文档