Member-only story
Leave Aside Git Checkout. Consider Git Switch for a Change
Switch between branches without checking out
Without looking any further can you tell straight away what git checkout
command does? I have asked my team and myself. Nobody including me was able to give a full answer.
First, what comes to mind is switching between existing branches. Running a command git checkout main
will switch from the current to the branch main. That is a correct answer yet not full.
The other thing that this command can do is switch to a certain commit in a history log. Running the command git checkout 21f25fe0
will bring the state of the changes made in commit with the SHA hash 21f25fe0
.
And still, something is missing. Git checkout command can do more. It is possible to reset unstaged changes in files. Running git checkout README.md
will remove any made changes if they were not added to the index.
Now, knowing all of this, does it feel like the right engineering solution? Rather opposite. It makes an impression of a big legacy that we are dealing with. The single command does three things based on the parameters. What we can use instead?
Switch Between Branches
We got used to the command git checkout
when we need to navigate between branches. Git has a dedicated command for that. By running git switch main
we are able to navigate between the branches. It is looking logical, isn’t it?
The checkout command can also accept a parameter -b
. We use it when we want to create a new branch and immediately switch to it. The command git checkout -b new_branch
will create a new branch and then switches to it. It is a convenient operation that does two actions at once. The good news is that the command switch has this option too. The parameter is -c
and the command git switch -c new_branch
will do exactly the same.
Jump To Commit
When you need to go in the past to a certain commit, we also use the checkout command. We specify a revision we want to inspect and the files will receive the state of that commit. This is a handy option for running bugs investigation if we want to find when things got broken.