For each one there is a short description, a use example and some tips. These are the commands I will talk about:
Rebase interactiveRebase (What is a rebase?) of a branch to merge different commits as is need.
Use example:A branch has 15 commits of a new feature. Before merging to master, doing a rebase interactive you can merge all the commits into 1 commit to keep master clean.
git rebase -i [hash of the previous commit to merge]
After marking all the commits to merge, at the branch there will be only 1 commit.
- For each rebase (interactive or not), if the commits have been previously pushed to a remote repository, the git history is modified. For these reason if you want to continue working with the branch, you have to make a new branch. I usually make a new branch appending -R1. For the next rebases I set -R2, -R3, … -Rn.
- To get the hash of the commit, you can make git log and search the hash of the commit that is before the first commit you want to merge.
- Very important! While you are marking the commits at the rebase, if you don’t set any action (squash, pick, …), the commit is deleted. In that case you can use the git reflog!.
Stash“Keep apart some changes that are not committed”
Use example:While developing some code at a branch, you need to change to another branch to check some code. Doing checkout shows an error of changes not committed, and you don’t want to commit them.
Easy, just do:
Then you will be able to checkout to another branch, search and check anything and then go back to the previous branch.
git stash pop
Voilà! You have your uncommitted changes again.
ReflogView all the steps that git has done. Git keeps a record of each checkout, commit, rebase, or any action that has been done (with git). You can go to any step using the hash of that step that can be found with reflog.
Use example:After doing incorrectly a rebase interactive, a commit has been lost (it wasn’t marked with any action). To solve it, you can go back to the step before the rebase.
Search for the hash of the step before the rebase interactive, and then do:
git reset --hard [hash]
- You can use the date/hour to help to find the step using:
git reflog --date=iso
Commit amendCommit changes adding them to the last commit.
Use example:In the middle of a development you want to save some small changes, and you don’t want to make a new commit.
All the changes will be committed (at the last commit).
git commit --amend
- A amend commit can not be done to a commit that has been already pushed to a remote repository.
- It’s an alternative to do a lot of commits and then a rebase interactive before merge to master.
Pull rebaseIf you want to make pull but you have some unpushed commits at a branch, with a normal pull, git will create a “Merge commit”. You can avoid that doing a rebase pull.
git pull --rebase origin master
- It is said that after each “Merge commit” a cute cat dies 🙁
- Doing a pull rebase, your commits are move to the top of the git history.
Cherry pickCherry pick allows you to take any commit from any branch and copy it to your branch.
I don’t use it usually, but it’s very useful and it must be at this list of git commands.
Other useful commands
$ git checkout -: Move to the previous branch where you have been.
$ git checkout -b [branch name]: Create and checkout a branch.
$ git blame [file name]: View who edited a file.
I’m a web and iOS developer. I like to start projects, personal ones, with friends or at work.