DEV Community

Cover image for How to move forward and backward between commits in git
Shivam Singhal for Itsopensource

Posted on • Originally published at itsopensource.com

7

How to move forward and backward between commits in git

We always keep moving forward and backward between commits in git. Once you checked out a previous hash git log no more shows the next commits, we end up rebasing or resetting, but git provides a way to see all the commits, and we can checkout the next commits too from a previous state.

The simple and easiest way to do this is:



git log --online --all


Enter fullscreen mode Exit fullscreen mode

Consider this example:

Alt Text

Here if we check out to commit id 95613ab Fix more TOC links and then see the git history with git log or git log --oneline you will only see:

Alt Text

As we see here we missed the commits ahead of 95613ab. You can see the HEAD with git show-ref --head but it will not show the commits in between the HEAD and the commit you checked out. So if you do git log --oneline --all you will get the whole history with the commit where the HEAD is right now.

Alt Text

Let us know if you know the better solution for this problem.

Cheers.

Neon image

Resources for building AI applications with Neon Postgres 🤖

Core concepts, starter applications, framework integrations, and deployment guides. Use these resources to build applications like RAG chatbots, semantic search engines, or custom AI tools.

Explore AI Tools →

Top comments (2)

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Thanks for sharing.

I will look at using --all. I usually use the branch references as below. and the order doesn't matter and more branch names could be added.

git log master my-feat
Enter fullscreen mode Exit fullscreen mode

It doesn't matter which one is latest, they will both show. This is useful if I am on my-feat and it is behind master and want to see commits on master and the feat.

That also works with two feat branch names. Or say master 1.2.0 for comparing tags. Especially if I accidentally tagged a feature branch so the tag doesn't show up on master.

If your master is behind the remote and you have you git fetch then you can also compare local master with remote

git log master origin/master
Enter fullscreen mode Exit fullscreen mode

Note you use online instead of oneline in the first block so needs to be fixed.

Collapse
 
michaelcurrin profile image
Michael Currin

If you want to be more precise you can use relative references

Start the log as if we were 5 commits behind.

git log HEAD~5

And you can check it out or reset.

git reset HEAD~5
git checkout HEAD~10

And reference future commits when you go back like that.

git log HEAD^10

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay