I assume you all know what Git is, if not, please make sure you read & understand about Git.


Repositories:

Every project has its own repository which we call upstream.

When a new developer joins the team and starts to work on an existing project, he or she will need to make a fork of the upstream repository. This fork is a copy of the original repository. This new repository is called origin.


Difference between upstream & origin:

upstream is the original repository, while the origin is a copy.


After creating your fork, you will have to clone the repository (the origin) on your local machine. 

git clone [your repo URL]


Once you have the origin locally, add the upstream also:

git remote add upstream [upstream repo URL]


To see the result, run git remote -v. Both origin and upstream should be listed.


Type of Branches:

Generally their should be 3 types of branches: 

1. master - has the code that is currently running in production.

2. release - gathers all the code ready to be merged into master.

3. development - has the code owned by developer. (There can be multiple development branches as per the requirement).


You have to create a new branch every time from the current release, for every new feature or new task. So you have to make sure that your release branch is up-to-date with the latest code.

git checkout release
git pull upstream release
git push origin release
git checkout -b [branch name]


Workflow:

Please DO NOT commit your code to your branch until you have a stable & working code.


git add [file to be committed]
git commit -m "commit message"
git push origin [branch name]


NOTE: Please commit as often as possible.


Please check first what is to be committed, before adding the files. 

You can check your code by running the git status and git diff commands.


After checking the code, add each file individually (or group of files) to the commit. 

Please avoid adding everything to the commit because this will often lead to unwanted changes to be committed and your code will fail.

When you're done, push the branch on the upstream repo so others can access it.

git push upstream [branch name]


After this, create a pull request from this new branch to the upstream release branch.


If everything is ok, then merged the code into the release branch to test it with other changes made by your team when working on different tasks. 


When everything is ready to be deployed, the release branch (the upstream release) will be merged into the master and the code will end up on the production server.



Thanks..!!