Git and Github Primer
Posted on 2020-05-16T09:26:10Z by Kevin Foong
Here is a quick introduction to Git and Github for beginners. Say you have been developing locally and now want to upload your files to Github, you can follow these steps to create a repository and upload your files.
First create a new local Git repository for your project.
Next show all the files that have been amended since the last commit. If this is your first commit, it will show all files.
At this stage make sure that you have a .gitignore file in your project folder to ensure certain files and folders are ignored by Git and not sent to Github. Some examples may include the below. Once you have a .gitignore file, do git status again and you should see those files/folders omitted.
Now add all files shown in git status to the staging area before git commit.
git add --all
Commit all changes to your local repostiory.
git commit -a -m “description of commit”
Create a repository in Github. Note the url of the repository. Back in your local project folder issue the below command to link your local repository with the one in Github.
git remote add origin https://github.com/Kevinf7/myrepo.git
You are now ready to push all your changes to Github.
git push -u origin master
You will be prompted to enter your Github username and password. Once you’ve done that it will push all your changes to Github. Refresh your Github page and you should now see all your files there.
Contributing to an existing project and branching
If you want to contribute to an existing project in Github, you will need to use "git clone" to download the repository. This will download all files into a "myrepo" folder.
git clone https://github.com/Kevinf7/myrepo.git
It is usually a good idea to push to a branch rather than directly to the master branch. To see what branch you are on issue the below command. If you haven't done anything you will be on the master branch.
To create and checkout a new branch use "git checkout". If you "git branch" again, you will see that you are now working on "new-branch".
git checkout -b new-branch
Now just "git add" and "git commit" as per normal. Finally when you push your changes back to Github you will push from "new-branch".
git push origin new-branch
Git will provide a url to create a pull request. Something like below. Visit the url and submit a post request. Alternatively just go to the Github page and initiate a pull request. A pull request is a request for Github to merge your changes into the main branch.
You can also set up a default template in Github for each pull request which the contrbutor needs to fill in prior to submitting the pull request. This is quite helpful in ensuring each commit has some form of quality control and documentation around it. See this page for more information.
The owner of the repository will receive the pull request and if happy with the changes will merge the changes back into main branch.
If you like you can also merge your local branch with your local master branch and delete new-branch.
git checkout master
git merge new-branch
git branch -d new-branch
And before you start work again, it is a good habit to do a fresh pull every time to ensure that you have the latest version (even though you think you have the latest you never know if someone else has updated something to master), create a new branch and so on ..
git pull https://github.com/Kevinf7/myrepo.git
git checkout -b new-branch
See part 2 on SSH keys here.