Git: From the beginning
Git is one of the most useful tools we have as developers. Since you'll use it in every single job, let's have a quick run-down of how it works.
Basic Definitions:
Repository:
The repository is the collection of all your commits, branches, and of course, your code. You have a local repository and a remote repository. There are several different providers for remote repositories (GitHub, Bitbucket, etc).
When you work with a team everyone has their own local repository which is an exact copy (meaning all the branches and everyone’s commits) of the remote repository. This means that when you work on a branch on your local repository, your changes will be on that same branch when you push to the remote repository along with all your commit messages.
Everyone makes their changes on their local repository and then pushes to the remote for others to be able to pull their changes.
Commit:
A commit is a certain stage in your code. When you make a significant change you commit your changes and write what you did. When you commit you are committing in your local repository.
Branch:
A branch is a group of commits. The reason why you create a different branch is so you can experiment without damaging the master branch. The master branch is usually used for production or release ready code so any features should be added in a different branch and then merged.
Pulling:
Pulling is syncing your local repository with the remote repository. This means that you will have everyone else’s changes plus any new branches that have been created.
Pushing:
Pushing is the process of syncing the remote repository with your local repository. This means that all your changes and branches will be on the remote for everyone else to access.
Merging:
Merging is the process of merging the code from one branch with the code from another branch. Imagine it like putting two pieces of a puzzle together. When you merge you could run into merge conflicts if for example 2 people changed the same line of code. This requires whoever is merging the branches to fix the conflict by selecting the correct piece of code. It’s as if 2 pieces of the puzzle don’t fit but you cut the correct hole to make them fit.
Cloning:
Cloning is simply the initial pull you make on a repository. When you don’t yet have a local version of a repository on your machine, you have to clone it in order to get one. Cloning will simply copy all the files and git history from the remote to your machine.
Basic Workflow:
Now that we understand the basic definitions, the basic git workflow is quite simple.
You start by cloning the remote repository. If you are not working on the master branch you switch to whatever branch you’re working on with git checkout and start writing and committing your code.
Once you’re satisfied with the code you’ve written you proceed to push your changes to the remote repository. However before you push you should always make sure to pull from the remote in order to get any changes anyone else may have made. This ensures your changes don’t conflict.
After you have tested your code and deemed it ready, it is time to merge it with the master branch, or any other branch you might need to merge it into. And that’s it!