What is Git?
Git is the most popular version control system. It keeps a record of all the changes that you make in the files and allows you to revert to specific versions if you ever need them. Further, git makes collaboration easier, faster and allows the work of multiple people to be merged into one file.
Git is useful regardless of whether you work as part of a team, or write code individually.
Git runs locally which means that your files, and their history, are continuously saved on your computer. Additionally, you can use online hosts such as GitHub to store the version of your work and its revision history. The ability to have a place on your computer where you can upload your changes and download changes from other people makes collaboration easier and more effective. By using git, two people can work on different parts of a project and merge those changes without losing each other's work!
How to use GIT and Git Repositories
Git software can be accessed through the terminal (command line), or a desktop app with a graphical user interface (GUI).
Git repo (short for repository) includes the entire revision history and project files. “Commit” is the term for each recorded change made to one, or several files. The first step before committing is staging where we tell git which files we want to commit which means that we can select the files we want to put in the staging phase. This allows us to choose only the files that we want to commit and not be forced to go through with all of them.
Online hosts are great for storing your git repo and uploading your changes. They’re also used to download other people’s changes. Online hosts are a great place for collaboration between developers. After you set up your remote repository you can upload (push) your changes and download (pull) other people’s changes on that particular repo.
Branches and Merging
Another great feature of git is the ability to branch out from the original codebase. This gives you as a developer more flexibility in your work. Let me give you an example of why branches are a great feature:
Let’s say that you are making a website and you decide to create a new feature. So, you create a new branch and start implementing it. Later you get a request from your boss to make a quick change in the website that needs to be done that same day. You can accomplish that by switching to your master branch, finishing the request, then turning back to your new branch to continue working. After finishing the new feature you can merge the new branch with the master. That way both the new feature and the request from your boss are kept!
Keep in mind that you can run into conflicts when you merge two branches (or merge a remote and local branch). This can happen when both you and your colleague work on the same part of a file at the same time. If your colleague pushes their changes to the remote repo and you try to pull them to your changed file, you will run into conflict. Fortunately, git has a way to handle these conflicts, which allows both of you to see each other's changes and decide which of them you want to keep.
Pull requests are used as a discussion place among developers or their supervisors before deciding if they want to merge those changes into the master working branch.
Let’s say that you are the supervisor for a project and one of the developers made some changes in their feature branch. Then they open the pull request wanting to merge some changes in the master branch. They will notify you by the pull request and you will be able to review these changes and decide whether you want those changes in your project
Common Git Commands
Below are some common git commands...
- git init – This will create a hidden .git folder inside your current folder, which will be the repository where git will store all of its tracking data.
- git clone <repository url> - This will download the repository from GitHub locally and extract the latest files’ latest changes. Inside <> will go to the URL of the repo that you want to download and its codebases called remote origin (the original place downloaded from).
- git status – This will show you which files have been modified recently
- git branch <new-branch-name> - This will create new branches
- git checkout <existing-branch-name> - This will checkout particular branches
- git checkout -b <new-branch-name> - This will create a new branch
- git diff <branch-name> <other-branch-name> - This will check differences between branches
- git add <files> - This will stage changes
- git add - This stages all files automatically git commit – m “Commit message” – This will show committing files and the message
- git push origin <branch-name> - This pushes your branch to upload somewhere, for example: GitHub
- git fetch – This will fetch the latest info about the repo
- git merge <other-branch-name> - This merges changes from somebody else
- git pull origin master – This pulls changes
Understanding git is the first step towards success in web development. Git is essential when it comes to collaborating with your colleagues, partners, managers, or tracking your personal projects as they grow. Using the above, you should become a master of git in no time.