Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to move back and forth between commits

I have a newbie question about Git:

I need to move back and forth in a history of a branch. That means, I need to get all the files to the state they were in in some old revision, and then I need to get back to the latest state in the repository. I don't need to commit.

With SVN, it would be

svn up -r800 

to get to revision 800, and

svn up 

to get in sync with the repository.

I know the hash of the commit I want to get back to, so I tried

git reset <hash> 

which seems to get me there. But then I tried

git pull 

but that complains about conflicts.

So what's the proper way to move through the history of the branch?

I'm thinking in terms of SVN, so don't hezitate to point me to some nice tutorial. Note that I've already checked http://git.or.cz/course/svn.html and http://www.youtube.com/watch?v=8dhZ9BXQgc4 .

Thanks, Ondra.

like image 953
Ondra Žižka Avatar asked Jan 22 '10 23:01

Ondra Žižka


People also ask

How do I move between commits?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

How do I move back one commit?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.


1 Answers

Well, I'm a former svn user too, and now use git for all my projects.

When using git, you should change the way of thinking from the client-server architecture that's used in svn. In svn, every change needs a connection with server. Using git, your repo is in the working directory. You don't need a connection for every repo action.

Only use git push and git pull to synchronise with repo. Think of it like using rsync or any backup solution, to make two place have exactly same content. Just like you connect external backup hard disk, then make the content in it same with the content in your main. That's the usage of git pull and git push.

If you just want to go back and forth the history, do it using git checkout. See the revision id using git history. If you're using Linux, use gitk to see the revision tree. In Windows, tortoise git can display it using revision graph.

To get back to latest revision, use git checkout master. Before doing any command, always make yourself do git status. This command will display anything you need to know about current repo condition, and what action that you need to do to make it right. Before do git pull and git push, it's better to make sure that git status result is contain text working directory clean.

If you need to revert a file to it's previous revision, you can do it with git merge. Before doing it to a file, test it first with git diff. Ex: git diff rev1:rev2 filename. It will print out any different between two revision. Change in rev1 will be replaced by the changes in rev2. So to do revert, rev2 will be the older than rev1. After you satisfy with the diff result, do it with git merge, just replace diff with merge, all other parameters stay the same.

I hope this helps you. The main key is to see that your working dir is your repo. Understanding this will help you use git to it's full capability. Good luck.

like image 171
Donny Kurnia Avatar answered Sep 28 '22 21:09

Donny Kurnia