Adán Carrasco
Posted on March 29, 2020
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:
1. Creating branch for our feature
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.
git checkout -b feature/user-service-implementation
Tip: prepend feature or bug to describe your branch's content :wink_face:
2. Committing your changes (this comes as 2x1)
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/
3. Exploring and reading historically other commits in the current branch
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:
4. Show me the contents of a specific commit
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
5. Crap, I want to undo my commit
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
6. About changing branches
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:
git checkout my-desired-branch-name
7. How to copy-paste commits
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.
First, you need to get the commit hash that you want to move (cherry-pick)
git log
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
8. Call to get remote changes
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:
git fetch
This will list any changes done in remote; can be any new added branches or changes to our source branch (typically develop).
9. Bring the latest changes on
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:
git pull
10. I'm ready to merge
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
Bonus: getting the latest from develop/master branch into your current working branch IN ONE LINE.
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!
Posted on March 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.