Table of contents
No headings in the article.
So I just started working in a corporate tech environment, and what they say is true: Github is the heart of collaborative project handling. So, knowing the basic ins and outs of this wonderful tool is essential.
But this post is not for the basic push-pull commands that we commonly used.
We face scenarios when we have to use other commands to get the job done. These cases might not occur often, but knowing them helps.
switch
A large project will always have multiple branches serving varied purposes: main branch, feature branch, release branch etc. So switching between these branches becomes necessary all the time.
git-checkout is often the preferred option. But this command is also used for many other scenarios.
Did you know there is a branch specifically for switching between branches?
References:
git switch <branch>
stash
This command helps us to stash all the committed and uncommitted changes and saves them for later use + reverts them from the working copy.
Hence, with a fresh working copy, you can make new changes: new commits, switch branches, and perform any other Git operations; then come back and re-apply your stash when you're ready. You put them back in your working copy using the git stash pop command.
git status #shows a bunch of committed + uncommitted changes git stash git status #no unstaged + staged info shown ... ... #you made a bunch of changes above and now wish to bring back your stashed changes git stash pop
References:
rebase
Often when working on a repository, when a bug crops up you may make a new branch from the main branch and work on the changes in the new branch. Later, when you are ready to merge the branch to the main, the main branch may have had its own set of commits too.
In such cases, git rebase helps to add my commits in my branch to the HEAD of the main branch as entirely new commits.
A big advantage of rebase over our normal merge command is that it makes sure that the branch I worked on is deleted and the merging of the new commits to the main branch is less messy.
But another downside is that all the history regarding the commit is overwritten by new history, which may result in a serious loss of metadata. And the chances of showing merge conflicts are also removed when using rebase.
#there are 2 branches Feature and Main with their own commits as per the diagram below git switch Feature git rebase Main #Note that we are rebasing to Main while being in Feature branch
References:
revert
This is a useful + preferred command over git reset.
This command undoes changes by adding new commits that would undo the previous commit changes. Hence maintaining the history of the previous commit + undoing commit changes.
git revert <commit-log>
References:
blame
So often while working on large codebases, it gets important to know who added a certain line or comment, to approach the person regarding the reason behind the code change. In such situations, git blame is the perfect command to be used.
Often you'll also notice git blame-like functionality being available in IDEs like IntelliJ.
References:
#find the author by line numbers
git blame -L <starting line>,<ending line> <filename>