Git commands cheatsheet for your next interview

In the article below I will be covering the basic commands to get started with git. These are mostly asked in the interviews as well.

Common Commands (All commands should be run without the <> brackets)

  • git log shows commit ids. The latest commit is at the top.

More commands

  • git rm <file> removes the file from server (Must then do a commit, then push)

Create repository

  1. In the command line, type git init <project name> to create a folder that’s a Git repo.

Clone Repository

  • git clone <repo>— clones repository and puts into the same folder that command is run from. The repository will be in GridWorldMDP folder

Submit changes

  1. git status to list new or modified files not yet committed
  • git pull — this will do a git fetch and a git merge. (assuming no merge conflicts, go on to the next steps)

Revert changes

  1. git revert <commit_number>

Rebase

Rebasing with Merge Conflicts

  1. Commit my code to mainline
  • Do a git diff to resolve the merge conflicts I have

Head is not on the main branch — Put changes on top of the head

A somewhat useful tutorial for git pull

  1. Do a git log to get the commit number corresponding to the changes you made. Save it for step 5.

Stashing

  • git stash — moves your changes to a stash (a location where changes can be saved)

Stash topmost commit

  1. git reset HEAD^ — basically like an uncommit

Merge Branch

To merge 1 branch into another, go to the “giving branch” and do a git pull. Then go to receiving branch, and run 1 of the following merge commands:

  • git merge <branch_name> — Run this from receiving branch. More info here

Create a new branch and upstream it

Troubleshoot

“Checkout” or “Pull” not working

A common mistake is to modify files on the local machine, and then try to do a “checkout” or “pull”. The problem is the checkout/pull will overwrite what we have. If we do want to OVERWRITE our files, we can erase our changes by typing git reset — hard HEAD. Then we can checkout/pull without problems, which gets us the remote files.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

I hope that you will find this article insightful.

Would you like me to encourage us to spread the insights on more topics, Please encourage us with a cup of coffee

For any suggestions/feedback please comment. Also please consider following and subscribing using my link.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store