Welcome back to another post in my GitHub series! If you've been following along, you know how important Git is for developers, helping us manage our code and collaborate smoothly.

Today, we're going to explore some key Git tasks, like deleting commit history, undoing resets, and using cherry-pick. I'll also guide you through resolving conflicts and explain the difference between git rebase and git merge.

Let’s dive in!

Delete commit history from the Github

When you’re starting out with web development, it's possible to accidentally push sensitive files, like your .env file with AWS credentials, to our public GitHub repository. Even if you remove or update these files, they can still be visible in the commit history. Here’s how you can remove these sensitive commits from your GitHub repository:

Steps to Remove "n" previous commit

  1. Switch to Your Main Branch:

     git checkout main
    

    Ensure you're on the correct branch from which you want to remove commits.

  2. Remove the Last 15 Commits:

     git reset --hard HEAD~15
    

    This command reverts your branch to the state it was in 15 commits ago. Adjust the number if you need to go further back.

  3. Force Push the Changes:

     git push origin main --force
    

    This updates the remote repository to match your local branch, effectively removing the unwanted commits.

By following these steps, you can help protect your sensitive information and maintain a clean repository.

To Rollback the Reset and Restore the Removed Commit:

You can retrieve the commit if you haven't done too much work after the reset. Git keeps a record of changes, so you can recover the commit using the following steps:

  1. Find the lost commit with git reflog:

    git reflog logs all actions you performed, including resets, and it includes the commit that was discarded during the git reset

     git reflog
    
  2. Reset to the commit:

     git reset --hard <commit-sha>
    
  3. Force-push the changes:

     git push origin main --force
    

This way, you should be able to restore the commit you removed and push it back to the remote branch.

Cherry-pick

git cherry-pick is a Git command used to apply the changes introduced by existing commits onto your current branch. This command is particularly useful when you want to incorporate specific changes from one branch into another without merging the entire branch.

Basic Usage

The basic syntax for git cherry-pick is:

git cherry-pick <commit-hash>

Steps to Cherry-Pick a Commit

  1. Identify the Commit: Find the commit hash of the commit you want to cherry-pick. You can use git log to see the commit history and identify the hash.

  2. Cherry-Pick the Commit: Run the git cherry-pick command followed by the commit hash.

git cherry-pick a1b2c3d4

Example Scenario

Suppose you have two branches, feature and main. You want to apply a specific commit from the feature branch to the main branch.

  1. Check out the main branch:
git checkout main
  1. Cherry-pick the commit from the feature branch:
shCopy codegit cherry-pick a1b2c3d4

Handling Conflicts

If the changes in the commit you are cherry-picking conflict with the changes in your current branch, Git will mark the conflicts in the affected files. You'll need to resolve these conflicts manually:

  1. Open the files with conflicts and resolve them.

  2. Stage the resolved files:

git add <file>
  1. Complete the cherry-pick process:
git cherry-pick --continue

If you decide not to proceed with the cherry-pick, you can abort it:

git cherry-pick --abort

Advanced Usage

You can also cherry-pick multiple commits at once by specifying a range of commit hashes:

git cherry-pick <start-commit-hash>^..<end-commit-hash>

git cherry-pick is a powerful tool for selectively integrating changes across branches. It allows for granular control over what changes to incorporate, making it valuable in various development workflows.

git rebase vs git merge

Both git rebase and git merge both are used to merge the branches, from the main branch to another branch.

But the commit history will be in the arranged order of the merged branch if we do the merge branch through git rebase.

If you like this blog, please do like it.

Check out my Portfolio website for connecting with me or just to say Hi !!.

Did you find this article valuable?

Support Anurag's blog by becoming a sponsor. Any amount is appreciated!