Everything You Need To Know About Tech

Git commands to fix mistakes and How to cancel it?

0 37

If you made a mistake in Git commands, figuring out what is happening and how to fix it is not an easy task. The Git documentation is a rabbit hole, from which you get out only knowing the specific name of the team that will solve your problem.

We talk about the commands that will help you get out of problem situations.

Damn, I did something wrong … Does Git have a time machine ?!

git reflog
# Here you will see everything that you did
# in Git in all branches.
# Each item has an index HEAD @ {index}.
# Find the one after which everything broke.
git reset HEAD @ {index}
# Time machine at your service.

So you can restore what you accidentally deleted, and roll back the merge, after which everything broke. reflogused very often – let’s thank the one who suggested adding it to Git.

I just committed and noticed that something needs to be fixed!

# Make changes
git add. # or add files individually.
git commit --amend --no-edit
# Now the last commit contains your changes.
# ATTENTION! Never modify published commits.

Usually this command is needed if you committed something, and then noticed some trifle, for example, a missing space after the sign =. Of course, you can make changes with a new commit, and then combine the commits with rebase -i, but this is much longer.

Caution Never modify commits in a public branch. Use this command only for commits in the local branch, otherwise you will end.

I need to change the message of the last commit!

git commit --amend
# Opens the commit message editor.

Dumb posting requirements …

I accidentally committed something to the master, although I had to in a new branch!

Related Posts
1 of 26
# This command will create a new branch from the current state of the wizard.
git branch some-new-branch-name
# And this one will delete the last commit from the master branch.
git reset HEAD ~ --hard
git checkout some-new-branch-name
# Now your commit is completely independent :)

Teams will not work if you have already committed to a public branch. In this case, it may help git reset [email protected]{some-number-commits-back}instead HEAD~.

Well, great. I committed to the wrong branch!

# Undoes the last commit, but leaves the changes available.
git reset HEAD ~ --soft
git stash
# Switch to the desired branch.
git checkout name-of-the-correct-branch
git stash pop
# Add a specific file or do not worry and drop everything at once.
git add.
git commit -m "Your message will be here"
# Now your changes in the right branch.

Many in this situation offer to use cherry-pick, so you can choose what you like best.

git checkout name-of-the-correct-branch
# Take the last commit from the master.
git cherry-pick master
# Delete it from the wizard.
git checkout master
git reset HEAD ~ --hard

I try to run diff but nothing happens

If you know that the changes have been made, but are diffempty, then perhaps you indexed the changes (through add). Therefore, you need to use a special flag.

git diff --staged

Of course, “this is not a bug, but a feature,” but at first glance it is damn ambiguous.

I need to somehow  cancel the commit that was made 5 commits ago

# Find the commit you want to cancel.
git log
# You can use the arrows to scroll up and down the list.
# Save the hash of the desired commit.
git revert [that hash]
# Git will create a new commit overriding the selected one.
# Edit the commit message or just save it.

You do not have to roll back and copy-paste the old files, replacing them with new ones. If you committed a bug, then the commit can be undone with revert.

In addition, you can not roll back the whole commit, but a separate file. But following the canon of Git, these will be completely different teams …

I need to undo the changes to the file

# Find the hash of the commit to roll back to.
git log
# Save the hash of the desired commit.
git checkout [that hash] --path / to / file
# Now the index will be the old version of the file.
git commit -m “Oh my gadble, you didn't even use copy-paste”

That is why it checkoutis the best tool to roll back changes to files.

Come on again, Misha, all x

cd ..
sudo rm -r git-repo-dir
git clone https://some.github.url/fucking-git-repo-dir.git
cd git-repo-di

If you need to completely roll back to the original version (i.e. cancel all changes), then you can try to do so.

Be careful, these commands are destructive and irreversible.

# Get the last state of origin.
git fetch origin
git checkout master
git reset --hard origin / master
# Delete unindexed files and folders.
git clean -d --force
# Repeat checkout / reset / clean for each broken branch.

These Git commands are needed for emergency situations, but not only they can come in handy.

Useful Git Commands: safely cancel commits, add a file from another branch, and others


This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More