Do I need to commit in Git, when should I do this, and how can I revert to old versions?
I’ve been following a tutorial on Git and I’m a bit confused about a few specific issues. The main reason I need to use version control right now is simply to access previous versions of my project if I make a mistake and don’t know how to get rid of an error.
Is it true that a version is only saved in Git if you executed a commit statement? For some reason, I was thinking that every time you make any change in your project it would automatically be added as a version in your repository for you to go back to.
- How to configure Git post commit hook
- Git: Merge a Commit into a different Branch
- Git commit directory in pycharm using keyboard shortcut
- svn doesn't commit .o files, but why?
- How to cancel commits on master and move them to branch
- Allow two users to commit with their usernames on the same machine
So when should you execute commit? Is it only when you think you’ve made a good bit of progress?
How do you actually revert to a previous version in Git? i.e. what’s the command?
4 Solutions collect form web for “Do I need to commit in Git, when should I do this, and how can I revert to old versions?”
1. With Git (same with SVN, or Bazzar, or others), you need to commit so a version is saved — this way, git only keeps track of the versions you considered as good enough.
2. I tend to commit when I’ve got something that works OK — at least when working on the main branch.
If I’m working on a temporary branch, created specifically to develop a new feature / fix a bug, then I commit pretty much whenever I want, to save the current state of my work.
3. You should take a look at the following page : Undoing in Git – Reset, Checkout and Revert
- Yes, you have to commit to persist a change. This is true in all VCSes though, not just Git.
- You should commit when you have a version of your code that builds and runs. It should be something you would want to be able to undo to. It’s unlikely that this is true on every save. I find I usually commit between 4 and 12 times a day, but it can be more frequent if I’m making a lot of small improvements.
- Yes, only when you run a commit.
- You should commit as often as you need to\want to. Whenever you finish something useful, before you change something your worried might not work out, or a point were you want to make sure you can come back to. Pushing those changes to others is a different matter. Only ever push things that are at least mostly working and stable.
- If you have a choice, and need file based version tracking you might want to use Subversion (SVN). Otherwise use
git checkout [commit-ref] [filename].
[commit-ref]is the hash value of the version you want.
Yes. But read on…
With git specifically, it’s a good idea to commit every time you think you have made one self-contained logical change. So this should happen quite a lot — typically commit between once in 5-30 minutes. For examples, just clone the git repo for git, and look at the work of some of the more experienced people.
In many other vcs:es, where your commits are immediately visible to other people, it’s often considered good practise to only commit when it builds, or when you have a complete feature. In git, this level of granularity is handled by branches, and someone viewing your commit log should be able to see not just what you did, but how you did it, eg:
- “new branch for feature frobnication”
- “commit: foos now can be frobnicated”
- “commit: baz now checks if foos it works on are being frobnicated”
- “commit: bar now can frobnicate foos”
- “commit: tests for frobnication”
This makes tools that look back on the source much more useful for debugging. You should certainly try to avoid single commits that change 100+ lines — they turn work you have done on the source into opaque blocks that are hard to understand.