Everything you need to know about Git, from getting started to advanced commands and workflows.
Quick links:
Git is a distributed version control software. Version control is a way to save changes over time without overwriting previous versions. Being distributed means that every developer working with a Git repository has a copy of that entire repository โ every commit, every branch, every file. If you're used to working with centralized version control systems, this is a big difference!
Whether or not you've worked with version control before, there are a few things you should know before getting started with Git:
- Branches are lightweight and cheap, so it's OK to have many of them
- Git stores changes in SHA hashes, which work by compressing text files. That makes Git a very good version control system (VCS) for software programming, but not so good for binary files like images or videos.
- Git repositories can be connected, so you can work on one locally on your own machine, and connect it to a shared repository. This way, you can push and pull changes to a repository and easily collaborate with others.
The tools that make up the core Git distribution are written in C, Shell, Perl, and Tcl. You can find Git's source code on GitHub under git/git.
Version control is very important โ without it, you risk losing your work. With Git, you can make a "commit", or a save point, as often as you'd like. You can also go back to previous commits. This takes the pressure off of you while you're working. Commit often and commit early, and you'll never have that gut-sinking feeling of overwriting or losing changes.
There are many version control systems out there โ but Git has some major advantages.
Like we mentioned above, Git uses SHA compression, which makes it very fast.
Git can handle merge conflicts, which means that it's OK for multiple people to work on the same file at the same time. This opens up the world of development in a way that isn't possible with centralized version control. You have access to the entire project, and if you're working on a branch, you can do whatever you need to and know that your changes are safe.
Speaking of branches, Git offers a lot of flexibility and opportunity for collaboration with branches. By using branches, developers can make changes in a safe sandbox.
Instead of only committing code that is 100% sure to succeed, developers can commit code that might still need help. Then, they can push that code to the remote and get fast feedback from integrated tests or peer review.
Without sharing the code through branches, this would never be possible.
If you make a mistake, it's OK! Commits are immutable, meaning they can't be changed. (Note: You can change history, but it will create new replacement commits instead of editing the existing commits. More on that later!) This means that if you do make a mistake, even on an important branch, like main, it's OK. You can easily revert that change, or roll back the branch pointer to the commit where everything was fine.
The benefits of this can't be overstated. Not only does it create a safer environment for the project and code, but it fosters a development environment where developers can be braver, trusting that Git has their back.
If you're getting started with Git, a great place to learn the basic commands is the Git Cheat sheet. It's translated into many languages, open source as a part of the github/training-kit repository, and a great starting place for the fundamentals on the command line.
Some of the most important and most used commands that you'll find there are:
git clone [url]: Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits.git status: Always a good idea, this command shows you what branch you're on, what files are in the working or staging directory, and any other important information.git branch: This shows the existing branches in your local repository. You can also usegit branch [branch-name]to create a branch from your current location, orgit branch --allto see all branches, both the local ones on your machine, and the remote tracking branches stored from the lastgit pullorgit fetchfrom the remote.git checkout [branch-name]: Switches to the specified branch and updates the working directory.git add [file]: Snapshots the file in preparation for versioning, adding it to the staging area.git commit -m "descriptive message": Records file snapshots permanently in the version history.git pull: Updates your current local working branch with all new commits from the corresponding remote branch on GitHub.git pullis a combination ofgit fetchandgit merge.git push: Uploads all local branch commits to the remote.git log: Browse and inspect the evolution of project files.