Beginner’s Git
What is Git?
Git is a very popular Version Control system. Version control systems are used to manage software sources.
It lets you save the state of your code at different stages/versions so that you can have a ‘history’ of it, it let’s you compare changes throughout the history, have remote backups, collaborate with other people, and a lot more.
Other alternatives to git exists: Mercurial, VCS, SVN, TFS, Darcs, and more. But git is by far the most popular one so might as well learn that.
Version control system are invaluable when writing code (or any text, really!) and you should always use them! They’ll save you a lot of headaches.
How does Git work?
Git keeps track on files and directories in a directory.
When you decide that you want to start using git on the current directory, write git init.
Git will create a directory named .git in your current directory.
The files in the .git directory contains everything it knows on the directory and all of the history.
You change the content of the .git directory by interacting with the git command.
Alternatively, when you clone a repository from remote location (using git clone <url>) git will download the content of that repository
and will create a new directory with the latest state of the project. You’ll see that there’s a .git directory there too.
Workspaces
- Your ‘current workspace’ is the content of your directory.
- The staging area: git keeps a copy of the files you tell it to track. To add a file to the staging area you use
git add <file>.- If you add a file to the staging area and then change the file in your current workspace, git will remember the content that was in the file when you added it.
- If you write
git add <file>again, git will will override the file in the staging area with the content in your current workspace.
- Git snapshots / commits: Once you add files to the staging areas and you decide that you want to always remember the current state of the staging area, you create a new snapshot (also known as commit) using
git commit.
The staging area and the snapshots are stored in the .git directory.
The cool thing about snapshots / commits, is that you can see and track the changes between two commits, you can go back and see what your app was like before and return to the latest commit. It’s the history of your code!
The difference between the staging area and a snapshot
The staging area is volatile, it can be changed easily using git add, git reset, and more.
A snapshot is robust. Once you commit you are… commited! When you create a new commit you do not erase or change previous ones.
You are bound to accumulate many many commits over the course of building a program
(For example: at the time of writing the LLVM project mirror on github has 164,574 commits!).
Things you want to do
- Commit frequently - preferably with working software. You’ll be able to track your changes more easily. And revert back to an earlier commit when things go wrong.
- Write meaningful commit messages, a summary of the changes you made. This will help you when you want to look back and understand which commit added a new feature (or bug!).
- Keep your workspace clean and don’t leave untracked files for too long. Untracked files are files that were not staged or commited - git does not know anything about them. If they are important they should be commited, if not, they should be deleted or moved somewhere else! (note, it is also possible that you might want git to premanently ignore them. to do that put them on .gitignore)
- **View the status of your repository** (via
git status)
Important commands
git init- creates a new.gitdirectory in the current directory. Git can now start tracking this directory!git clone <url>- copy a remote git repositorygit add <file>- add a file or directory to the staging areagit commit- creates a new snapshot on top of the last snapshot with the changes in the staging areagit push <remote> <branch>- will upload the new commits in a branch to the remote (e.g. your github repository)<remote>is usuallyorigin<branch>is usuallymaster
git status- Shows the status of your directory with regards to the latest commit. Use this frequently!git diff- View the changes in the current workspace that are not staged.- To view the changes that are staged but not commited, use the
--stagedflag
- To view the changes that are staged but not commited, use the
More information
- How to setup git
- Saving Changes
- Undoing changes
- A simple workflow for syncing work with others
- Git - the simple guide
Or: