查看git help reset,特别是--soft--mixed和--hard的部分,以便更好地了解它的作用。
重新加载
如果你搞砸了,你可以随时使用reflg来查找丢弃的提交:
$ git reset @~$ git reflogc4f708b HEAD@{0}: reset: moving to @~2c52489 HEAD@{1}: commit: added some .class files$ git reset 2c52489... and you're back where you started
git logcommit 101: bad commit # Latest commit. This would be called 'HEAD'.commit 100: good commit # Second to last commit. This is the one we want.
要将所有内容恢复到上次提交之前的状态,我们需要将reset恢复到HEAD之前的提交:
git reset --soft HEAD^ # Use --soft if you want to keep your changesgit reset --hard HEAD^ # Use --hard if you don't care about keeping the changes you made
git commit -m 'restoring the file I removed by accident'git logcommit 102: restoring the file I removed by accidentcommit 101: removing a file we don't needcommit 100: adding a file that we need
echo "some changes..." > file.htmlgit add file.htmlgit commit -m "wrong commit"
# I need to resetgit reset --hard HEAD~1 (cancel changes)# ORgit reset --soft HEAD~1 # Back to staginggit reset HEAD file.html # back to working directorygit checkout -- file.html # cancel changes
# Checkout a given commit.# Doing so will result in a `detached HEAD` which mean that the `HEAD`# is not pointing to the latest so you will need to checkout branch# in order to be able to update the code.git checkout <commit-id>
# create a new branch forked to the given commitgit checkout -b <branch name>
# This will destroy any local modifications.# Don't do it if you have uncommitted work you want to keep.git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:git stashgit reset --hard 0d1d7fc32git stash pop# This saves the modifications, then reapplies that patch after resetting.# You could get merge conflicts if you've modified things which were# changed since the commit you reset to.
git commit --amend# follow prompts to change the commit message
哦,狗屎,我不小心犯了一些主人,应该是一个全新的分支!
# Create a new branch from the current state of mastergit branch some-new-branch-name# Remove the commit from the master branchgit reset HEAD~ --hardgit checkout some-new-branch-name# Your commit lives in this branch now :)
git push --delete (branch_name) //this will be removing the public version of your branch
git push origin (branch_name) //This will add the previous version back
$ git reset HEAD~fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.Use '--' to separate paths from revisions, like this:'git <command> [<revision>...] -- [<file>...]'
Call this following git-commit-edit and put it in your $PATH:
#!/bin/bash
# Do an automatic git rebase --interactive, editing the specified commit# Revert the index and working tree to the point before the commit was staged# https://stackoverflow.com/a/52324605/5353461
set -euo pipefail
script_name=${0##*/}
warn () { printf '%s: %s\n' "$script_name" "$*" >&2; }die () { warn "$@"; exit 1; }
[[ $# -ge 2 ]] && die "Expected single commit to edit. Defaults to HEAD~"
# Default to editing the parent of the most recent commit# The most recent commit can be edited with `git commit --amend`commit=$(git rev-parse --short "${1:-HEAD~}")
# Be able to show what commit we're editing to the userif git config --get alias.print-commit-1 &>/dev/null; thenmessage=$(git print-commit-1 "$commit")elsemessage=$(git log -1 --format='%h %s' "$commit")fi
if [[ $OSTYPE =~ ^darwin ]]; thensed_inplace=(sed -Ei "")elsesed_inplace=(sed -Ei)fi
export GIT_SEQUENCE_EDITOR="${sed_inplace[*]} "' "s/^pick ('"$commit"' .*)/edit \\1/"'git rebase --quiet --interactive --autostash --autosquash "$commit"~git reset --quiet @~ "$(git rev-parse --show-toplevel)" # Reset the cache of the toplevel directory to the previous commitgit commit --quiet --amend --no-edit --allow-empty # Commit an empty commit so that that cache diffs are un-reversed
echoecho "Editing commit: $message" >&2echo
commit e305d21bdcdc51d623faec631ced72645cca9131 (HEAD -> master, origin/master, origin/HEAD)Author: Christophe <blabla@bla.com>Date: Thu Jul 30 03:42:26 2020 +0200
U2_30 S45; updating files package.json & yarn.lock for GitHub Web Page from docs/CV_Portfolio...
承诺我们现在想要的
commit 36212a48b0123456789e01a6c174103be9a11e61Author: Christophe <blabla@bla.com>Date: Thu Jul 30 02:38:01 2020 +0200
First commit, new title
通过删除最后一个来达到之前的提交
$ git reset --hard 36212a4
HEAD is now at 36212a4 First commit, new title
检查是否正常
$ git log
commit 36212a48b0123456789e01a6c174103be9a11e61 (HEAD -> master)Author: Christophe <blabla@bla.com>Date: Thu Jul 30 02:38:01 2020 +0200
First commit, new title
$ git status
On branch masterYour branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.(use "git pull" to update your local branch)
nothing to commit, working tree clean
# Checkout a given commit.# Doing so will result in a `detached HEAD` which mean that the `HEAD`# is not pointing to the latest so you will need to checkout branch# in order to be able to update the code.git checkout <commit-id>
# Create a new branch forked to the given commitgit checkout -b <branch name>
# This will destroy any local modifications.# Don't do it if you have uncommitted work you want to keep.git reset --hard 0d1d7fc32
# Alternatively, if there's work to keep:git stashgit reset --hard 0d1d7fc32git stash pop# This saves the modifications, then reapplies that patch after resetting.# You could get merge conflicts if you've modified things which were# changed since the commit you reset to.