Git is a very useful tool, but at the same time it is not very intuitive for new users. In this short post I wanted to show you how you can utilize Git to solve your day to day problems.
Organising your work
Okay, so you have cloned your repository and you want to start coding. Let us also assume that you are using some task tracking tool like Jira. Your typical workflow would look like this:
Create a ticket for your task. If you are using Jira your ticket identifier could look like this XY-123.
Next you need to create a branch for you to work on.
git checkout -b XY-123-short-description-of-your-task
Now you can begin working on your task, but at some point you may want to save some of your work. To do that you can just add your changes, commit it and push off to your branch...
git add changed-directory-or-file
git commit -m "XY-123 Short summary of the thing you did.
A more in-depth description of the changes you have made.
Could be written as a list of changes:
* A
* B
* C"
git push
...but hey, it did not work and it suggested some other command to run?! It just means that your branch does not exist on your Git server, so you need to tell it to create one.
git push --set-upstream origin XY-123-short-description-of-your-task
After some time you have probably created loads of changes and you want to present the work that you have done. Now, instead of creating a pull request (which is like a request to review your code before it is merged) straight away, the master branch has probably changed slightly. Additionally, you would not want your colleagues to see some of the commits that you have made, as some of them may have had some bad descriptions. To clean your history you can use something which is called interactive git rebase. It allows you to pick and squash commits that you have made and put your changes on top of the target branch. To put it simple - it allows you to synchronize with what is the current version of the code. To do it - simply type:
git fetch --all
git rebase -i origin/master
Now you will be presented with a vim like editor in which you may be able to select your commits. To edit the files just hit `i` and to exit the mode hit `ESC`. In this use case you probably just want to select the first commit and then squash the remaining commits into your first one. To do that replace the word `pick` with `s`.
After you have chosen which commits to include and squash confirm your choice by hitting `ESC` and typing `:wq`. Now git will apply your commits on top of the target branch. If a merge conflict occurs simply edit the files in which it has happened and git add them.
After you are happy with how you have resolved merge conflicts resume rebasing process by running `git rebase --continue`. If you have changed your mind and you do not want to proceed with the rebase simply type `git rebase --abort`. At the end you will be presented with an option to rephrase your commits' messages.
Now it is time to push your new history to your remote repository
git push origin XY-123-short-description-of-your-task
If you have rebased you will need to apply additional flag to push with `force` to override the history stored on the server run
git push -f origin XY-123-short-description-of-your-task
Now go to your git server and create a pull request (might be named differently for different types of git servers).
After you have submitted a pull request people might want to you to edit some parts of your code. Simply work on them and submit them as individual commits - this way the reviewers will be able to quickly see the changes that you have applied. When you are sure that you and your reviewers are happy with the quality of your code it is time to rebase once again and create a better summary of the changes you have created with some explanation why were they necessary etc. And that is it! Simple!
Commits on the wrong branch
So you have created a commit on the wrong branch. What a shame, but not everything is lost. You can still use git log to find out your commit's identifier and chery-pick it onto the correct branch. To do that simply take the commits id, checkout to the branch that you wanted to modify (git checkout) and apply your commit on top of it using