Git commands cheatsheet for your next interview

Aditya Daria
5 min readMay 21, 2022

--

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.
  • git log -p — lists the changed files
  • git checkout -b develop checks out a new branch called “develop”
  • git checkout init_devices switches branch to init_devices
  • git checkout <commit_number> goes to a specific commit
  • git checkout -b 1234 origin/mainline checks out a new branch called “1234”, that “tracks” origin/mainline, meaning “local branch has its upstream set to a remote branch”
  • git status lists new or modified files not yet committed
  • git diff <commit_number> compares current code to any commit
  • git add * adds all files to “Staging”. Ready for commit
  • git add <file> adds a specific file to “Staging”. Ready for commit.
  • git commit -m “Put Message Here” commits locally
  • git commit — amend” updates commit by amending your new changes to it. Must run after “add” command.
  • git commit — amend -m “an updated commit message” updates the commit, and its commit message.
  • git branch says which branch we’re on
  • git push pushes code to the branch we’re on
  • git push origin <my_branch> pushes to a specific branch. I think origin is a keyword. Reference
  • git pull pulls all changed files
  • git cherry-pick <commit_number> adds a specific commit to your branch

More commands

  • git rm <file> removes the file from server (Must then do a commit, then push)
  • git ls-files | xargs wc -l counts # of lines of code in a Github repo.
  • git config — global — edit Edit git commit signature
  • git branch -d the_local_branch Removes local branch. Link
  • git rebase -i HEAD~3 combines the last 3 commits into 1 commit. Called Squashing Commits.
  • git reset filename.txt undoes adding a file.

Create repository

  1. In the command line, type git init <project name> to create a folder that’s a Git repo.
  2. Add the repository to Github Desktop and push it through Github Desktop.

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
  • git clone <repo> putIntoThisFolder — same as above but puts GridWorldMDP repository into putIntoThisFolder.

Submit changes

  1. git status to list new or modified files not yet committed
  2. git add <file>
  3. git commit -m “<commit message>”
  4. git push to push the code to the repository. If it fails since the code needs to be pulled first, do:
  • git pull — this will do a git fetch and a git merge. (assuming no merge conflicts, go on to the next steps)
  • git push

Revert changes

  1. git revert <commit_number>
  2. then :wq to save in VIM
  3. then rebase

Rebase

Rebasing with Merge Conflicts

  1. Commit my code to mainline
  2. git pull — rebase
  3. If merge conflicts, read the super-helpful tips in the terminal. Basically just
  • Do a git diff to resolve the merge conflicts I have
  • I think next do a git rebase — continue

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.
  2. Get the branch we need. Try: git pull origin <branch_name> or git checkout <branch>
  3. Do git branch to make sure we’re on correct branch
  4. Do git reset — hard <commit_number> using the actual 7-digit (or full) commit number, to put changes on top of the branch we just switched to. Use commit number from step 1.
  5. If not on the latest commit, do a git pull — rebase. Note: this command doesn’t create a merge commit, so it makes other people’s code diverge from what they had.

Stashing

  • git stash — moves your changes to a stash (a location where changes can be saved)
  • git stash save “Custom Message” — stashes changes, with a custom message.
  • git stash apply — applies saved changes to your branch. Code also stays in stash.
  • git stash pop — applies saved changes to your branch. Code is removed stash.
  • git stash show stash@{1} -p — shows diff for stash@{1} (the 1st entry in the stash). Remove the -p to get abbreviated diff.
  • git stash list — shows all stashes.
  • More stash commands, and even More stash commands

Stash topmost commit

  1. git reset HEAD^ — basically like an uncommit
  2. git stash (this may not stash new files. Maybe try “tracking” the new files to see if this works)

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
  • git merge <commit_number_from_another_branch> — Merges another branch (up to the commit number) into this branch.

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.

--

--