Git My Go-To cheat sheet 🔑

Imen Gharsalli
6 min readOct 16, 2018

--

In Part I and Part II of Git The-fundamentals, I went through the main concepts of Git 📚. I established an understanding of how Git works and discussed Git objects as well as the main sections of a Git project. I also provided an explanation of the main features that Git provides such as commits, references, branches, stashing along with an explanation of merging and rebasing. These notions make it possible for you to start working confidently with Git👌.
☀️Within this article, I will provide a complete cheat sheet in order to enhance these concepts with the key commands and I will explore the main options provided with these commands.We will start from key commits and references commands and finish with a thorough explanation of interactive rebase commands😻

1- Commits

  • To stage a file that you just edited (example.txt).
git add example.txt
  • To commit changes.
git commit -m "Initial commit"
  • To commit all changed files.
git commit -a

2- References

⭐HEAD 🙆🏻

HEAD is a reference to that last commit in the current checked-out branch.

  • To display what HEAD points to.
cat .git/HEAD
  • To discard all staged changes
git reset --hard HEAD
  • To roll back HEAD to a specific commit (a2316ad)
git reset --hard a2316ad
  • To compare head with the previous version (💡this will display the log message and the textual difference).
git show

➽ An other way to do the same thing.

git diff HEAD~..HEAD
  • To compare HEAD to a few commits ago (ex. 4 commits ago)
git diff HEAD~4..HEAD
  • To show your working tree status (difference between the index file and the current HEAD commit)
git status

Tags 🏷

Tagging is used to mark a remarkable point in history like release points.

  • To create a new annotated tag (v2.3) with “our version 2.3” as a message. (💡-a option indicates that it’s an annotated tag and -m option indicates the tagging message ).
git tag -a v2.3 -m "our version 2.3"
  • To create a new lightweight tag (v2.3-lw)
git tag v2.3-lw
  • To list the existing tags
git tag
  • To push a tag (v2.3) to remote
git push origin (v2.3)
  • To push all of your tags to remote at once
git push origin --tags
  • To checkout a tag (v2.3).💡This will put your repository into a detached-HEAD state.
git checkout (v2.3)
  • To delete a tag (v2.3)
git tag -d v2.3

Remote 🚀

  • To create a new remote (https://github.com/disneyland/minimouse) with “dl” as short name.
git remote add dl https://github.com/disneyland/minimouse
  • To list your existing remotes.
git remote
  • To fetch data from your remotes.
git fetch
  • To pull the latest changes is to perform a fetch and a merge combined in one command 👊.
git pull
  • To pull by performing a git fetch followed by a git rebase instead.
git pull --rebase

3- Branches

  • To switch to an existing branch (master).
git checkout master
  • To switch to a new branch (featureL).
git checkout -b featureL
  • To delete a local branch (featureL) that you are done with.
git branch -d featureL
  • To delete a remote branch (featureR).
git push origin --delete featureR
  • To find out which branches have already been merged to master (this can give you an idea about which branches you can remove).
git branch --merged master
  • To find out which branches have already been merged into HEAD.
git branch --merged
  • To find out which branches have not yet been merged to master.
git branch --no-merged master

4- Stashing 🗄

  • To stash your changes.
git stash
  • To remove the changes from your last stash and reapply them to your branch.
git stash pop
  • To keep the changes in your last stash and reapply them to your branch.
git stash apply
  • To delete your last stash.
git stash drop
  • To delete a specific stash (ex. stash nbr 3).
git stash drop stash@{3}
  • To delete all of your stashes at once.
git stash clear
  • To include your untracked files in your stash.
git stash--include-untracked
  • To add a description while saving your stash.
git stash save “WIP: making progress on foo”
  • To list your stashes.
git stash list

5- Merging

  • To merge your branch (myFeature).
git merge myFeature

6- Fast forward

As explained in Git-The fundamentals Part II, if no commits happened in the source branch after creating the feature branch from it, we have a fast forward.

  • To force a merge commit in a fast forward situation.
git merge myFeature--no-ff

7- Merge conflicts 😾

  • To enable rerere (reuse recorded resolution) for conflict resolution in your project.
git config rerere.enabled true
  • To enable rerere for conflict resolution in all projects.
git config --global rerere.enabled true

8- Rebasing

  • To rebase a branch (master).
git rebase master

9- Interactive rebasing 🦄

  • To use interactive rebasing on a commit (8352b7e).
git rebase -i 8352b7e^
  • To use interactive rebasing in order to change the last 5 commits ( or some of them).
git rebase -i HEAD~5
  • This will open an editor which includes the commits that you requested to change 🎉 . Following is an example of the content displayed in the editor.
pick 310154e refactor MinnieMouseHelper
pick b2a1d3e add anew value in MinnieMouseFriendEnum
pick f2a3b1b add new section in the README
pick 150378d improve minnieMouseFavoriteOutfit() exception handling
pick a2316ad reduce minnieMouseFavoriteOutfit() complexity
pick b5e1d3e fix typo in the README
  • Additionally, a comment which provides a description of the role of the available commands is provided.
Interactive rebase commands description as provided once rebase -i is called

⭐ Using interactive rebase commands

  • Now let’s suppose that we would like to 🤔:
    * combine the 6th commit(b5e1d3e) with the 3rd commit(f2a3b1b) and edit the message ➽ squash.
    * Combine the 4th commit(150378d) with the 5th commit(a2316ad) and keep the previous commit message ➽ fixup.
    * Change the message of the 2nd commit(b2a1d3e) to fix typo “anew” ➽ reword.
    * Break the 1st commit (310154e) into two smaller commits ➽ edit.
  • Now, here’s what we need to do to transform our commits 🔮:
pick f2a3b1b add new section in the README
squash b5e1d3e fix typo in the README
pick 150378d improve minnieMouseFavoriteOutfit() exception handling
fixup a2316ad reduce minnieMouseFavoriteOutfit() complexity
edit 310154e refactor MinnieMouseHelper
reword b2a1d3e add anew value in MinnieMouseFriendEnum
  • As mentioned in the commands description , the order of the commands can be changed accordingly since they will be executed from top to bottom. Some of the commands we used will need interaction like squash, edit and reword, which will require the editor to open again in order to make the necessary changes. Other commands like pick and fixup will be processed with no interaction with us.

⭐ Handling squash

Now, Git will process pick, then the editor will open for squash as follows:

# This is a combination of 2 commits.
# This is the 1st commit message:
add new section in the README# This is the commit message #2:fix typo in the README# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

Handling edit

  • Next step would be to handle the second pick. Git will perform pick silently and the same applies to fixup as well. After that, Git reaches the edit command and the following message is printed in the terminal:
Commands to use to handle edit in interactive rebase

This means that you are able to perform changes on any file by running :

git commit --amend

Every change is a new commit. Once you are done with editing you can run:

git rebase --continue

Handling reword

  • Now that we are done with the edit command, Git will move on to the reword command and the editor will open again with a similar content as the following:
add anew value in MinnieMouseFriendEnum# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
...
  • At this point, you can change your commit message to fix the typo save and close the editor. Git will complete the rebase and you will have a brand new history rewritten just like you wish.

Conclusion

Throughout this article, I presented my go-to cheat sheet of Git commands. Every command in this article corresponds to a notion that was either developed in Part I or Part II of Git-The fundamentals. Relevant examples were used to better illustrate some scenarios such as references and interactive rebasing.

--

--