In this article, we are going to learn some life-saving Git commands.
Not only will we learn about them, but we will also build a mental model of how they function behind the scenes.
Some you may have used in the past, while others may be new to you, but all are life-saving.
“Remember your typos and edit history”
Typing errors happen all the time!
Git gives us the ability to correct our mistakes.
Sometimes we can press “Enter” too early when performing, resulting in a typo somewhere in the commit message.
perfect! Now, your co-workers will not see that “perform“Originally written”to commit“.
Maybe you used a command
git add <file> or
git add . Many times but this does not allow us to execute only part of a file.
git add -p Allows you to direct parts of a modified file, instead of the entire file.
This allows you to make concise and well-designed commitments that make history easier to read.
This can improve the quality of commitments.
It also allows you to undo changes to a file that was there only for debugging purposes – before execution – simply without having to go back to the editor.
Once this command works, we are asked about “studs”.
You will see the question, “Combine this hunk [y, n, g, a, d, e, ?]? “At the bottom of the terminal.
When we are done with all the “studs”, we can see that our commitment is ready and we can continue to push!
git commit -p Combines
git add -p and
git commit In one command.
You made a commitment but now you regret it? You may have made secrets by mistake – not a good idea – or you may want to add more tests to your code changes.
These are all legitimate reasons to cancel your last commitment.
Git gives us an order to cancel our last commitment.
git reset There are a variety of things you can follow, let’s talk about the “soft” trailer.
git reset —soft HEAD~ Removes the commit from the current branch, but save the changes!
The cool thing about this ordinance is that the changes will not go away!
All changes will remain in the work tree!
If you do not want to save the changes made by certain commitments, use
git reset —hard HEAD~
Hagit Hook in advance to commit Is a built-in feature that allows you to configure scripts to run automatically before each commit. Use it to reduce the need to cancel commitments.
Suppose you are working with a team of developers on a medium to large project.
Another team member has suggested some changes, and you want to apply some of them to your main project, no all.
Because managing the changes between multiple Git branches can become a complex task, and you do not want to merge an entire branch into another branch.
You only need to choose one or two specific commitments. Choose some changes in your main project industry from other industries called cherry picking.
In this command, we can select a particular branch that has a commit and pull it into another. Let’s get over it.
commit is a snapshot of your git database at a given point in time and each commit cumulatively creates your repo history.
cherry-pick The command takes changes from commit purpose and places them on the HEAD of the branch where you are currently located.
From here, you can continue working with the changes in your workbook or you can make the selected changes immediately.
This is helpful if you accidentally commit to the wrong branch.
With cherry picking, you can transfer these changes to the right industry without repeating all this work.
cherry-pick Can lead to double liabilities, and sometimes a merger may be preferred to preserve the liability history.
Suppose we have a branch
76d12 And we made all our changes.
There’s another branch, we’ll just use
master But it can be any branch, and we want to enter
76d12The commitment of.
By using a command,
git cherry-pick 76d12We can put all this commitment into another branch.
Want to select a specific file from a branch and bring it to the current branch?
Som, I’m using
git checkout Mainly for moving from one branch to another.
Yes, I know but we can also use it to check files and commitments.
git cherry-pick But instead of “picking a cherry” a whole commitment, we dive even deeper into a branch and choose only a particular file that we want to merge.
While staying inside
master All the time, we can run the command
git checkout Som-test index.js Grab this particular committed file at that particular branch and bring it to the master to eventually push.
This is pretty neat when the changes of a particular industry are many and we are only looking for one file to test or push.
git checkout Tell Git which branch or undertaking to apply your new changes to.
A branch is just a pointer to one specific commit, and commit is a snapshot of your pool at a particular point in time.
The industry pointer moves along with each new commit you make.
If you want to make changes at a branch where you have not yet checked out, you must first make the payment at the branch.
Exiting a branch will update your repo files to match the snapshot of any commitment to which the branch points.
Hence, the branch indicates a follow-up of any new commitment you make to the branch.
You can also make a commit directly, which will update your repo files to match the snapshot of each commit you made.
Note that the commit check passes the HEAD pointer to the commit instead of the branch, and puts your repo into what is called a “disconnected head mode” where all changes will not be committed to the branch.
While a git checkout is great to start working with branches, it is also a useful tool for checking old commitments as it replaces the version of your repo with this older snapshot.
Suppose we have a bunch of changes in the file but we need to go back and check something before we commit and push it or we are just not ready to make our changes.
This is where
git stash Kicks.
When we run the command
git stash It takes the saved and uncommitted changes and “stores” them for a later date.
We can then move around different branches, attract changes to the master, and so on.
Once we have finished checking or doing everything we needed to do, we can go back to our branch, run the command
git stash pop And our changes will come back! It’s like we never left!
There are plenty of git commands to choose from. And it’s critical that we do not execute commands without knowing what they will do to our code.
If we just ran away
git help <command> Then we get an explanation of the command right there in our terminal.
Run the command
git help -w <command> Takes us directly to a site where we can read about all the things with the command in question.
Git is a really useful program. If done incorrectly, it could be a headache. I hope you find these hidden gems useful in making your Git experience more productive.
What is your go-to Git command? Please tell me in the comments!