A wholly non-comprehensive guide to quick starting Git + GitHub.
Git is a CLI (Command Line Interface)-based local version control program used with the ubiquitous remote server warehouse GitHub. Note that Git can also be used standalone for local version control (not just with GitHub), but is most often used with GitHub for collaboration and cloud storage.
Setting up Git
Follow the instructions here to briefly configure your preferences.
A Note on Workflows
- Git doesn’t like empty directories. If you like to create your file trees in advance, put an empty text file in each so Git recognizes those directories when you add and commit them.
- No matter which way you look at it, you always have to create an empty remote repo on github before you can hook it up to your local project directory. In most tutorials they do the local stuff first, then switch to the remote repo creation, and then switch back to local to finish up - but I think that’s confusing. So we’re going to do the remote step first and then do all the local stuff after.
Before Anything Else
There are two commands that I use constantly in git. They are
git status and
git log --oneline. I use
git status almost after every step to make sure the changes I made were what I wanted them to be. Use them often so you don’t get lost like I do.
Local Git Workflow
- Create a new repo on Github using the Github GUI. Don’t add any files to it yet!
- Create local directories and files
- Navigate to project directory in Terminal and then
git init—- (to add git service to your project folder)
ls -lato make sure your invisible
.gitfile was created. If it was, then you now have git version control for all elements in your project folder.
git add <filename>—- (adds changes to middling “stage” before “committing”), or more useful,
git add -Ato stage all.
git commit -m “type descriptive message here”—- (commits save a version of changes with a message that can be referred back to later); also takes argument
git commit -am “message”, to commit all staged changes.
Remote Git Workflow (Connecting Git + Github)
- In your terminal, navigate to the empty github repo we created in the first step. Copy the URL.
git remote add <declare alias for remote repo, usually “origin”> <insert URL of remote github repo>—- (adds a remote repo). Type git remote to see which remotes are remembered.
git push <declared alias for remote repo, usually “origin”> <insert branch, “master” for main branch>. If you don’t remember what branch you are in, type in
git branchand it will show you.
- Refresh your github repo and it should reflect the new contents you pushed to it.
Now we’re all setup! If you are working alone, you can just repeat the local workflow for each new change (
git add -A, git commit -am “message”) and then push it to your remote github repo (
git push <alias, usually “origin”> <branch>) to save the commit “in the cloud” so to speak. However, if you are collaborating with other developers that are pushing and pulling from the same remote github repo, it’s a good idea to
git pull before you start work each time and before you push anything. This updates your local repo with changes made by other people to the shared remote repo. More information on how to work with git remotes can be found here.
git fetch= “Fetch branches and/or tags (collectively, “refs”) from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated (see the description of
below for ways to control this behavior).”
git pullto keep local repo up to date after remote repo connection has been established. Before continuing on project each session, always
git pullfirst to get the most recent updates. General rule of thumb is to always pull before push. And always pull often!
git merge= “Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used by
git pullto incorporate changes from another repository and can be used by hand to merge changes from one branch into another”
- References to “HEAD” basically mean the most recently checked out directory that Git is focused on.
man <command>- manual
- when in a man, navigate with f (forward), b (back), q (quit)
git status- gives you the status of added files, commits, etc. I use this after every other command to check things worked as expected.
git log- shows a full history of repo. Use
git log -—onelineto show condensed one line version. Use
—authorargument to see by author. Use
—since=YYYY-MM-DDto set parameters on history.
—until=YYYY-MM-DDworks the same as well.
git diff- shows difference between old version and new versions of files.
git branch- shows what branch you are on and what branches are available.
git branch <name new branch>- creates new branch with declared name
git checkout <name of branch>- checks out git version-controlled branches. Will also update HEAD to set the specified branch as the current branch.
git show HEAD- shows where the head is currently pointed.
git reset HEAD <name of staged file>- unstages added files. Use
git statusto see names of staged files.
git mv <first file> <new name of first file>- Git uses mv command to effectively change the name of a file.
One More Thing
Besides using Git for just your own projects, you can use Github to get involved with the open source community. Since all repos you can see on Github are public (there’s a private option, but you have to be a contributor to see them), you can “fork” them and make edits to the code. If you think your code fixes or improves upon the original, you can submit a “PR”, or pull request, to the author and they will decide whether to include it in the project or not.
forka project - “A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project. Most commonly, forks are used to either propose changes to someone else’s project or to use someone else’s project as a starting point for your own idea.”