Some handy git commands

Man, I haven't blogged in a long time! I don't have any particular excuse for that :| So I'm gonna try and write a random post in order to start again, and try to blog frequently.

These are some git commands I found useful in the past two months of contributing to open source.

$> git checkout -b branch-name
$> git add .
$> git commit -m "Commit message"
$> git push origin branch-name

You can see all your branches with git branch -l. After your PR is merged, you can remove that branch with git branch -D branch-name.

Protip: Branch out from master. Visualize trees (Referring to the data structure).

$> git commit --amend
$> git push -f origin branch-name
$> git rebase -i HEAD~n
# Change pick to squash for a all n-1 commits except
# the first one in the text editor, save and exit.
$> git push -f origin branch-name
git reset --hard hash
$> git stash
$> git stash list
stash@{0}: WIP on rename_LDA_QDA: d09f7f7 Removed ...
stash@{1}: WIP on RFECV: 4a57c92 Merge pull request ...

git stash apply will apply the most recent stash on the working branch. git stash pop will apply the most recent stash and delete it from the list. A specific stash can be applied using git stash apply stash@{n}.

git stash drop stash@{n} will delete that stash from the list.

Suppose you are on the master branch and want only a specific commit from another branch without merging that branch, you can cherry-pick that commit using its SHA-1 hash.

git cherry-pick hash

I will add to this list when I come across more useful commands!