Introduction
There are multiple strategies for working with git and GitHub and each has it’s own set of strengths and weaknesses. What strategy you choose will depend upon your own specific circumstances and needs. The processes shown in this cheat sheet are not the only way of working with git, nor are they necessarily the best way. They are just what seems to work best for me.
For context I should explain that I am a sole developer who rarely needs to collaborate, but who does use multiple machines. So I rarely have to manage conflicting changes. My primary requirements are:
- Version control of my projects
- Get common access to my repositories from multiple machines and locations
- Ensure that my repositories survive no matter what happens to my local machines
- Git installation and setup
- Useful .gitignore templates
- Add an existing local repository to your GitHub account
- Clone a GitHub repo to local machine
- Save changes to local repo
- Create a GitHub repo from local repo
- Push local changes to GtHub
- Pull GitHub changes into local repo
- Create a new local git branch
- Merge changes from one branch into another
Clone a GitHub repo to a local machine
First open up a web browser and navigate to the GitHub repository that you wish to clone. At the top right of the list of folders and files is a drop-down button (rendered green on my system) labled “<> Code”. Clicking the button drops down a dialogue from which the appropriate URL can be copied. Note that there are three different protocols shown. Choose the one most appropriate for your setup. Typically this will be either HTML or SSH. Copy the appropriate URL to your clipboard.
Now open up a bash or command prompt window (depending upon which OS you are using) and cd into the directory where you want the repository sub-directory to be added.
enter the following commands (replacing “<path_to_repo_parent_dir>” with the location you want to add the repository and “<URL>” with the copied URL:
cd <path_to_repo_parent_dir>
git clone <URL>
After a while (depending how large the repository is and how slow your internet connection is) the complete repository will have been cloned into a sub-directory of the same name as the repository that you cloned.
Save changes to local repo
This is a two-step process. First you stage the changed, deleted and new that you wish to commit; then you actually commit them. In practice I find that it is beneficial to check the status of the repository both before and after a commit sequence. So my preferred process looks like this:
git status
git add file1 file2 file3 etc
git commit -m "Summary of what the changes achieve"
git status
The first git status lets me see a list of all the changes that have been made since my last commit so that I can decide how to group my commits.
The git add adds the file that I want to commit together with a common commit message to the staging area. You can think of the staging area as a waiting room or changes that you want to group together. In my example I have explicitly named each changed file, but if you wish to add all of the changes to your commit, then there is a short-hand command “git add .” Note the space then dot which tells git to stage all changes.
I then do a git commit to commit those changes to the repository along with a short explanation of what the changes achieve as opposed to how the code changed (that can be found from a diff if it is needed later). I also tend to prefix my commit comments with a keyword such as “FEATURE: “, “BUG FIX:“, “REFACTOR:” this has a tendency to both simplify my comments and make it easier to find which commit was responsible for a particular change.
Finally I do another git status to check that I have not missed any changes. If I have i just repeat the above process.
Push local changes to GitHub
This command assumes that your local repo is already associated with an existing GitHub repo. If that is not the case then see here before continuing.
Very simple. First of all save any changes to your local repo then:
git pull
git push
The git pull ensures that you are in sync with any changes committed by other users or from other machines while you were working on this machine.
Then the git push merges your local changes into the remote GitHub repo.
Pull GitHub changes into local repo
git pull
Create a new local git branch
First of all make sure that you commit any unsaved changes to your existing branch then:
git checkout -c new_branch_name
The new branch will be created as a clone of your existing branch and then you will be switched on to the new branch.
If you also want the new branch to appear on your associated remote GitHub repo, then you also need to issue the following command too:
git push -u origin new_branch_name
When you do this git will automatically setup tracking for you, so future pushes can be done using the normal shorter:
git push
Merge changes from one branch to another
First make sure that you don’t have any unsaved changes on either branch. Then switch to the branch that you want to merge into and issue the merge command. Here it is assumed that the target branch is “master” and that the branch to be merged into master is “hotfix“. Replace those names with your own relevant branch names in the code below:
git checkout master
git merge hotfix
and that’s all there is to it.