git学习笔记

来源:互联网 发布:误删数据库怎么恢复 编辑:程序博客网 时间:2024/06/05 06:00

在Git中,用HEAD表示当前版本。上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

修改最后一次commit的记录

git commit -m "a wrong commit"#修改一些数据touch changeFilegit add changeFile
git commit --amend  
git revert HEAD~1  #回滚到倒数第二次commit

revert和reset的区别

  • git revert是用一次新的commit来回滚之前的commit
  • git reset:set the current branch head (HEAD) to commit
git revert <SHA># resolve conflictsgit revert --continue
git reset --soft  <SHA> #Does not touch the index file or the working tree at allgit reset --mixed(default ) <SHA> #Resets the index but not the working tree git reset --hard  <SHA> #Resets the index and working tree
git checkout <branch>       #switch branchrm -f hello.cgit checkout master hello.c     #restore hello.c from the index 

查看文件中的每一行的作者、最新的变更提交和提交时间

git blame <file_name>

git reflog

列出了head曾经指向过的一系列commit。要明白它们只存在于你本机中;而不是你的版本仓库的一部分,也不包含在push和merge操作中。

git reflog       

rebase压缩多个Commit

分支开发完成后,很可能有一堆commit,但是合并到主干的时候,往往希望只有一个(或最多两三个)commit,这样不仅清晰,也容易管理。

git rebase -i master

git rebase命令的i参数表示互动(interactive),这时git会打开一个互动界面,进行下一步操作。

Stash未提交的更改

你正在修改某个bug或者某个特性,又突然被要求展示你的工作。而你现在所做的工作还不足以提交,这个阶段你还无法进行展示(不能回到更改之前)。在这种情况下, git stash可以帮助你。stash在本质上会取走所有的变更并存储它们为以备将来使用。

git stashgit stash list  #希望检查stash列表git stash apply #解除stash并且恢复未提交的变更

检查丢失的提交

尽管 reflog 是唯一检查丢失提交的方式。但它不是适应用于大型的仓库。那就是 fsck(文件系统检测)命令登场的时候了。

git fsck --lost-found

你可以通过运行 git show [commit_hash] 查看提交之后的改变或者运行git merge [commit_hash] 来恢复到之前的提交。

cherry-pick

cherry-pick就是从不同的分支中捡出一个单独的commit,并把它和你当前的分支合并。
这里写图片描述

Refer:

  • 10 个迅速提升你 Git 水平的提示
  • 总结自己的Git常用命令
  • Git回滚的常用手法
  • git revert和git reset的区别
0 0