When we talk about version control in Software Development, (in most of the cases) the first concept that pops to our minds is Git.
In Today's post I will share a story that will save you a lot of time and will give you a better understanding on how git works.
One way to go is to use a visual tool that could help you doing what those commands will help you doing, however, in most of the cases you end up clicking a lot to do what could have been done in a single line.
Let's picture the scenario where we want to implement the functionality for the User Service:
The first thing to do when you are about to develop a new feature is to create a new branch to track the changes for your amazing feature/bug.
Tip: prepend feature or bug to describe your branch's content :wink_face:
A good practice when working with git is to commit your changes often and in chunks, in a way that you can easily revert changes.
For one line commit message:
git commit -m "Add User service files and initial setup"
For more than one line commit message:
But before committing your changes you must add the file changes:
To add all the modified/added files:
git add .
To add specific files or directories (specify the path to the directory or file in case you only want to add a file)
git add user/service/
You may have already committed a bunch of things and you want to know wether you included a chunk or not. There's a shortcut to look at the commits have been already done. Some sort of history:
With the previous command you get the list of commits that have been made to your repository, but what you don't see is the content of those commit changes. To see them you just ask for them:
git show desired_commit_hash_goes_here
At this point of time you've already seen the commit that you weren't ready to include. How can you easily revert it? Well, you ask for it. :)
git revert desired_commit_hash_goes_here
Let's take a breath and learn a command that will help us in the next set of steps. The breath is about learning how to switch between branches. You simply do:
For the cases where you want to move one or more commits to other branch not affecting the git history and workflow you have an option too. Let's say you committed some changes for the UserController but you weren't supposed to do that because your current branch is for the User Service. With cherry-pick command you can move this commit to another branch, let's do it.
Second, once you have identified that commit or commits you take note of the hashes, because you'll need them to tell git what you want to move.
Third, you now go to the desired branch by doing:
git checkout feature/user-controller-implementation
Fourth, now in this branch we simply do:
git cherry-pick desired_commit_hash_goes_here
You do cherry-pick for every commit you want to move
Then you go back to your working branch by doing git checkout one more time:
git checkout feature/user-service-implementation
At this point we are done with the code to implement our feature and we are ready to create a Pull Request. However, there's a possibility that while we were implementing our changes someone else made some changes in our source branch. To verify we do:
This will list any changes done in remote; can be any new added branches or changes to our source branch (typically develop).
If there are some changes we need to bring them to our local. To do that we checkout to our desired branch then we do:
This command will merge from any specified branch to your current working branch. You'll see it in action in the Bonus section.
git merge merge_from_this_branch_name
git checkout develop; git pull origin develop; git checkout feature/user-service-implementation; git merge develop;
Those are the most common git commands I use in my day to day life as a Software Engineer. I prefer to know the commands so I don't depend on any visual tools. With this all I need is git installed and that's it. I'm ready to go and to work. And as most of the time my fingers are in the keyboard it saves a lot of time for the most common tasks done when working with git.
What are your most common git commands?
Thanks for reading!
Claim your page on DEV before someone else does
Level up every day