Handy Git Commands To Boost Your Development Productivity

I thought I'd put together handy Git commands that I normally use on regular basis. I hope this will help you be more productive in developing your app or managing your codebases.

If you want to create a clean copy of a repository,

 `git clone <LinkToGitHubRepo>`, creates a local copy of the repository

 `git pull`, fetches and merges changes from the remote branch to local branch

To learn more about the branches in the repository,

 `git branch --all`, views all local and remote branches

 `git branch -vv`, checks the local branch's upstream

If you want to create / delete a remote branch to save, share, and collaborate with other developers,

  1. `git checkout -b <NameOfBranch>`, creates the local branch
  2. Commit the changes to local branch
  3. To create a new remote branch, `git push origin <NameOfLocalBranch>:<NameOfRemoteBranch>` OR `git push origin <NameOfBranch>`, if you want the Local and Remote to be the same, use the latter e.g. `git push origin TestBranch`
  4. To delete the remote branch `git push origin :<NameOfRemoteBranch>` ** to delete the local branch `git branch -D <NameOfLocalBranch>`

This is handy for running tests on previous revision (back-tracking),

 `git checkout <FirstSevenCharactersOfSHAID> -b <LocalBranchName>`, ** -b creates a new local branch, checks out a particular revision using Git SHAID. I usually use the SHAID as the Branch Name when running tests. 

If you want to revert changes,

 `git revert <ShaID>`, reverts a particular commit SHA 

 `git checkout --<NameOfFile>`, checks out the original file from the remote branch

 `git reset --hard HEAD`, resets the local branch to it’s original state

Finding who modified a particular line of code / file

 `git blame <NameOfFile>` , go to the line(s) of code that you are tracing the history of, git will show you the author’s credential and the SHAID. 

** Fetch (`git fetch`) the latest code to see the latest committer. 

Stashing changes to move to different branch

  1.  `git stash`, saves the changes
  2.  `git pull`, updates and fetches the latest changes from the remote repository 
  3. `git checkout <nameOfOtherLocalBranch>`, change to different branch 
  4. `git stash apply`, applies the changes to the new branch 

Cherry-picking -- porting commits from one branch to the other

  1. Get the SHA from a particular branch or repository.
  2. Switch to the desired branch `git checkout <BranchName>`
  3. Cherry pick changes `git cherry-pick <FirstSevenCharOfShaID>`

Finding all commit related to particular project or topic

 `git log --grep=”<keyword>”`

 `git log --grep="<ProjectIDHere>"`, returns any related commit to that particular Project ID (Project Management App ID) 

 `git log --author="<NameofTeamMemberHere>"`, returns any related commit that person checked in that repository (checkout --since to add filter)

To squash changes in single commit,

 `git rebase -i`, follow the prompt to accomplish your objective. I normally use this to squash multiple local commits into a single commit prior to committing to the central repository

If you want to understand the fundamentals and strategies, this is the book that I read and would recommend "Version Control with Git" by O'Reilly Media. However, there are many more new commands that have been implemented since. Your best bet would be to browse their man page.

While these handy commands are convenient to have, you may want to checkout Atlassian’s Source Tree plugin. It is convenient to those who are GUI oriented. 

Please do not hesitate to reach out if you need help with branching and release strategy as well as training your team using Git. I am glad to help!

Read This Next

A Better Way To Structure A Team

My thoughts in adapting committee structure instead of traditional pyramid hierarchy when forming a team.

Recommended Books

List of books in product and personal development and futuristic stories, I would personally recommend.

See All Posts

Want to get updates from me? Please subscribe or connect with me.

I respect your privacy.