Git Outta Trouble
By: Peter Conrad
Tags: Git, Programming, Code, Github

If you get out into the woods with Git, there’s usually a way to get back. Here are a few command-line tricks to help you back to the trail.

Authentication

The first time you use Git on the command line, it might ask you to complete authentication in your browser:

$ git push

info: please complete authentication in your browser...

Everything up-to-date

If that happens, go to the browser and Git will help you create a token that authenticates you every time you type a command.

The old method of authentication involved a username and password when you typed a command:

$ git push

Username for 'https://github.com':

If that happens, it means your repository hasn’t been updated to the new way yet. If you’re using a host such as GitHub, your repository should already be configured to use a token.

If your repo still uses the old authentication method, you can store your username and password so you don’t have to enter it every time:

$ git config credential.helper store

$ git push

Everything up-to-date

This stores your credentials in plain text on your computer, which is not nearly as secure as the new authentication method, which uses a token.

Edited in the Wrong Branch

You’ve edited a file in the wrong branch. What you’d like to be able to do is undo those changes, switch branches, then re-do them. Actually, it would be even better to lift those changes off of the wrong branch, laying them gently on top of the branch you meant to be in. Fortunately, Git provides a command called stash that does exactly that.

If you have not only edited, but committed by mistake, un-commit the changes first (see “Committed by Mistake” above) before proceeding with the following steps.

Make sure you’re in the right directory. Use git status to check what branch you’re on. If necessary, change to the branch where you erroneously edited the file. For example:

$ git checkout the-wrong-branch

Switched to branch ‘the-wrong-branch’ Stash your uncommitted changes:

$ git stash Switch to the branch you wish you had been working in:

$ git checkout -b the-wrong-branch

Switched to branch ‘the-wrong-branch’ Use stash pop to apply the changes there:

$ git stash pop

Edited the Wrong File

You opened a file to look at it, but then your cat walked across the keyboard.

You’re not sure what was added or deleted. You just want to go back to the way things were at the last commit. For this, use checkout —it’s not just for switching branches!

Make sure you’re in the right directory. Use git status to check what branch you’re on. If necessary, switch to the branch where the problem occurred. For example:

$ git checkout the-branch

Switched to branch ’the-branch’ Use git status to see what files were accidentally modified. For example:

$ git status

On branch main

Changes not staged for commit:

(use “git add <file>…” to update what will be committed)

(use “git checkout — <file>…” to discard changes

in working directory)



modified: dont-change-this.md Use git checkout -- <file> to undo the changes. For example:

git checkout -- dont-change-this.md

The output of the git status command tells you how to use git checkout this way.

Merge Conflict

When two changes have happened in the same place in the same file, Git can’t merge without your help.You need to edit the file and decide which of the two changes to keep.

When you open the file, the merge conflict looks like this:

<<<<<<< HEAD

Some content that was changed by one person

=======

Other content that someone else changed

>>>>>>> 9af9d3b

HEAD is a pointer to the most recent commit in the branch you’re on. The other label can be another branch name or a number representing another commit.

All you need to do is decide which version of the content you want to keep and then delete the merge conflict markers ( <<<<<<< , ======= , #>>>>>>> ).

After you’ve resolved all the changes in that way, just commit again.

Staged by Mistake

You edited the right file the right way, but then you added it to the staging area too hastily. You don’t want to undo your changes to the file, but you would like to remove it from the next commit. This is one of the uses of reset . You can also use reset to do more drastic rollbacks—you can undo entire commits if needed.

Make sure you’re in the right directory. Use git status to check what branch you’re on. If necessary, switch to the branch where the problem occurred. For example:

$ git checkout the-branch

Switched to branch ‘the-branch’ Use git status to see what files were accidentally modified. For example:

$ git status

On branch main

Changes to be committed:

(use “git reset HEAD <file>…” to unstage)



renamed: README.md -> README

modified: dont-commit-this.md Use git reset to remove the file from the next commit. For example:

$ git reset HEAD dont-commit-this.md

The output of the git status command tells you how to use reset to unstage changes.

Committed by Mistake

If you’ve committed too hastily, you can un-commit your changes so long as you haven’t also pushed them.

Make sure you’re in the right directory. Use git status to check what branch you’re on. If necessary, change to the branch where you erroneously committed. For example:

$ git checkout the-wrong-branch

Switched to branch ‘the-wrong-branch’ Use git reset to un-commit the changes:

git reset HEAD~ --soft

You can re-commit the changes later if you like.

If you want to move the changes to another branch first, you can use git stash (see "Edited in the Wrong Branch" below).

Pushed by Mistake

If you’ve pushed a commit erroneously, your mistake is now available to other people. Fortunately, there is a way to “undo” a push.

It can be useful to log on to the git host to see what actually got pushed.

Revert a Bad Push

Here are the steps to revert a bad push:

Use git log to show a list of recent commits, and identify the one you pushed by mistake. The --oneline option makes the list easier to read. The list is sorted from newest to oldest. Example:

$ git log --oneline

d9fa1d2 (HEAD -> master, origin/master, origin/HEAD) This commit is a mistake!

d555f66 Committing some lovely changes Use git revert and the commit number (or commit hash) to create a new commit that un-does everything in the bad commit. Example:

$ git revert d9fa1d2 A screen appears where you can type a commit message, or just keep the default message. Saving and exiting are different depending on which editor Git is set up to use.

- If you see a colon at the bottom of the screen, you’re probably using vi or Vim. Type wq and press enter.

- If you see a list of commands at the bottom of the screen, you’re probably using Nano. Press Control-o then Control-x . You can use git status to see that you have a new commit, ready to push:

$ git status

On branch master

Your branch is ahead of ‘origin/master’ by 1 commit.

(use “git push” to publish your local commits) Push the commit with the git push command:

$ git push

That takes care of fixing the repo. Anyone who pulls will now get the entire history, including both the mistake and the fix, and all will be right with the world.

This operation leaves things as they were before you made the changes that you mistakenly pushed. In other words, your files will look like they did after you committed “Committing some lovely changes,” and all the work you did in “This commit is a mistake” has been undone.

Retrieve Changes from an Old Commit

If you want to retrieve changes so you can work on them again, go back to the old commit (for example, d9fa1d2 ) and stash them:

Use git checkout to check out the old commit. Example:

$ git checkout d9fa1d2 Do a soft reset, which uncommits all the changes:

$ git reset — soft HEAD~1 If you like, you can use git status to see what uncommitted changes you now have. Use git stash to stash the changes. Use git checkout to check out the HEAD of the branch again. If you have been working in the main branch, use git checkout main :

$ git checkout main

Previous HEAD position was d555f66 Committing some lovely changes

Switched to branch ‘main’

Your branch is up to date with ‘origin/main’. Use git stash pop to apply the changes.

You can continue working on the changes, then commit and push when you’re ready.

More trouble

For more help and advice, check out Dangit, Git!?!