Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github

LinkedIn

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

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

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.

    [git-checkout in SCM], Return

    New branch

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

    git branch -d branch name
    

    [git-branch in SCM], Return

    Editing

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

    To see whether the file is managed:

    git status

    git cherry-pick

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

    git cherry-pick [commit SHA1]
    

    [git-cherry-pick in SCM,VIDEO]

    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 enumerate what files will be cleared:

    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.

    [git-clean in SCM]

    Un-Add/Reset from Staging

    To remove a specified [file] (such as README.md) just from the staging area (undo git add), 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, add -r . (with the dot representing all files and folders under the current folder):

    git rm --cached -r .
    

    [git-reset in 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"#DAC-123 Update again for show" --amend
    

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

    The amend action is remembered by git reflog locally until purged.

    Commit - Amend Contents

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

    git commit --amend --no-edit
    

    The above command means you lose the ability to fall-back to previous versions. So use it only to fix minor typos.

    See [docs/git-commit]

    Commit - Should Have Branched

    If you made a commit to master and then realized (before pushing) that you would like to put changes in the lastest commit under a new branch:

  7. Checkout a new branch named “my-new-branch”, which you change:

    git checkout -b my-new-branch
  8. Identify the SHA of the previous commit:

    git log --oneline

    In the example below, it would be “ee1a5c98”:

    b10405ec (HEAD -> master, origin/master, origin/HEAD) whatever
    ee1a5c98 Update
  9. Identify the specific date and time of a commit:

    git show --no-patch --no-notes --pretty='%ci' ee1a5c98

    The response is a sortable international date format such as this:

    2019-08-29 10:36:37 -0400

    -0400 is the time zone offset for the local time in New York City, West of Greenwich, England (UTC 0).

  10. Position the branch to the SHA found:*, for example:

    git branch -f master ee1a5c98
  11. Confirm:

    git log --oneline

    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
    

    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.

    git-revert in 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^
    
    • PROTIP: --hard is recommended because it keeps the working tree the same as what’s in the .git repository.
    • 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: You may prefer to completely clear out whatever files created are untracked in the working tree.
    • The next step is usually a git add.

    [git-reset in 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
    

    [git-push in 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
    

    PROTIP: 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

    Cancel 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 reported by git log:

    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

    Forgot to create a branch first

    If you’ve been committing to the master branch and realize that you should have first created a branch, you can create that branch and reset the master to that as the HEAD. [This article describes the process.

    Create a branch:

    git branch mytopicbranch
    

    Identify the SHA associated to the commit where HEAD should be by looking on-line or a git log command such as commits not yet pushed, such as “2f7efb32”.

    Reset HEAD to that SHA, such as:

    git reset 2f7efb32 --hard
    

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 whoops (correct mistakes)
  6. Git messages (in commits)

  7. Git command shortcuts
  8. Git custom commands

  9. Git-client based workflows

  10. Git HEAD (Commitish references)

  11. Git interactive merge (imerge)
  12. Git patch
  13. Git rebase

  14. Git utilities
  15. Git-signing

  16. Git hooks
  17. GitHub data security
  18. TFS vs GitHub

  19. GitHub actions for automation JavaScript
  20. GitHub REST API
  21. GitHub GraphQL API
  22. GitHub PowerShell API Programming
  23. GitHub GraphQL PowerShell Module