Version control is the most important system for managing code bases. It is the system for recording different versions of files, usually source code files, changing over time. It allows users to track changes in code over time and helps collaborative code development. Tracking and unique identification of every change posted to a code enables users to monitor progress, mark particular states of the code and completely or selectively revert to older versions if needed.
Version control system (VCS) is a very handy software for code organization, multiple developer collaboration and code sharing. VCS allows parallel code development/testing/deployment for several programmers spread across multiple large teams. The most common version control tools used are git, cvs, mercurial and svn.
Git is one of the best and most widely used distributed version control system to handle big projects. It was developed by Linus Torvalds during the development of the Linux kernel. It is a free and open-source tool shipped natively with most of the linux distros. While locally installed git is sufficient for individual purpose, the true essence of VCS lies in remote collaboration.
INTRODUCTION
Git is a decentralized version control system where all users can have their individual versions of the code. Also, git allows offline code development while network connectivity and online VCS hosts are needed for remote collaboration. Online platforms like Github, Gitlab and Bitbucket are the most widely used ones and serve for code sharing within between the VCS framework. Git keeps variations in code over time by recording and tracking only the difference in code and not maintaining multiple code copies. All these differences are sorted and stored, as per different branches of the git tree, in a hidden .git folder inside the porject source code.
GIT CONCEPTS & TERMINOLOGY
Following are the basic concepts and git terminology for access, modification and development of the project source code.
master
branch is usually the main branch that every other individual feature
, bug-fix
or release
branch merges to.
GENERAL GIT COMMANDS
General Commands
⇒ Initialize the git control system inside the project source code folder
git init
⇒ Stage the new addtional changes completely or partially to commit to the branch
git add . // Stage all changes
git add --all // Stage changes including deleted files
git add <file_name/folder_name> // Stage all changes
⇒ Finally add staged changes to the current branch
git commit -m <message to notify commit log>
⇒ Push the new commits on a local branch to remote repository
git push origin <branch>
⇒ Fetch or obtain the updated codes in the different branches from the remote repositories
git fetch
⇒ Merge the changes from a different branch to the current branch
git merge <branch>
⇒ Pull and merge the changes made to the current branch on the remote repository from a differnt user. It is a fetch + merge
git pull origin <branch>
Pull before push is a standard protocol for collaboration. Changes to a local branch can't be merged to the same online unless the updates to the remote(made probably by a different collaborator) are absorbed in the local branch.
⇒ Clone a remote repository to the local machine
git clone <remote_git_url>
⇒ Reset the all changes and get to master branch
git reset --hard
git reset --hard <commit_id>
git reset // Undo the staging command for the currently uncommitted changes
Diagnostics
⇒ Check the difference and edits in new uncommitted and unstaged files
git diff
⇒ Get the list of branches on the local machine
git branch
⇒ Get the list of tags available
git tag
⇒ See history for commits, messages and authors
git log
⇒ List the changes, timestamps and authors for the file
git blame <file_name>
⇒ Show the changes made in a particular commit
git show <commit_id>
Branch Commands
⇒ Switch to the specified branch
git checkout <branch_name>
⇒ Create a new branch
git checkout -b <new_branch_name> <base_branch_name>
⇒ Delete a branch
git branch -d <branch_name>
⇒ Switch to a particular commit ID of the code if it exists on the branch
git checkout <commit_id>
Discard or archive uncommitted changes
⇒ Restore a particular file to the last committed version. All uncommited local changes are lost
git checkout <file_name>
⇒ Remove all new uncommitted edits and restore the project to the last commit
git stash
⇒ Archive the current uncommitted changes to stash
git stash save "<message_to_save_edit>"
⇒ Obtain the list of stashed or archived changes
git stash list
⇒ Apply last stashed set of changes to edits
git stash apply
⇒ Apply a particular set of stashed changes to the current branch
git stash apply stash@{<stash_number>} // Stash number from the list
User Credentials' Configuration
⇒ Add username for remote git service account
git config --global user.name "<github/bitbucket user_name>"
⇒ Add email ID for remote git service account
git config --global user.email "<github/bitbucket user_mail_id>"
⇒ Connect a local repository to remote git service URL
git remote add origin "<github/bitbucket repository URL>"
⇒ Set the origin/remote git service URL to current local repository
git set-url remote origin "<github/bitbucket repository URL>"
BENEFITS OF GIT
Git is one of the most sought after technical skill in the software development industry. It has made collaboration more systematic and synchronized. While this list of git commands is only the ones used most commonly, man git
can be used to learn more detailed description of git commands.
IF YOU LIKED THE ARTICLE, DON'T FORGET TO LEAVE A REACTION OR A COMMENT!