Git and its Basic Commands
If you are an aspiring software engineer, you must have come across Git or some other Version Control system. While creating individual projects one usually does not feel the need of using a Version Control system; however, the perspective turns 180 degrees when working on projects that have multiple collaborators, sometimes located in different parts of the world.
Git is a Version Control system, which basically means that it keeps track of all the changes that you make to your project, allows you to create branches from the master on which you can work independently and enables you to merge your work to the master branch after proper peer review from other collaborators.
In this post, I will cover some of the basic Git commands that I have come across and that have really eased out the process of developing projects with others.
Getting started
git initThe command initialises the git repo in your project. So, first browse to your project folder through the terminal or any git client you prefer and run the command. You are now ready to start using git.
git clone <remote> <branch-name>The command pulls the complete repository branch onto your local machine. <remote> is the location where the project is hosted remotely, in the most general case it will be origin. <branch-name> is the branch you will be working on. Whenever you begin your project, the branch name would be master. Each time you work on a new feature, a new branch will be forked from the main branch. Forking a branch refers to creating a copy where you can make your own changes without directly impacting the master branch.
Managing branches
When a product is being developed, each collaborator works on their own independent branch where they make the necessary changes.
git checkout -b <new-branch-name>The git checkout -b command enables a user to create a new branch (fork a branch) from the current branch with any name the user wants which will replace <new-branch-name>.
git checkout <name>The above command performs two functions in the most basic form. The command is useful when you move from your current branch to an already existing branch on your machine with the name of the branch in place of <name>. Secondly, once you make a few changes to a file and you decide to discard them completely before a commit has been made, the same command with the <name> replaced with the file name (just the name or the path based on your current location in the project) reverts all changes done to the file.
git branchThe above command lists all the branches that are available locally on your system and highlights your current branch. This is handy when you work on a number of tasks and you need to check your current branch before you make the necessary changes or to identify the target branch you need to switch to.
git pull <remote> <branch-name>The above command lets a user get all the changes on the branch <branch-name> onto their current local branch and merge the changes of the two branches into the current branch. Such a step brings all the modifications done on the other branch and combines them recursively with the present branch and its changes. However, if the two branches are working with the same file or same code snippet, then this will lead to a merge conflict which basically means that the user must inspect the changes in both the branches and decide which changes are to be kept (choose the remote branch changes, choose the user’s own changes or keep some of the remote branch and some of user’s changes).
Storing your changes
Once you make the necessary changes, you can now make sure that they are solidified and combined with the remote main branch.
git add *The above command will take all the files where you have made changes and stage them. Staging is the middle ground between no recorded changes and the changes that will always stay recorded. If you wish to make any changes to any of the files now, you can easily unstage the files from here. The command can be made more specific by replacing the * with the actual file names. This is handy when you made changes to a large number of files but want only specific files to be staged.
git commitThis command will take all the staged files and finalise the changes such that they are recorded permanently. On execution of this command, the editor opens up allowing you to write a commit message that provides a short description to the changes made. The command can be shortened as git commit -m "Commit Message goes here" which allows the user to specify the commit message in the command itself. A user can commit as many times as needed and then push them all together to the remote branch.
git push origin <branch-name>Once one or more commits are made, the user can then push all the work to the remote branch as specified in the command above. Usually the branch name is the same as the current branch.
Making amends
While keeping track of changes is a blessing, sometimes we can make mistakes. This is when the powerful editing tools of Git come handy.
git rebase -i HEAD~2Sometimes, it’s easy to misspell a word in the commit message or add a wrong tracking number. In such a case, the rebase command enables a user to jump to a certain commit. In this case, the number represents the number of commits to jump back to which in this case is 2. The editor shows up, where you type r in front of this commit’s message after which the editor allows you to rename that message.
git cherry-pick <commit id>Git also allows a user to move a commit from one branch to another. Moving a commit means that all the changes made in that commit will automatically start reflecting in the new branch. To do so, you can get the commit id for the commit you want to move by using git log command. Move to the new branch and then simply run the above command with the commit id and all changes will start reflecting here. Yet again, this might conflict with the changes in the current branch so merge conflicts would need to be resolved.
git branch -D <branch-name>When you need to delete a branch from your local machine, the above command removes the branch from your machine irrespective of whether there are any unsaved changes in the branch or not. If we replace -D with -d, the deletion command will halt if there are some unsaved changes in the branch. You can also delete the branch from remote location by using the command below.
git push origin --delete <branch-name>Conclusion
This is not an exhaustive list of commands but some that I find quite useful in everyday work. Hope these commands prove helpful to you too.
Please feel free to share your thoughts and suggestions.
