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 origin master
git checkout -b new-branch
Git fetch and work on branch
If your colleague has made some changes on a new branch and have submitted a pull request, and now you want to test his changes, you can issue the following commands.
Allows you to view the latest but doesn't integrate anything to your local working directory.
git branch -a
All branches available
git checkout new_branch
Updates the files in your working directory and allows you to work on it locally.
Other useful stuff
To replace everything in your local with the copy in remote use the below. This might be useful if you made some ad-hoc changes and now want to switch back to the remote copy in Github.
git fetch origin
git reset --hard origin/master
If you happen to ignore the file (add file to .gitignore) after it has been commited you may need to remove the file from staging.
git rm --cached filename
To see previous commits of a file use the below command, where HEAD~2 is the version 2 commits previously.
git show HEAD~2:path/to/my_file.js
See part 2 on SSH keys here.