Showing posts with label SRE. Show all posts
Showing posts with label SRE. Show all posts

Thursday 16 May 2024

What are the steps to remove a Git branch from both my local machine and the remote repository?

Many times, I found myself having to delete Git repositories as part of my daily duties. Following some of the documentation online, I tried many times getting frustrated ..




Failed Attempts to Delete a Remote Branch:

$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.

$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.

$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).

$ git push
Everything up-to-date

$ git pull
From github.com:gituser/gitproject

* [new branch] bugfix -> origin/bugfix
Already up-to-date.


How do I properly delete the remotes/origin/bugfix branch both locally and remotely?


Monday 13 May 2024

How do I undo the most recent local commits in Git?

I accidentally committed the wrong files to Git but didn't push the commit to the server yet.

How do I undo those commits from the local repository?

This is a simple Guide. Here are the steps to undo the last commit from your local repository for both Linux and Windows users:

Step 1: Open your terminal (Command Prompt for Windows users).

Step 2: Navigate to your git repository.
cd /path/to/your/repository
Step 3: Run the following command to undo the last commit:
git reset --soft HEAD~1

This command will undo the last commit and bring the changes back into your staging area (i.e., the changes will be “uncommitted” but still present).

Step 4: If you want to completely remove the changes, instead of just “uncommitting”, you can use the following command:
git reset --hard HEAD~1

This command will permanently remove the last commit. Be careful with this command, as it permanently discards committed changes.

Note: These commands work the same way in both Linux and Windows. Open the terminal (Linux) or Command Prompt (Windows) as your first step.

A More complex answer here ðŸ‘‡ðŸ‘‡ðŸ‘‡ðŸ‘‡ðŸ‘‡ðŸ‘‡ 


Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.

git reset --hard <commit_id>

"Move" your HEAD back to the desired commit.


# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard HEAD~

# Alternatively, if there's work to keep (stash your changes before):
git stash -u
git reset --hard HEAD~
git stash pop
# This saves the modifications, then reapplies that patch after resetting.

# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.

enter image description here

  1. Make corrections to working tree files.
  2. git add anything that you want to include in your new commit.
  3. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEADcommit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message)commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1.

The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

Another answer which explain about HEAD and way to "recover" from commit mistakes:
How can I move HEAD back to a previous location? (Detached head) & Undo commits

How to Master the Perfect ChatGPT Prompt Formula (in just 8 minutes)!

Imagine having the power to get the exact response you need from ChatGPT —every time. Whether you’re looking for writing help, coding assi...