Git Gud
What is Git?
Git is a version control system (commonly, VCS). A VCS is just a way to keep track of your changes in something. Git is a tool that developers often use to track the history of files. Throughout your codebase, you may have a ton of files and manually keeping track of them is not feasible. It is not unlikely to accidentially delete a file that you didn't mean to delete or run a script and completely change the code or data that you didn't mean to.
This is where git shines. Git is a command-line tool that allows you to use commands to keep track of the files you are working on. There are a plethora of git commands but there are only a handful to get familiar with to get you 90% of the way through.
What is GitHub?
GitHub is just a website that allows you to use git and store your files and history on there. It offers other stuff, but that's outside of the scope of this post.
Basic Git Commands
There are a few very important git commands to know.
- git clone
- git add
- git commit
- git pull
- git push
Clone
The git clone
command let's you clone a repository (commonly, repo). All that means is that, there is some code at a link and you copy it down to your local machine.
A repository is just a collection of files hosted on GitHub or something similar.
On GitHub there is a green download button that let's you choose how to clone the repo and gives you the command to copy and paste to do so. You only need to do this once. When you clone a repo, the repo that lives on the server (often GitHub) is referred to as the "remote" or "remote repo", whereas the copy that you hvae is often referred to as the "local" or "local repo".
Add
The git add
command let's you add files to the staging area. The staging area is essentially the state in which you have files that you are going to commit and push. You can do the following to add a file to the staging area.
1git add filename.ext
I like to use the -p
flag to also do a short review of what changes in the files are going to be added. For example, if I change lines 24 to 52 and also 64 to 70 but I only want to track the changes in the lines 24 to 52, I can do so using -p
. By default, git add
will stage the entire file.
Commit
After you have added files to the staging area, you can now commit them with a message. The message is necessary and you add a message to a commit using the -m
flag.
Example:
1git add file1.txt 2git commit -m "Added file1.txt"
At this point, you have committed the changes and the state of those files are saved and can be referenced by the hash you see from the output of the commit command. It should look something like this:
1git commit -m "Added git article -- WIP" 2 3[main d8146d8] Added git article -- WIP 4 1 file changed, 49 insertions(+) 5 create mode 100644 src/posts/git-gud.md
This is a real commit that I just made for this post. The hash for this commit is d8146d8
. This hash is useful if there are changes you make later and want to come back to a previous state, you can do so using this hash.
Pull
The git pull
command lets you pull changes from the remote repo to your local repo. This is something that you will often need to do if there are changes that happen on the remote repo that you likely didn't make and need to get before you push (e.g. a team member makes changes and pushs them).
Push
The git push
command lets you push changes from your local repo to the remote repo. This is what you do after you have have a commmit. The changes that you made are described in your commit and when you push, others can see those changes on GitHub or when they do a pull. You cannot push before committing.
Status
The git status
command is useful to understand the state your repo is in. Running this command will output information like this:
1On branch main 2Your branch is ahead of 'origin/main' by 1 commit. 3 (use "git push" to publish your local commits) 4 5Changes not staged for commit: 6 (use "git add <file>..." to update what will be committed) 7 (use "git restore <file>..." to discard changes in working directory) 8 modified: src/posts/git-gud.md 9 10Untracked files: 11 (use "git add <file>..." to include in what will be committed) 12 src/posts/docker-swarm.md 13 14no changes added to commit (use "git add" and/or "git commit -a")
This output tells me that I have a commit that I made but haven't pushed, I have changes in my src/posts/git-gud.md
file (this post), and that my src/posts/docker-swarm.md
file is not tracked. Files that aren't tracked are permanently lost when you delete them (unless you use a file explorer and delete them, in which case it will likely be in your Trash or Recylcing Bin).