Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Don’t panic. Here’s how to un-do mistakes in Git


Overview

This presents ways to reverse or un-do common actions in Git.

Fork

If you didn’t mean to fork a repository, but you did anyway, erase the repo.

  1. Click on Settings tab in GitHub.

  2. Scroll down to the bottom of the page and click Delete this repository.

  3. Type the name of the repo.

  4. Click “I understand the consequences, delete this repository”.

  5. Provide your password if it’s requested again.

  6. For a list of your repos, click your avatar at the upper-right corner and select Your profile.

    Return

Git config

If you find a mis-spelling in your attribution, simply repeat the command with the information you want.

On a Mac, git config commands create entries in the ~/.git/config file:

[user]
   email = wilsonmar@gmail.com
   name = Wilson Mar
   user = wilsonmar
   

Edit the text file.

Return

### Clone in account folder

If you mistyped, remove a folder and all its contents with a rm -rf command. But be at the folder above the folder you want to remove.

pwd
   cd ..
   rm -rf gitclass
   

Return

### git checkout file

Mentioning a single [file] in the git checkout command overwrites whatever changes have been made to the file and replaces it with an old version of that file:

git checkout -- [file]
   
  • As with several other git commands, two dashes goes before specification of a single file.

  • Git assumes you want to checkout HEAD, the last commit on the currently-checked-out branch.

  • Whatever exists are permanently gone because they were not committed.

Checking out an old commit from .git repo history makes the entire working directory match that commit. However, the “current” state of your project remains untouched in the master branch.

Return

### New branch

A branch can be deleted because they are just pointers to commits.

git branch -d branch name
   

Return

### Editing

It doesn’t matter how many times a file is changed and saved if that file is unmanaged.

Use the git status command to see whether the file is managed.

### git cherry-pick

To replay changes specified in a specific commit onto files in the working directory:

git cherry-pick [commit SHA1]
   

### Status

The git status command details the status of changes to the repo.

### Local Clean

If you are overwhelmed by too many untracked files in your working directory, first see what files will be cleared using one command, first use the -n flag:

git clean -n
   

Nuke files from the folder using the clean command:

git clean -fdx
   
  • -f removes files untracked. It’s required.
  • -d removes untracked directories.
  • -x removes files Git ignores due to mention in .gitignore.

### Un-Add/Reset from Staging

To remove a specified [file] (such as README.md) just from the staging area, but leave the working directory unchanged:

git reset [file]
   

Alternately, the Staging area is also called cache because the command to remove a file in Staging:

git rm --cached [file]
   
  • Specifying git rm without –cached removes the file from both cached and working directory.

Return

### Commit - Amend Message

Changes can be made to specific commits as long as they have not been pushed to others in GitHub. The following applies to such commits.

If the message text is all you want to undo, repeat the command with --amend added.

git commit -m"Update again for show" --amend
   

The above creates a new commit in place of the previous commit.

The action is remembered by git reflog locally until purged.

To replace just the content of a commit, git add the change, then:

git commit --amend --no-edit
   

You lose the ability to fall-back to previous versions. So use it only to fix minor typos.

### Commit - Revert

To change a file with commits already pushed to others, use the git revert command for Git to figure out the opposite of all the change introduced by a particular commit. If the commit added some text, revert deletes that text.

First figure out the specific commit id using the git log command (such as “5a34def”)

git log</em>
   

Then supply that commit id in the command:

git revert [commit-SHA]
   

The creates a new commit which need to be pushed up to GitHub.

See this explanation and manual on the git revert command.

Return

### Rebasing

Before pushing to GitHub, some prefer to rebase to squash some commits so that only one commit message appears for several commits made.

  • Rebasing cleans up intermediate commits that is unwanted noise to the rest of the team. The extra commits complicates the history, and makes back-out of code more difficult.

  • Yes, this changes history, which is why we do it. But it’s only on your local version.

  • See http://wilsonmar.github.io/git-rebase for step-by-step instructions.

### Reset Local Commits

To reset the .git repo history and the working tree (the “hard” part) back the way it was 2 commits ago:

git reset --hard HEAD^
   
  • This should only be for commits which have not been pushed public.
  • HEAD~1 is same as HEAD^ to go back one commit.
  • HEAD~2 or HEAD^^ to go back two commits.
  • Do not specify just HEAD because the point of the command is to go back.
  • PROTIP: --hard is recommended because it keeps the working tree the same as what’s in the .git repository.
  • PROTIP: You may prefer to completely clear out whatever files created are untracked in the working tree.
  • The next step is usually a git add.

### Public revert push

The preferred alternatively is a revert command can be issued, as described above.

But do it for each commit pushed, in reverse order.

Alternately, to undo the previous git push command (specified by HEAD^) which sent to a remote origin what has been committed for a specific branch:

git push -f origin HEAD^:master
   

Return

### Un-Push tags

To delete a tag in the origin repo (on GitHub or GitLab),

git tag released/201706
   

Remember the colon character to specify delete, followed by “refs/tags” as in:

git push origin :refs/tags/released/201706
   

Return

### Delete branch

  1. Because branches are just markers within Git, once a feature branch is in GitHub, that branch can be deleted from the local repo

    git branch -d feat1
    
  2. and from GitHub (by specifying that colon in front of the branch name).

    git push origin :feat1

    NOTE: The colon is the secret special sauce. There is no “delete” command with this.

    Return

    Pull Request

    A pull request can be cancelled from GitHub.

    Return

    Upstream Remove

    To remove a remote:

    git remote remove upstream 
    

    Verify:

    git remote -v
    

    Return

    Pull reversal

    Use the ORIG_HEAD created when a checkout occurred:

    git reset --hard ORIG_HEAD
    

    Alternately, you can use time specification:

    git reset --hard master@{"10 minutes ago"}
    

    Return

    Un-Fetch my changes

    To undo what has been fetched from a remote, remove the remote:

    git remote remove upstream
    

    Now add it again and fetch only single branch:

    git remote add upstream https://github.com/wilsonmar/git-utilities
    git fetch upstream
    

    Return

    Merge abandon during merge

    Cancel a merge:

    git merge --abort
    

    Return

Resources

More

This is one of a series on Git and GitHub:

  1. Git and GitHub videos

  2. Why Git? (file-based backups vs Git clone)
  3. Git command shortcuts

  4. Git-client based workflows
  5. Git whoops (correct mistakes)
  6. Git rebase
  7. Git interactive merge (imerge)
  8. Git HEAD (Commitish references)
  9. Git commits with a Tag and Signature

  10. Git custom commands
  11. Git utilities
  12. Git hooks

  13. GitHub data security

  14. TFS vs GitHub
  15. GitHub REST API
  16. GitHub GraphQL API
  17. GitHub PowerShell API Programming
  18. GitHub GraphQL PowerShell Module