Introduction to GitHub

Recently I’ve moved to using GitHub as opposed to Git on a server. Not much difference but I thought it would be useful to create a basic flow for GitHub and share it. So, here you go!

Create a new repo

1)      Ensure an appropriate gitignore file has been created

2)     From within the local directory type ‘git init’ to initialise this as a git repo

3)     Add the files that need to be committed with ‘git add’

4)     Commit these files with an appropriate message – ‘git commit –m “Initial commit of this repository”

5)     From within the github website, create a new repository

6)     Copy the remote repository URL from ‘Quick Setup’

7)     From the command line enter, ‘git remote add origin <copied remote repository URL>” – This sets the remote URL

8)     “git remote –v” can then be used to verify the new remote URL

9)     PUSH your changes with ‘git push origin master’

Clone an Existing Repo

1)      Create a local directory on your computer

2)     From within that directory enter ‘git clone https://githubcom/username/repo.git .’ – to clone the directory

Checkout a different remote branch

1)      After having cloned the project.

2)     “git fetch”

3)      “git branch -a” – Displays a list of all branches

4)     “git checkout <branchname>

5)     “git branch -a” – This can be used as a validation to show that you’re now on the expected branch.

Create a new Branch

1)      It’s not safe to work on the main branch, create your own working branch. Create one each time you have a block of work that you need to do

2)     “git checkout –b dev-my-branch-name”

Adding and Committing files

1)      Modify some code.

2)     You will then want to ‘add’ the files to the staging area, ‘git add .’ – to add all files (or ‘git add <filename>’ for specific ones)

3)     ‘git status’ will then show you which of your files are added to the staging area.

4)     To reset a file use ‘git rest HEAD <filename>’ – This removes your file from the staging area but does not delete or modify your file.

5)     A chunk of files that you have to fix a small piece of code is a commit. Once you’ve done some work you’re happy with you can commit.

6)     ‘git commit –m “Some great and useful message”’ is used to create a LOCAL commit. You can then continue working if you want or move onto pushing.

Pushing to the remote repo

1)      Type ‘git push’ it will fail as you haven’t specified where the file should go… i.e. the remote branch.

2)     Follow the prompt to push to the appropriate repo, i.e. master if you have cloned directly, branch if you created a branch.

3)     Type ‘git push – -set-upstream origin master’ (Note.. that’s 2 dashes before the word set) (FOR MASTER)

4)     Type ‘git push – -set-upstream origin the-name-of-my-branch’ (Note.. that’s 2 dashes before the word set) (FOR BRANCH)

5)     Enter your username and personal authentication key

Merging from the branch back to the main branch

1)      Eventually you will hopefully want to merge code from the repo to the main branch. This can be done with git merge but ingithub, ‘pull’ requests are used. This is where you ‘request’ the main branch <owner> to pull your code into the main repo.

2)     From within the github website, locate your branch and create a new ‘pull request’

3)     Add a detailed description. Use @username to specify an individual.

4)     Review the code from within github’s UI.

5)     Add comments etc… (LGTM if it’s all good.. )

6)     The main branch owner can then ‘Merge Pull Request’ from within github’s UI to integrate these changes.

(NOTE.. this completely ignores the testing phase which would occur between pushing the repo and merging back from the main branch.

Generally you should add TestScripts for any code that you write and commit these with any new files.)