A Brief Review of Git and GitHub

Git allows you to create versions of your files and therefore it is known as the “Version Control System”. The following table compares others types of systems that allow to create versions and the features they provide:

Versions Any Editor Use Offline Manual Save
Manual Yes Yes Yes
Drop Box Yes No No
Google Doc No No No
Wikipedia No No Yes
Git Yes Yes Yes
SVN Yes No Yes

Fundamental concept:

Git system comprises of the local repository and the remote repository. The local repository manages your versions in your local machine and remote repository syncs your work from local to the remote so it be shared with others. The local repository has three virtual areas – 1. working directory, 2. staging area and the 3. commit history. git add command is used to move the files from the working directory to the staging area. git commit takes the snapshot of files in the staging area and adds it to the commit history. It is recommended to make a commit after each logical change in your code. When you are ready to share your code, you run git push command to sync with the remote.

Jargons:

Repository: A collection of files
Commit: A snapshot of files

Basic Commands:

git log View the history of all commits of the current branch in a repository
git log –stat git log that shows the files changed
git log -n 5 View the 5 most recent commits of the current branch in a repository
git diff a5c d4e View the difference between the given two commits
git show d4e Compares the given commit to its parent
git clone remote-url Get the remote repository to your local. Does two things – 1. creates a local branch called “master” and 2. creates a local branch called “origin/master” which is the last known snapshot of the remote
git checkout d4e Resets the repository to the given commit

Branches:

git branch List all branch
git branch -a List all branch including the origin/master branch that is automatically created at the time of git clone
git branch branch_name Create a branch with the given branch name
git checkout branch_name Change the branch to the given branch
git log branch_name view the history of all commits in the given branch. Note: if you want to see the history of commits in a remote repository, then the command to use git log origin/master. More to come on this later, but you should know for now that “origin/master” is the local copy of the remote origin branch
git log –graph –oneline master coins View all commits of the given branches – master and coins
git branch -d coins Delete a branch. Deleting a branch makes all commits in the branch unreachable. Git garbage collector will delete the commits later
git gc Manually run Git’s garbage collector
git merge coins master Merge the branch “coins” into “master”

git push origin –delete <name of remote branch> deletes a remote branch

Remotes:

git remote Get the name of the remote
git remote add origin remote_url Add a remote to the local repository and give it a name
git remote -v List all remote names including the corresponding remote url added to the local repository
git push origin master Merges the local commits to the remote named “origin” to “master” branch
git pull origin master Merges the master branch of the remote to the master branch of the local

GitHub:

Forking a Github repository: Clone a github repository from somebody else’s account to your own account
Pull Request: It is request made by a collaborator to another collaborator with higher permission to merge its branch to the master branch. The person that receives the request usually reviews the code, sends feedback and if everything looks good fulfills the merge request.

Conflicting Collaboration:

If there are different changes in the local and remote, extra work needs to be done to resolve the conflict. Usually, it is a two-step process –
git fetch origin Merges the remote’s master branch to the local origin/master
git merge master origin/master Merges the local master branch to the local origin/master

In fact, git pull origin master runs the exact above two commands. You can use git status to see the status of your local before and after the fetch. Before the fetch, the git status will show that your branch is ahead of origin/master by certain number of commits. After the fetch, the git status command will show that your branch and the origin/master have diverged. After successful merge (the second step), the git status command will show that your branch is up-to-date with the origin/master.

Concept of fast-forward merge

If there is no conflicting change between the remote and the local, git pull command will merge the remote with the local branch by moving the label from an older commit to the most recent commit. This type of merge is called a fast-forward merge. In this type of merge, the branch to merge is the parent of the branch to merge into.

Configuration:

Cache git password: git config –global credential.helper cache
Set default editor for commit messages: git config –global core.editor “vim”
Set push default: git config –global push.default matching
Set diff format: git config –global merge.conflictstyle diff3

The above commands create a .gitconfig file in the home directory.

Leave a Reply

Your email address will not be published. Required fields are marked *