GIT - how to look at older commits without losing changes?
Asked Answered
S

4

7

I'm working on a project (master) that has around 100 commits, no branches etc.

I want to see how the project looked like at commit 1, and then at 10, 20, 30.

How can I look at the early commits without losing any data?

I want to return to where I am now after I see it from previous commits.

How to do that? I think reverting is more permanent and I should look for something else?

Sava answered 31/10, 2014 at 21:49 Comment(1)
"Can I somehow revert to commit 1/10/20/30 etc. without losing any data?" --- how can you checkout to an old commit with losing data at first place?Diploblastic
E
11

For just looking (to 'see' as you ask) do

git checkout [the name of the sha]

You get the sha from doing

git log

Here you see me checking out the initial commit of a repository with hundreds of commits:

$ git checkout 772df05 #  Enough of the sha to be unique, often 6-8 chrs.
Note: checking out '772df05'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 772df05... Initial commit

Looks at files and then...

$ git checkout master

A final note - before switching branches you may need to take of two things:

First any locally changed files, that already exist in git (as opposed to new files) should be either added or stashed. Added is probably the easiest when learning git.
Secondly, any new or changed files that have been git added should be committed - even if just as a "wip".

Endotoxin answered 31/10, 2014 at 21:53 Comment(2)
Works like a charm, but when I try checkout back to master I get : $ git checkout master error: cannot stat 'core/admin': Permission denied error: cannot stat 'core/admin': Permission denied error: cannot stat 'core/admin': Permission denied - tried aborting rebase, but there's no rebase in progress. Yes, I'm on Windows (8.1) of course. Any ideas why this happens and how to fix it? :( [update] Closed Sublime Text, looks like when your changed files are open in any program they got locked on Windows. Solved and left for others if this happens to them as well.Sava
@Sava what if you use sudo before git checkout master?Amigo
T
4

You can also git stash save to save your current work in stash as a patch. Then do whatever you want including checkout revisions or apply patches. Then git stash apply or git stash pop to re apply your local changes.

Edit: oops sorry realize this is a bit off from the original post.

Tender answered 31/10, 2014 at 21:57 Comment(0)
C
3

git revert doesn't lose any data, per se. However, it does change the working copy and the head of the current branch. If you want to look at previous commits, you can use git log and its many arguments. If you want to checkout an actual commit to view the files in an editor, just use git checkout to move to any commit you want. When you are finished, just do git checkout master to return to your current state.

Conker answered 31/10, 2014 at 21:57 Comment(0)
A
2

use git log to see all the commit of your project, scroll down using arrow keys untill you found the commit you want to go.

Then, simply take in note his hash (like 55ea49) annd use the following command:

git checkout <hash>

It will extract files to match the commit, but keeping the other in save, so when you have done, you can go back to the latest commit using

git checkout HEAD

Note that to go 10 commits before the latest, you can do

git checkout HEAD~10

And obviously you can change 10 by any numbers

You should do

git help checkout

to understand what checkout does

Amigo answered 31/10, 2014 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.