Undo changes | GitLab (2024)

  • Undo local changes
    • Undo unstaged local changes
    • Undo staged local changes
  • Undo committed local changes
    • Undo staged local changes without modifying history
      • Undo multiple committed changes
    • Undo staged local changes with history modification
      • Delete a specific commit
      • Modify a specific commit
    • Redoing the undo
  • Undo remote changes without changing history
  • Undo remote changes while changing history
    • When changing history is acceptable
    • How to change history
    • Redact text
    • Delete sensitive information from commits
  • Undo commits by removing them
    • Git reset sample workflow
  • Undo commits with a new replacement commit
  • The difference between git revert and git reset
  • Unstage changes
    • Unstage a file
    • Remove a file
  • Related topics

Git provides options for undoing changes. You can undo changes at any point in theGit workflow.

The method to use to undo changes depends on if the changes are:

  • Only on your local computer.
  • Stored remotely on a Git server such as GitLab.com.

Undo local changes

Until you push your changes to a remote repository, changesyou make in Git are only in your local development environment.

Undo unstaged local changes

When you make a change, but have not yet staged it, you can undo your work.

  1. Confirm that the file is unstaged (that you did not use git add <file>) by running git status:

    $ git statusOn branch mainYour branch is up-to-date with 'origin/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: <file>no changes added to commit (use "git add" and/or "git commit -a")
  2. Choose an option and undo your changes:

    • To overwrite local changes:

      git checkout -- <file>
    • To discard local changes to all files, permanently:

      git reset --hard

Undo staged local changes

If you added a file to staging, you can undo it.

  1. Confirm that the file is staged (that you used git add <file>) by running git status:

    $ git statusOn branch mainYour branch is up-to-date with 'origin/main'.Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: <file>
  2. Choose an option and undo your changes:

    • To unstage the file but keep your changes:

      git restore --staged <file>
    • To unstage everything but keep your changes:

      git reset
    • To unstage the file to current commit (HEAD):

      git reset HEAD <file>
    • To discard everything permanently:

      git reset --hard

Undo committed local changes

When you commit to your local repository (git commit), Git recordsyour changes. Because you did not push to a remote repository yet, your changes arenot public (or shared with other developers). At this point, you can undo your changes.

Undo staged local changes without modifying history

You can revert a commit while retaining the commit history.

This example uses five commits A,B,C,D,E, which were committed in order: A-B-C-D-E.The commit you want to undo is B.

  1. Find the commit SHA of the commit you want to revert to. To lookthrough a log of commits, type git log.
  2. Choose an option and undo your changes:

Undo multiple committed changes

You can recover from multiple commits. For example, if you have done commits A-B-C-Don your branch and then realize that C and D are wrong.

To recover from multiple incorrect commits:

  1. Check out the last correct commit. In this example, B.

    git checkout <commit-B-SHA>
  2. Create a new branch.

    git checkout -b new-path-of-feature
  3. Add, push, and commit your changes.

The commits are now A-B-C-D-E.

Alternatively, with GitLab,you can cherry-pickthat commit into a new merge request.

Another solution is to reset to B and commit E. However, this solution results in A-B-E,which clashes with what other developers have locally.

Undo staged local changes with history modification

The following sections document tasks that rewrite Git history. For more information, seeWhat happens during rebase.

Delete a specific commit

You can delete a specific commit. For example, if you havecommits A-B-C-D and you want to delete commit B.

  1. Rebase the range from current commit D to B:

    git rebase -i A

    A list of commits is displayed in your editor.

  2. In front of commit B, replace pick with drop.
  3. Leave the default, pick, for all other commits.
  4. Save and exit the editor.

Modify a specific commit

You can modify a specific commit. For example, if you havecommits A-B-C-D and you want to modify something introduced in commit B.

  1. Rebase the range from current commit D to B:

    git rebase -i A

    A list of commits is displayed in your editor.

  2. In front of commit B, replace pick with edit.
  3. Leave the default, pick, for all other commits.
  4. Save and exit the editor.
  5. Open the file in your editor, make your edits, and commit the changes:

    git commit -a

Redoing the undo

You can recall previous local commits. However, not all previous commits are available, becauseGit regularly cleans the commits that are unreachable by branches or tags.

To view repository history and track prior commits, run git reflog show. For example:

$ git reflog show# Example output:b673187 HEAD@{4}: merge 6e43d5987921bde189640cc1e37661f7f75c9c0b: Merge made by the 'recursive' strategy.eb37e74 HEAD@{5}: rebase -i (finish): returning to refs/heads/mastereb37e74 HEAD@{6}: rebase -i (pick): Commit C97436c6 HEAD@{7}: rebase -i (start): checkout 97436c6eec6396c63856c19b6a96372705b08b1b...88f1867 HEAD@{12}: commit: Commit D97436c6 HEAD@{13}: checkout: moving from 97436c6eec6396c63856c19b6a96372705b08b1b to test97436c6 HEAD@{14}: checkout: moving from master to 97436c605cc326 HEAD@{15}: commit: Commit C6e43d59 HEAD@{16}: commit: Commit B

This output shows the repository history, including:

  • The commit SHA.
  • How many HEAD-changing actions ago the commit was made (HEAD@{12} was 12 HEAD-changing actions ago).
  • The action that was taken, for example: commit, rebase, merge.
  • A description of the action that changed HEAD.

Undo remote changes without changing history

To undo changes in the remote repository, you can create a new commit with the changes youwant to undo. You should follow this process, which preserves the history andprovides a clear timeline and development structure. However, youonly need this procedure if your work was merged into a branch thatother developers use as the base for their work.

To revert changes introduced in a specific commit B:

git revert B

Undo remote changes while changing history

You can undo remote changes and change history.

Even with an updated history, old commits can still beaccessed by commit SHA. This is the case at least until all the automated cleanupof detached commits is performed, or a cleanup is run manually. Even the cleanup might not remove old commits if there are still refs pointing to them.

When changing history is acceptable

You should not change the history when you’re working in a public branchor a branch that might be used by other developers.

When you contribute to large open source repositories, like GitLab,you can squash your commits into a single one.

To squash commits on your branch to a single commit on a target branchat merge, use git merge --squash.

Never modify the commit history of your default branch or shared branch.

How to change history

A branch of a merge request is a public branch and might be used byother developers. However, the project rules might requireyou to use git rebase to reduce the number ofdisplayed commits on target branch after reviews are done.

You can modify history by using git rebase -i. Use this command to modify, squash,and delete commits.

## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Empty commits are commented out

If you decide to stop a rebase, do not close your editor.Instead, remove all uncommented lines and save.

Use git rebase carefully on shared and remote branches.Experiment locally before you push to the remote repository.

# Modify history from commit-id to HEAD (current commit)git rebase -i commit-id

Redact text

History

  • Introduced in GitLab 17.1 with a flag named rewrite_history_ui. Disabled by default.
  • Enabled on GitLab.com in GitLab 17.2.
  • Enabled on self-managed and GitLab Dedicated in GitLab 17.3.

Permanently delete sensitive or confidential information that was accidentally committed, ensuringit’s no longer accessible in your repository’s history.Replaces a list of strings with ***REMOVED***.

Alternatively, to completely delete specific files from a repository, seeRemove blobs.

Prerequisites:

  • You must have the Owner role for the instance.

To redact text from your repository:

  1. On the left sidebar, select Search or go to and find your project.
  2. Select Settings > Repository.
  3. Expand Repository maintenance.
  4. Select Redact text.
  5. On the drawer, enter the text to redact.You can use regex and glob patterns.
  6. Select Redact matching strings.
  7. On the confirmation dialog, enter your project path.
  8. Select Yes, redact matching strings.
  9. On the left sidebar, select Settings > General.
  10. Expand Advanced.
  11. Select Run housekeeping.

Delete sensitive information from commits

You can use Git to delete sensitive information from your past commits. However,history is modified in the process.

To rewrite history withcertain filters,run git filter-branch.

To remove a file from the history altogether use:

git filter-branch --tree-filter 'rm filename' HEAD

The git filter-branch command might be slow on large repositories.Tools are available to execute Git commands more quickly.These tools are faster because they do not provide the samefeature set as git filter-branch does, but focus on specific use cases.

For more information about purging files from the repository history and GitLab storage,see Reduce repository size.

Undo commits by removing them

  • Undo your last commit and put everything back in the staging area:

    git reset --soft HEAD^
  • Add files and change the commit message:

    git commit --amend -m "New Message"
  • Undo the last change and remove all other changes,if you did not push yet:

    git reset --hard HEAD^
  • Undo the last change and remove the last two commits,if you did not push yet:

    git reset --hard HEAD^^

Git reset sample workflow

The following is a common Git reset workflow:

  1. Edit a file.
  2. Check the status of the branch:

    git status
  3. Commit the changes to the branch with a wrong commit message:

    git commit -am "kjkfjkg"
  4. Check the Git log:

    git log
  5. Amend the commit with the correct commit message:

    git commit --amend -m "New comment added"
  6. Check the Git log again:

    git log
  7. Soft reset the branch:

    git reset --soft HEAD^
  8. Check the Git log again:

    git log
  9. Pull updates for the branch from the remote:

    git pull origin <branch>
  10. Push changes for the branch to the remote:

    git push origin <branch>

Undo commits with a new replacement commit

git revert <commit-sha>

The difference between git revert and git reset

  • The git reset command removes the commit. The git revert command removes the changes but leaves the commit.
  • The git revert command is safer, because you can revert a revert.
# Changed filegit commit -am "bug introduced"git revert HEAD# New commit created reverting changes# Now we want to re apply the reverted commitgit log # take hash from the revert commitgit revert <rev commit hash># reverted commit is back (new commit created again)

Unstage changes

When you stage a file in Git, you instruct Git to track changes to the file inpreparation for a commit. To disregard changes to a file, and notinclude it in your next commit, unstage the file.

Unstage a file

  • To remove files from staging, but keep your changes:

    git reset HEAD <file>
  • To unstage the last three commits:

    git reset HEAD^3
  • To unstage changes to a certain file from HEAD:

    git reset <filename>

After you unstage the file, to revert the file back to the state it was in before the changes:

git checkout -- <file>

Remove a file

  • To remove a file from disk and repository, use git rm. To remove a directory, use the -r flag:

    git rm '*.txt'git rm -r <dirname>
  • To keep a file on disk but remove it from the repository (such as a file you wantto add to .gitignore), use the rm command with the --cache flag:

    git rm <filename> --cache

These commands remove the file from current branches, but do not expunge it from your repository’s history.To completely remove all traces of the file, past and present, from your repository, seeRemove blobs.

  • git blame
  • Cherry-pick
  • Git history
  • Revert an existing commit
  • Squash and merge
Undo changes | GitLab (2024)
Top Articles
Selling A House With Foundation Issues | We Buy Ugly Houses®
__symbol__ Stock Quote Price and Forecast | CNN
Ron Martin Realty Cam
Craigslist Benton Harbor Michigan
Craigslist Portales
What's New on Hulu in October 2023
Sinai Web Scheduler
Remnant Graveyard Elf
Danielle Longet
Aita Autism
Transformers Movie Wiki
Bros Movie Wiki
The Binding of Isaac
Peraton Sso
Best Suv In 2010
Haunted Mansion Showtimes Near Millstone 14
Xxn Abbreviation List 2023
Missed Connections Inland Empire
Craigslist Appomattox Va
Coomeet Premium Mod Apk For Pc
Cain Toyota Vehicles
Foolproof Module 6 Test Answers
Sam's Club Gas Price Hilliard
Craigslist Apartments In Philly
Wbap Iheart
Penn State Service Management
Planned re-opening of Interchange welcomed - but questions still remain
Ghid depunere declarație unică
Current Time In Maryland
Www Violationinfo Com Login New Orleans
CVS Near Me | Somersworth, NH
Hisense Ht5021Kp Manual
Go Smiles Herndon Reviews
Ukg Dimensions Urmc
Tugboat Information
Gary Lezak Annual Salary
Nsav Investorshub
140000 Kilometers To Miles
World Social Protection Report 2024-26: Universal social protection for climate action and a just transition
Ferguson Showroom West Chester Pa
Doe Infohub
Advance Auto.parts Near Me
Mybiglots Net Associates
Southwest Airlines Departures Atlanta
Ssc South Carolina
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
Vagicaine Walgreens
Random Animal Hybrid Generator Wheel
Enter The Gungeon Gunther
Jasgotgass2
Jesus Calling Oct 6
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5668

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.