Finding a previous version of your code on GitHub
GitHub stores every version of your code automatically when you commit changes. To see what your code looked like at any point in the past, you look at the commit history — a record of each change someone made, when they made it, and why. You can view this history directly in GitHub's web interface without needing any special tools.
The fastest way is to go to your repository on GitHub, click the clock icon or "History" button above your code files, and you'll see a list of all commits in reverse order (newest first). Click any commit to see exactly what changed in that version.
Key Takeaways
- Every commit in GitHub's history shows you the exact state of your code at that moment, along with who made the change and when.
- You can view a previous version by clicking on a commit in the history, or by using the commit's unique ID (hash) if you know it.
- The diff view shows you line-by-line what changed between two versions, making it easy to spot what was added, removed, or modified.
- You can restore your working code to a previous version using Git commands, but viewing and comparing versions requires only the GitHub website.
Viewing commit history in the GitHub web interface
Open your repository on GitHub and navigate to the branch you want to check. Click the "Commits" link near the top of the file list — it usually shows a number like "42 commits" or sits in the toolbar above your files. This takes you to the full commit history for that branch.
Each row shows one commit with the commit message (what the person who made the change wrote to describe it), the author's name, when it happened, and a shortened version of the commit ID. The most recent commit appears at the top. Scroll down to see older commits, or use the search box to find a commit by message or author name.
Viewing the code as it was in a specific commit
Click on any commit in the history to open it. You'll see a detailed view showing every file that changed in that commit, with the additions highlighted in green and deletions in red. This is called the diff view. At the top of the page, you'll see the commit ID (a long string of letters and numbers), the author, the date, and the commit message.
To see the entire state of your repository at that point — not just the files that changed — click the "Browse the repository" link or the code icon that appears near the commit ID. This shows you all your files and folders as they existed in that version, and you can click into any file to read its full contents.
Comparing two different versions
If you want to see what changed between two specific commits, you can compare them directly. In the commit history, click on an older commit, then look for a button or link that says "Compare" or shows two commits side by side. Alternatively, you can manually build a comparison URL: go to your repository, click "Commits," then click the three-dot menu next to a commit and select "Compare."
The comparison view shows you every file that differs between the two versions, with additions in green and deletions in red. This is useful when you're trying to understand what happened between two points in time, or when you're reviewing someone else's work before merging it into your main branch.
Finding a specific commit by message or date
If you remember roughly when a change was made or what the commit message said, use the search box at the top of the commit history. Type a keyword from the message, a file name, or an author's name, and GitHub filters the list to show only matching commits. You can also sort by date or author using the filter options that appear above the list.
If you know the exact commit ID (the long hash), you can jump directly to it by adding it to your repository URL. For example, if your repository is at github.com/username/repo, you can go to github.com/username/repo/commit/[commit-id] to view that specific commit instantly.
Understanding commit IDs and what they mean
Each commit has two IDs: a long one (the full SHA-1 hash, usually 40 characters) and a short one (typically 7 characters) that GitHub shows in the interface. Both refer to the same commit. The ID is a unique fingerprint of that version — it's generated based on the exact contents of the code and metadata, so the same code will always produce the same ID.
You'll see these IDs in the commit history, in pull requests, and in error messages. When someone tells you "check out commit abc1234," they're giving you the short ID. You can use either the short or long ID in most places, and GitHub will understand which commit you mean.
Restoring your code to a previous version
Viewing a previous version in GitHub doesn't change your actual code — it just shows you what it looked like. If you want to actually restore your working files to a previous state, you need to use Git commands on your computer. The most common approach is to create a new commit that undoes the changes from an older commit, rather than erasing history.
You can also use the "Revert" button that appears on a commit's detail page in GitHub. This creates a new commit that reverses the changes from the commit you selected, keeping the full history intact. This is safer than deleting commits, especially if other people are working on the same repository.
Frequently Asked Questions
Can I see who changed a specific line of code?
Yes. Open the file you want to check, click the "Blame" button at the top, and GitHub shows you which commit changed each line and who made that commit. Hover over a line to see the commit message and date. This is useful when you're trying to understand why a particular piece of code exists.
What if I accidentally deleted a file — can I get it back from the history?
Yes. Go to the commit history, find the last commit where the file existed, click "Browse the repository," and you'll see the file in that version. You can view it, copy its contents, or use Git commands to restore it to your current branch. GitHub keeps all deleted files in the history indefinitely.
Do I need to download anything to view previous versions?
No. You can view and compare all previous versions directly on the GitHub website using only your web browser. You only need Git or other tools if you want to restore your actual working code to a previous state, rather than just looking at it.
Can I see the history of a specific file instead of the whole repository?
Yes. Click on the file name in your repository, then click the "History" button (usually a clock icon) at the top right of the file view. This shows you only the commits that changed that particular file, making it easier to track its evolution.
What's the difference between a commit and a branch?
A commit is a single saved version of your code at one moment in time. A branch is a separate line of development that contains multiple commits. You can have many branches, each with its own history of commits. The commit history shows you the sequence of changes within one branch.