Using a public repo, I want to get my master branch back to a certain commit from the past. I have reviewed the options and the best thing for me looks to be a simple checkout t
git stash may be the quickest way to clean up the working tree. and then git checkout -b $newbranch $commit-sha1-you-want to create a branch you are going to work with. after all your work's done, git stash pop to restore the working tree.
Git checkout will not remove files added since a previous commit. To do this I would need git revert
.
However I find git checkout thehash .
a lot easier to use, and its not so hard to see which files have been added since that hash:
git diff --name-status HEAD thehash
Do you want to roll back your repo to that state? Or you just want your local repo to look like that?
See https://git-scm.com/docs/git-reset for git reset.
CASE 1: if you do
git reset --hard [commit hash]
It will make your local code and local history be just like it was at that commit. But then if you wanted to push this to someone else who has the new history, it would fail.
CASE 2: if you do
git reset --soft [commit hash]
It will make your local files changed to be like they were then, but leave your history etc. the same.
I found answer here. Also you can see related answer here.
When you used git checkout aaa1 .
, you told Git to translate aaa1
to a commit, find that commit (more precisely, its tree), and copy every file in that commit to your index / staging area and work-tree.
Let's say, just for the sake of argument, that you start with master
containing two files, README
and hello
:
$ git checkout master
[output snipped]
$ ls
README hello
$ cat README
Yay, you read me!
$ cat hello
world
$
Let's say further that commit aaa1
exists and has two files in it, README
and addendum
. Its README
says Thank you for reading.
Let's do that checkout:
$ git checkout aaa1 -- .
[output snipped]
$ ls
README addendum hello
(I added the --
: it's not actually required here, but it's good practice.) The contents of README
are the updated README
. The file addendum
has also been extracted. The file hello
is not removed and remains unchanged from the version found in master
. The updated README
and hello
are staged:
$ git status --short
M README
A addendum
but hello
is not removed:
$ git ls-files --stage
100644 ac6f2cf1acbe1b6f11c7be2288fbae72b982823c 0 README
100644 7ddf1d71e0209a8512fe4862b4689d6ff542bf99 0 addendum
100644 cc628ccd10742baea8241c5924df992b5c019f71 0 hello
Using git clean
, even with -x
, will have no effect: nothing needs cleaning; there are no unstaged files (hello
is staged, it's just not modified).
You specifically wanted to get the work-tree to match commit aaa1
, byte for byte. To do that, you must find files that are in the index now, but were not in aaa1
, and remove them.
There is, however, an easier way: just remove everything. Then, use your git checkout aaa1 -- .
to extract everything from aaa1
. This will fill in the index and work-tree from aaa1
: any files that need to be restored to the way they were before removing, are restored (to the way they were in aaa1
which is the same as the way they are in HEAD
). Any files that need to be changed to match the way they were in aaa1
, are restored (to the way they were in aaa1
which is different).
$ git rm -rf .
rm 'README'
rm 'addendum'
rm 'hello'
$ git checkout aaa1 -- .
$ git ls-files --stage
100644 ac6f2cf1acbe1b6f11c7be2288fbae72b982823c 0 README
100644 7ddf1d71e0209a8512fe4862b4689d6ff542bf99 0 addendum
$ git status --short
M README
A addendum
D hello
You can now commit and you will have a new commit on master
that, regardless of what was there before, has exactly the same tree as aaa1
.
(Whether this is a good idea is another thing entirely, but it will get you the desired state.)