Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Instagram 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.

    [SCM], Return

    New branch

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

    git branch -d branch name
    

    [SCM], 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]
    

    [SCM]

    Status

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

    Committed but not pushed

    To get a list of commits but not yet pushed:

    git log origin/master..HEAD --oneline
    

    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.

    [SCM]

    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.

    To remove every file from Git’s index/Staging:

    git rm --cached -r .
    
    • The -r means -recursive of all files and folders under the current folder.

    [SCM], 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.

    [SCM],

    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.

    SCM, Return

    Reset Local Commits

    To reset the .git repo history and the working tree (the “hard” part) back the way it was one commit (^) 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.

    [SCM]

    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
    

    [SCM], Return

    Un-Push tags

    To delete a tag in the origin repo (on GitHub or GitLab), such as in branch released:

    git tag released/201706
    

    In GitHub, the colon character is what specifies delete, followed by “refs/tags” as in:

    git push origin :refs/tags/released/201706
    

    [SCM], Return

    Fallback to previous SHA

    I’ve had to do this with my github.io repository, which contains markdown text in the _posts folder that GitHub automatically converts into displayable HTML in the _source folder. GitHub sends emails out if it cannot do that. However, I didn’t read my email and several changes to markdown didn’t become visible to readers.

    To fix it, I looked at GitHub online to identify the date and time stamp to the last good update so I can fall back all changes to that point in time.

    PROTIP: This is a way to fall back if there are no merges in between the current HEAD and the hash one before the one that you want to go back to:

    git revert --no-commit e05fced..HEAD
    git commit
    git push

    Delete branch

    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
    

    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.

    [SCM], Return

    Pull Request

    A pull request can be cancelled in the GitHub web GUI.

    Return

    Upstream Remove

    To remove a remote named “upstream”:

    git remote remove upstream 
    

    Verify:

    git remote -v
    

    [SCM], Return

    Pull reversal

    Use the ORIG_HEAD created when the last checkout occurred:

    git reset --hard ORIG_HEAD
    

    Alternately, specify a timeframes:

    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
    

    [SCM], Return

Resources

This page combines the wisdom from these who came before me:

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 Markdown text
  4. Git basics (script)
  5. Git command shortcuts

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

  12. Git custom commands
  13. Git utilities
  14. Git hooks

  15. GitHub data security

  16. TFS vs GitHub
  17. GitHub REST API
  18. GitHub GraphQL API
  19. GitHub PowerShell API Programming
  20. GitHub GraphQL PowerShell Module