I have a student wishing to show the incremental progress of his web project in a presentation next week. We use Git, typically accessed via IntelliJ, though the command line is fine too.
This student intends to deploy his multiple project versions in three folders so that each version can be accessed with URLs like:
http://<IP Address>/V1/index.html
http://<IP Address>/V2/index.html
etc.
After trying several things that seemed like they should do the job, we ended up using a cumbersome process creating new temp projects and checking out historical versions in order to get complete historical version builds.
For next time, and to help me and my student better learn Git... Is there not an easier way to checkout a prior version without rolling back or creating new branches, etc? Is there a way to just say "Export me a copy of the code as of commit #37"?
Thanks in advance for any help.
CodePudding user response:
For standard commit actions:
If you want to get back to commit X, you can write git checkout commitSha
.
Where commitSha is represented by number like: 3a38e644314f73d4082a00dc29de39e9d3ae3357
, you can always revert this by using git reset --hard
[which resets your history].
You can also use git reset which changes your history with git reset --hard commitSha
, you can also go back X amount of commits to the past with git reset --hard HEAD~1
to update your history you can use git pull
Here are 2 links with bit more info about those 2 commands, which go with bit more details, I hope this is the answer you are looking for:
https://www.atlassian.com/git/tutorials/undoing-changes/git-reset
https://www.atlassian.com/git/tutorials/using-branches/git-checkout
EDIT [releases]:
Probably the thing you need are releases, they can hold your snapshots of the app that you're making, making all versions easily accessible, bit more info here
https://docs.github.com/en/repositories/releasing-projects-on-github/about-releases
https://docs.gitlab.com/ee/user/project/releases/
CodePudding user response:
Wersjon's answer has correct info, but I wanted to simplify for this specific case where the intent is not to undo or rollback. And where creating branches or releases is not desired. For when you just want a snapshot of your older build/code:
TL;DR
- If you have uncommitted local changes before starting this, you may lose work. Probably wise to commit everything before starting.
- Don't use IntelliJ's "Show History" for this. Instead, use the list of commits in the Git Log.
- Right-click and check out the commit you want to get a snapshot of.
- Copy out the files you need.
- Then right-click again on the latest commit and checkout your main branch to get back to the present.
More Details: In IntelliJ IDEA, the "Git Log" and "History" features present a similar list of project commits. However they have different contextual functionality. If you have accessed the list of commits via "History", there is no "Checkout" contextual option. However there is a "Get" option which does way more than you want if you are just after a snapshot of a historical version. If you just want a snapshot of your code, don't use "History" and don't use "Get".
From the contextual menu (usually right-click) of any commit in IntelliJ's view of the Git Log however, you can choose "Check out" to get at prior versions of your work without it being interpreted as a rollback or re-branching, etc.