Date:

Share:

Git Commands That Will Save Your Job

Related Articles

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.

The real culprit is that there are glitches and no fixes

“Remember your typos and edit history”

– Anonymous

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.

1%2AfX6dZBNM2d etdA6 UaITQ
Use the -end command to correct the typing error

perfect! Now, your co-workers will not see that “perform“Originally written”to commit“.

A well-designed thing is something worth seeing

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.

1%2AgSwWWmLhWgzttSPlHcTJ7g

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”.

1%2A3SLl1UnyheC5E m0QXB0mQ
“Studs” are _pieces_ of code that we decide what to do with.

You will see the question, “Combine this hunk [y, n, g, a, d, e, ?]? “At the bottom of the terminal.

1%2ASj4F5okGIW K60QjbRoHNg

When we are done with all the “studs”, we can see that our commitment is ready and we can continue to push!

Bonus tip:

git commit -p Combines git add -p and git commit In one command.

Your mistakes do not define you

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.

1%2A1KBq39Wsd0HJWO4v0q8WNQ

The command 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!

We ran the git reset command – soft HEAD ~ 2 – it moved the HEAD to the previous two commits.

If you do not want to save the changes made by certain commitments, use git reset —hard HEAD~

0%2Aue43AYghqHFJHrg5

Bonus tip:

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.

Look for your choices, choose the best ones and then go with it.

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.

1%2AnvKsXUB33j s5zSlHDsNaQ

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.

1%2ADrwJLoomyKrBbVQa1OBFog

God cherry-pick The command takes changes from commit purpose and places them on the HEAD of the branch where you are currently located.

1%2AjB7KWUr2iKnMu0EEI73jJA

From here, you can continue working with the changes in your workbook or you can make the selected changes immediately.

1%2Ao2HlQEG1lWkZpRHK9oOHdg

This is helpful if you accidentally commit to the wrong branch.

1%2Ar4Rn2Hf9o0CDJ8lBsXGGLQ

With cherry picking, you can transfer these changes to the right industry without repeating all this work.

1%2ATQ4V0IN1v9preGrIOLXQwA

However, use cherry-pick Sparse!

Overuse cherry-pick Can lead to double liabilities, and sometimes a merger may be preferred to preserve the liability history.

1%2Alfz nmKf TfNAMue4eKO9Q

Real life scenario:

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.

0%2A0uzNtqDd8KiuSCI9
`Cherry-pick b`_ring commit whole from one branch to another._

Do not want everything? No problem – just a cash register

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.

it’s like 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.

1%2ApoZDlGmqOwvJSleVGbiN0g

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.

How It Works:

git checkout Tell Git which branch or undertaking to apply your new changes to.

1%2AyL68gbUi3Yr4jYw3jM3fhg

A branch is just a pointer to one specific commit, and commit is a snapshot of your pool at a particular point in time.

1%2AVF0e86kTT4PIhyTNB W0vw

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.

1%2AnL8uzFZw9GAG0MN0m2vhaQ

Exiting a branch will update your repo files to match the snapshot of any commitment to which the branch points.

1%2ATziopasHJcCOarYEDN8aGQ

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.

1%2ARlHBw8VmAUyELW1U2uRUQg

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.

1%2Asjv7VIu4 XGrNBAmInUQ9A

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.

One day … but not yet, not yet

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.

1%2AHBo3QdLBcH13 5RqQUpolw
Save uncommitted changes and save them for later. This is useful when you want to commit and push, but have not been willing to make the changes since the last commit.

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!

It’s time for you to get some help

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.

Bonus tip:

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.

Summary

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!

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles