Git常用操作命令


字数:1.1k 阅读时长:4分钟 阅读:85

Git是目前世界上最先进的分布式版本控制系统(没有之一)。在日常开发中使用到的git操作命令挺多的,在此记录一下,以备忘却之需。

Git Shell

一、常用操作命令

  1. 查看修改内容
1
git diff test.txt
  1. 查看提交的版本和提交ID
1
2
3
4
5
6
7
8
9
10
11
12
13
git log

# 以版本号的形式查看当前提交版本
git log --pretty=oneline

# 查看简写的提交记录
git log --oneline

# 查看提交后的版本ID
git reflog

# 查看分支合并情况
git log --graph --pretty=oneline --abbrev-commit
  1. 版本回退及撤销commit,撤销pull
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 参数说明
# --hard 删除工作空间改动代码,撤销commit,撤销git add .
# --soft 不删除工作空间改动代码,撤销commit,不撤销git add .

# 返回上一版本
git reset --hard HEAD^^

# 返回到前 1 版本
git reset --hard HEAD~1

# 返回到前 2 版本
git reset --hard HEAD~2

# 滚动到指定ID版本
git reset --hard [ID]

# 未 git add 撤销修改操作
git checkout -- test.js # 撤销指定文件的修改
git checkout . # 撤销全部修改

# 已 git add 撤销
git reset HEAD demo.txt
git checkout -- demo.txt
  1. 删除分支
1
2
3
4
5
# 删除本地分支
git branch -D dev

# 删除远程分支
git push origin --delete dev
  1. 合并分支
1
2
# dev 分支合并到 master 分支
git merge --no-ff -m "commit description" dev
  1. 查看、设置分支

    1
    2
    3
    4
    5
    6
    7
    8
    # 查看当前分支
    git branch

    # 查看所有分支
    git branch -a

    # 设置默认分支 master
    git branch --set-upstream-to=origin/master master
  2. tag操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看所有 tag
git tag

# 打 tag
git tag v1.0.0

# 显示指定版本详细信息
git show v1.0.0

# 为指定 tag 添加文字说明
git tag -a v0.3 -m "desc" ID

# 删除本地 tag
git tag -d v1.0.0

# 推送tag至远程库
git push origin v1.0.0

# 一次性推送全部本地tab
git push origin --tags

# 删除远程的 tag
git push origin :refs/tags/v1.0.0
  1. 远程仓库信息
1
git remote -v
  1. 关联远程仓库
1
2
3
4
5
# 删除 origin
git remote rm origin

# 关联远程
git remote add origin git@gitee.xxx.git
  1. 推送至远程仓库
1
2
3
4
git push origin master

# 把本地库所有内容推送到远程库
git push -u origin master
  1. 修改仓库地址
1
git remote set-url origin git@gitlab.xxx.git
  1. 推送本地所有分支记录到远程
1
git push origin --all
  1. 清除已经删除的远程分支记录
1
git remote prune origin
  1. 创建SSH KEY
1
ssh-keygen -t rsa -C "yourName@163.com"

二、进阶用法

场景:
当你开发一个需求,你的工作进行到一半,这时有一个紧急的线上 bug 需要修复。但是又不想使用 git commit 来保存当前的代码,怎么办?
Git 还提供了一个 stash 功能,可以把当前工作现场 储藏 起来,等以后恢复现场后继续工作。

相关命令:

  1. 储藏代码
1
2
3
4
git stash

# 加上描述信息
git stash save "xxx"
  1. 查看缓存的列表
1
git stash list
  1. 查看详情
1
2
3
4
5
6
7
git stash show [名]

#
git stash show stash@{0}

# 看详细差异
git stash show stash@{0} -p
  1. 恢复储藏的代码
1
2
3
4
5
6
7
8
9
10
11
# 恢复后,stash 内容并不删除
git stash apply

# 恢复指定记录
git stash apply stash@{0}

# 恢复最新储藏的内容后,并删除堆中的记录
git stash pop

# 恢复指定记录,并删除
git stash pop stash@{1}
  1. 删除缓存记录
1
2
3
4
5
# 清除单条记录
git stash drop stash@{0}

# 清除所有
git stash clear
  1. 基于缓存创建分支
1
2
3
4
5
6
7
8
9
10
11
12
# 语法
git stash branch <branchname> [<stash>]

# 基于最新缓存创建分支
git stash branch new-branch

# 等价于
git checkout -b new-branch
git stash apply

# 基于指定缓存创建分支
git stash branch new-branch stash@{2}
  1. 回退代码,又保留提交历史

具体操作:假设当前最新提交就在分支current-branch上,回退提交为prev-commit,这个回退提交可以是一次commit id,也可以是一个tag,也可以是一个分支名。

1
2
3
4
5
6
git checkout prev-commit
git diff current-branch > ~/diff.patch
git checkout current-branch
cat ~/diff.patch | git apply
git commit -am "roll back to prev-commit"
git push

Git 相关系列


欢迎访问:天问博客

本文作者: Tiven
发布时间: 2021-12-01
最后更新: 2023-07-18
本文标题: Git常用操作命令
本文链接: https://www.tiven.cn/p/656d75c5/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!
欢迎留言,提问 ^_^
个人邮箱: tw.email@qq.com
notification icon
博客有更新,将会发送通知给您!