Understanding git as Version Control System
By Diwanshu Shekhar
- 5 minutes read - 962 wordsGit 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.
Basic Commands
View the history of all commits of the current branch in a repository
git log
Logs that show the files changed
git log --stat
View the 5 most recent commits of the current branch in a repository
git log -n 5
View the difference between the given two commits
git diff a5c d4e
Compares the given commit to its parent
git show d4e
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 clone remote-url
Resets the repository to the given commit
git checkout d4e
Branches
List all branch
git branch
List all branch including the origin/master branch that is automatically created at the time of git clone
git branch -a
Create a branch with the given branch name
git branch <branch_name>
Change the branch to the given branch
git checkout 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 master branch
git log branch_name
View all commits of the given branches - master and coins
git log --graph --oneline master coins
Delete a branch. Deleting a branch makes all commits in the branch unreachable. Git garbage collector will delete the commits later
git branch -d coins
Run Git’s garbage collector manually
git gc
Merge the branch “coins” into “master”
git merge coins master
Deletes a remote branch
git push origin --delete <name of remote branch>
Remotes
Get the name of the remote
git remote
Add a remote to the local repository and give it a name
git remote add origin remote_url
List all remote names including the corresponding remote url added to the local repository
git remote -v
Merges the local commits to the remote named “origin” to “master” branch
git push origin master
Merges the master branch of the remote to the master branch of the local
git pull origin master
GitHub Jargons
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 command 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.
Advanced commands
Cherry pick all commits from a merge commit of a branch into a branch
git checkout release-branch
git cherry-pick -m 1 merge-commit-hash
git push origin release-branch
Cherry-picking certain commits into a branch
git checkout release-branch
git cherry-pick xyzabc123
git push origin release-branch
Undoing change from a merged PR from a branch
# Checkout the branch where the PR was merged and run the following command
git revert -m 1 commit_id_of_merge_pr
git revert
makes new commits to remove old commits in a way while keeping the commit history. This is the recommended way to undo a commit. There is also another command called git reset
, but this doesn’t keep the history of your commits.
Undoing change to a particular commit
git revert --no-commit commit_sha..HEAD
Push reference of one branch to another
git push origin master:production
List commit logs with reference
git reflog