Git & GitHub
Jargons
-
Repository: A collection of files tracked by Git. For example, the files that make up the content of this website is kept in a Git repository.
-
Remote: Any version of a repository that is not stored locally on a device is called a "remote". (So, GitHub is a service for you to host remote repositories). "Origin" is used to refer to the "remote" from which the local repository was originally downloaded from.
-
Commit: Git does not save any changes made to the files within your repository until you "commit" it. So, as a verb, it is the action of storing a new snapshot of the repository's state in the Git history. When "commit" is used as a noun, it refers to a single point in the Git history.
-
Staging: Let's explain this one with an example; assume you made changes to 4 files within your repository, but you only want to commit 2 of them because the other 2 are buggy or not complete yet. How do you commit only 2? Well, you put them in the "staging area" after which you commit. So, staging a file means that you have marked it for a commit.
Summary of useful commands
git init
: create an empty Git repository in a directory.git add <filename(s)>
: add files to the staging area to be included in the next commitgit add .
: adds all files
git commit -m "message"
: take a snapshot of the repository and save it with a message about the changesgit commit -am "message"
: add files and commit changes all in one
git status
: print what is currently going on with the repositorygit log
: print a history of all the commits that have been madegit log --pretty=oneline
: list commit history in a compact format
git diff <commit> <commit>
: show the changes made between the two commits (identified by their IDs)git checkout <commit>
: revert the repository back to a given commit. Use it if you want to discard changes to un-staged file/s.git reset --hard <commit>
: reset the repository to a given commit. Use it if you want to undo staging of a modified file.git clone <url>
: take a repository stored on a server (like GitHub) and downloads itgit clone <url> folder-name
: clone tofolder-name
git clone -b <branch> <url>
: clone a specific branch
git push
: push any local changes (commits) to a remote server- push only after you staged and committed the changes
git fetch
: download all of the latest commits from a remote to a local devicegit pull
: pull any remote changes from a remote server to a local computergit branch
: list all the branches currently in a repositorygit branch <name>
: create a new branch calledname
git checkout <name>
: switch current working branch toname
git merge <name>
: merge branchname
into current working branch (normallymaster
)git merge origin/master
: mergeorigin/master
, which is the remote version of a repository normally downloaded withgit fetch
, into the local, preexistingmaster
branchgit remote set-url origin https://github.com/USERNAME/REPOSITORY.git
: changing a remote's URL
Here is a two-page PDF containing the most useful Git commands.
Learn More!
There are many resources to learn more about Git and GitHub. Check out javinpaul
's article My Favorite Free Courses to Learn Git and Github — Best of Lot.
Not a fan of the Terminal!
If you don't like working with terminal to manage your git repository, you are in luck!
- Most editors/IDEs have built in tools for working with Git/GitHub. For example, if you are using VSCode, checkout this article: Working with GitHub in VS Code.
- There are also several great software that provide a graphical user interface (GUI) for using Git/GitHub. You may want to checkout gitkaraken, sourcetree, or gittower.