Professional Git Workflow
Why?
If you do not have a Git workflow that you can repeat every day, then you will have a lot of confusion while collaborating in teams.
Git workflow
The stages for a good workflow with Git are the following: Track master, create a feature branch, add your changes, commit your changes, add more changes, push the feature branch, create a pull request, get all changes, merge all the changes from master, select finnal commit, update remote branch, merge pull request, remove feature branch. Let's see how it's done step by step.
Track master
First thing we want to do is create our feature branch out of master, because we want to start with the latest+working changes.
git checkout master
Now we are on master.
Create a feature branch
Suppose we will add a new feature to our app, so lets create a feature branch which will contain the changes for our feature.
git checkout -b add-user-registration
We have created a brand new branch called "add-user-registration"
Add your changes
Proceed to add/update/delete files in your app, just as an example:
touch registration.html
git add -A
This command will create an empty registration HTML file.
Commit your changes
Here is where we save our progress, we will commit our changes now.
git commit -m "Add user registration"
The commit message should be as short as possible, so it's easy to read by the people who is going to review the pull request later on.
What if I have to take a break?
It's 5 PM and you have added a new change to the app, say you created a new HTML file so users know how to register in the app BUT you have not finished with this new file because you are missing some details:
touch how-to-register.html
git add -A
We have added a new file, let's commit this changes too.
git commit -m "WIP: how to register"
Notice we added "WIP" which stands for "Work in progress", so our team knows we are still working on this feature branch.
Push the feature branch
git push origin add-user-registration
This will make your changes visible in your remote.
Create a pull request
So visit the Github repo and click the tab "Pull requests", then click "New pull request", then select one option from the left dropdown (target) and select one option from the right dropdown (source), then click "Create pull request".
Note: For the target option, try to always select the "develop" branch (if available). The develop branch is meant to be for experimental changes, so it's higly recommended to use it for new features like the one we are working on in this tutorial.
NOTE: I'm using Github, but this is very similar for other platforms.
Get all changes
So, it's the next day and we continue working on the same feature branch, remember?
Say our team has been adding changes into master but our feature branch does not have them because we were sleeping.
Let's update our feature branch with the latest master changes.
git fetch
Git fetch will simply get all the changes from the remote repository (Github in this case), however it's not going to alter our branch in any way.
Merge all the changes from master
But git fetch
is not enough to merge this changes into our feature branch, so let's merge the changes we just fetched.
git rebase -i origin/master
What we just did is put all master branch commits before our feature branch commits so our feature branch commits are last. The following diagrams explain this better,
Before git rebase:
* - add-user-registration
* - *
* - master
After git rebase:
* - * - * - master - * - add-user-registration
Note: The -i
option will open an interactive session, but let's see that in the next section.
Select finnal commit
So in our pull request we don't want to see two commits, we just want to see one because it's easy to search in the repository history.
The interactive session is still opened and we want to combine all the commits into one. Here is how it should looks like:
pick e414s79 Add user registration
s a767u81 WIP: how to register
...
Here you can also change the commit message if you like since we have added multiple changes then it probably makes sense to think of better commit message.
Note: The pick
option will indicate that we want to select that commit as our finnal commit, and the s
option indicate that we want to append the changes from the commit (a767u81) to the selected commit (e414s79).
Note: These commits IDs are not going to be the same for you, since this IDs are different for every repository.
Update remote branch
Now that we have merged the changes into our feature branch it's time to update the remote branch.
git push origin add-user-registration -f
Notice we use the -f
option since we want to force push the branch. The reason behind this it's because we have changed the Git history (by using rebase).
Team review pull request
Merge pull request
From your pull request page on Github, once it's reviewed by your team you can click "Merge pull request".
Remove feature branch
From your pull request page on Github, once it's merged, you can remove the remote branch from there by clicking "Delete branch". It's a good practice to remove merged branches. You don't need to worry about it because this changes has been merged already and it does not makes sense to keep it.
You should also remove your branch locally:
git branch -D add-user-registration
Final thoughts
That's it! You have learned how professional teams manage their version control in a daily basis using Git.
Every company has it's own different approach but in most of the cases it's very similar to this one.
I also think this should be part of every project wiki since it makes live easier for new team members.
@fnaf security breach
Now your article is really helpful for my work, you have provided the information I need, many thanks!
Thanks Victor. Added this to our project wiki. Great article!
You forgot to git add . after touch, isn’t it?
You are right Manuel
Ok! Also don’t forget to add it after
touch how-to-register.html
:)You are right again Manuel, thanks!