I just started using git in my project . But i am still little confuse between a commit and a branch . They do the same function i think so . Can anyone explain me what the difference between them . Please explain .
4 Answers
A branch is like a road. A commit is like a milepost.
You can extend a road and add more mileposts and while on the road the location you're at can be identified by the milepost.
Think of a commit as a collection of changes. Think of a branch as a collection of all the files contained in the repository. Whenever you commit you transform what the [HEAD of the] branch looks like.
So when you branch you can think as creating a copy of the repo (in whatever state it is when you create) and referring to it by the branch name. Commits on that branch just alter the state of this copy.
A commit represents a chunk of code changes you've made. Say you touch a couple files to create a new feature and you're happy with the work, you commit those changes so git has a record of what's changed. This way you can go back to this point in time if you need to revert to an old state of the code or just reference something from earlier in the project.
A branch is where all of your related commits live. They typically exist as an offshoot of master, which generally represents your entire project. By branching off of master, you're free to experiment with new ideas, making code changes without risk of messing up master. Then, when you're happy with the changes you've made on your offshoot branch, you can then merge them into master, thereby updating your project as a whole.
You have a very complete documentation here.
Here is the documentation for the commit command
Here is the documentation about the branch command
When You start with Git, you clone a "repository", in this repository you have one current "branch". you can create many "branch".
When you want to change one or many files of your project on your branch, you want to make a "commit", it's a part of your branch...you can have many and many commit by branch...
... But read the doc, the explanation is much more successful ^_^
If you start with Git, I advise you to use the "git games" website for learn GIT.. It's the best website for learn to use git.
Instead of long explanation that everyone can find by using the - not new - search option on google or stackechange ^^ here is a prompt example:
Branches are a way to group the modifications you are working on.
A commit is snapshot of some modifications you have done.
Myproject.git:
~ branch master:
for production (you usually don't want to modify something here)
- commit: merge stuff from other branchs
- commit: merge hotfix xxx
~ branch develop:
here you work.
- commit1: add files
- commit2: optmise something
- commit3: merge a new feature
~ branch feature-newstuff
- commit: modifiy files xxx
- commit: modify files zzz
- commit: update file yyy
this is just an example. check Git workflows for more details.