Skip to content

Instantly share code, notes, and snippets.

@oana-sipos
Last active May 25, 2018 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oana-sipos/20196a665d49c58807be7a0268141c5d to your computer and use it in GitHub Desktop.
Save oana-sipos/20196a665d49c58807be7a0268141c5d to your computer and use it in GitHub Desktop.
10-git-comands-you-must-know-about

Intro - my story

I was in a constant love-and-hate relationship with git at the beginning of time. Right now, I love it and consider it an amazing idea that totally transforms our daily processes. I believe it's a great time saver and an awesome safety net. If git ever frustrated you, know you are not alone. But keep persisting and I am sure you will reach the day when you will love git!


Intro - their story

Questions to get to know better the audience:

  • Do you use git daily?
  • Do you use a command line or GUI to work with Git?
  • Are you a beginner / intermediate / advanced?
  • Do you love git?
  • Do you hate git?
  • Did you ever get a merge conflict and preferred to delete the local repo & clone it again instead of sorting it out with git?

Let's get to work!

I have cloned this repository to prepare this presentation. Code might help though to explain some of the commands in a more real life scenario.

  1. git joke
  • it only exists because of a bash script in the repository above, so use it with care :)
  1. git stash
  • usage: whenever you are in the middle of a feature that is not ready and you need to 'leave' it for a bit (change a branch, pull from the remote, etc), you can "save" your changes by using git stash suite of commands
  • it is very handy and useful and it even allows to "save" untracked files until that moment
git stash
git stash pop
git stash list
git stash show STASH_REF
git stasf drop STASH_REF
git stash apply STASH_REF
git stash save "my message"
git stash -u
  1. git diff
  • usage: check what are the differences in the current branch between HEAD and the last commit
git diff SHA-1 SHA-2 --name-only
git diff SHA-1 SHA-2 --word-diff
git diff SHA-1 SHA-2 -w
git diff HEAD^1 HEAD^2
  1. git blame
  • usage: you can check the history for each line of code committed in a repository
  • very useful for checking when was every line committed, who did it, what else was committed in the same batch, etc.
  1. git commit --amend
  • usage: if you ever forget to add a file in a commit or if you ever want to modify the commit message, you can --amend the last commit and add all these (note: of course, you shouldn't have pushed to the remote!)
  • one scenario this is useful is when you want to incrementally add changes that are fixed and you are sure they work (tests for example), as you can go back to the previous version in case you screw something
git commit --amend --no-edit
git commit --amend "New commit message"
  1. git fetch --all && git merge remote branch
  • usage: update a local branch with changes that happened on a remote branch with the same name
  1. git rebase -i HEAD~3
  • usage: squashing 3 commits together in a single one
  1. git reflog
  • usage: life saver - whenever something seems to have been lost, running git reflog will reveal it
  • it shows all the changes made within the repositories (switch of branches, from which HEAD, what was logged etc)
  • whatever argument git log takes works with git reflog too
git log -g --abbrev-commit --pretty=oneline
  1. git branch
  • usage: general - create new branch
  • the point here is to delete local and remote branches (take the garbage out!)
git branch -D branch-name
git push oana --delete remote-branch-name
  1. git push origin HEAD
  • usage: a very nice way to push the current branch to a remote one with the same name
  • very handy when you have a very complicated branch naming system, such as 24213_create_a_new_booking_system
  • basically equivalent to git push origin 24213_create_a_new_booking_system:24213_create_a_new_booking_system
  1. git help
  • usage: get more commands info whenever you want

To consider: https://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch (tracking a remote branch to be able to pull on a local branch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment