March 4, 2025

Git: One-Stop Solution for Amending Commits, Rebasing, and Best Practices

Git is an essential tool for developers, but it can sometimes be confusing, especially for beginners or those new to advanced workflows like amending commits, rebasing, and handling SSH authentication. This guide will help you understand Git in a way that even a 10-year-old or a seasoned developer can follow. By the end, you'll have a future-ready workflow for handling Git like a pro.


1๏ธโƒฃ Understanding Git Simply

Think of Git like a time machine for your code. Every time you save changes (commit), you're creating a snapshot of your project. If something goes wrong, you can travel back in time and restore your project. Cool, right? ๐Ÿš€


2๏ธโƒฃ Amending a Commit (Fixing a Mistake ๐Ÿ› ๏ธ)

Sometimes, you commit a change and realize, "Oops! I forgot to add a file" or "I need to tweak my message." Instead of creating a new commit, you can amend the last one:

git commit --amend

If you just want to add new changes without modifying the commit message:

git commit --amend --no-edit

๐Ÿ”ด Warning: If you've already pushed the commit, you need to force push:

git push --force

Use this only when working alone or after notifying your team. Otherwise, it can rewrite history and mess up othersโ€™ work.


3๏ธโƒฃ Rebasing (Keeping History Clean ๐Ÿ“œ)

Rebasing is like cleaning up your code's history before sharing it with others. Instead of showing every small step, it makes your changes look like they were made in an organized way.

โœจ How to Rebase Interactively:

git rebase -i HEAD~N

๐Ÿ”น Replace N with the number of commits you want to modify. ๐Ÿ”น Options youโ€™ll see:

  • pick โ†’ Keep commit as it is.
  • reword โ†’ Change commit message.
  • edit โ†’ Modify the commitโ€™s content.
  • squash โ†’ Merge commits into the previous one.
  • drop โ†’ Remove commit.

๐Ÿค” When to Use Rebase vs Merge?

  • Rebase (Good for personal branches, keeps history clean)
  • Merge (Good for shared branches, keeps commit history intact)

To rebase onto main:

git checkout feature-branch
git rebase main

If there are conflicts:

git rebase --continue

4๏ธโƒฃ Setting Up SSH for Secure Git Access ๐Ÿ”

Using SSH lets you push and pull code securely without typing passwords every time.

โœ… Steps to Set Up SSH:

  1. Check if SSH keys already exist:
    ls -al ~/.ssh
    
  2. Generate a new SSH key (if needed):
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  3. Add SSH key to agent:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    
  4. Copy the SSH key and add it to GitHub/GitLab:
    cat ~/.ssh/id_rsa.pub
    
  5. Test the SSH connection:
    ssh -T git@github.com
    
    If successful, you should see:
    Hi <username>! You've successfully authenticated.
    

5๏ธโƒฃ Must-Know Git Cheat Sheet ๐Ÿ“œ

๐Ÿ“Œ Essential Git Commands

git clone <repo_url>   # Copy a repository to your machine
git status             # Check status of files
git add .              # Add all files to staging
git commit -m "Message" # Commit changes
git push origin <branch> # Push changes

โช Undo Mistakes

git reset --soft HEAD~1  # Undo last commit, keep changes
git reset --hard HEAD~1  # Undo last commit, discard changes
git checkout -- .        # Discard uncommitted changes

๐Ÿ”€ Branch Management

git branch <new-branch>  # Create new branch
git checkout <branch>    # Switch branch
git merge <branch>       # Merge branches

๐Ÿ’พ Stashing (Temporary Save)

git stash                # Save changes temporarily
git stash pop            # Restore saved changes

๐Ÿ“œ View Logs

git log --oneline --graph --decorate --all

6๏ธโƒฃ Best Practices for a Future-Ready Git Workflow ๐Ÿ†

โœ… Write Meaningful Commit Messages

  • BAD: fixed bug
  • GOOD: Fixed login issue causing incorrect redirection

โœ… Commit Only Whatโ€™s Necessary

  • Avoid committing .env, node_modules/, or .DS_Store

โœ… Use Branches for Features and Fixes

  • Keep main stable, work on feature-xyz branches

โœ… Rebase Before Merging to Keep History Clean

git rebase main

โœ… Use git pull --rebase Instead of git pull

  • Avoids unnecessary merge commits

โœ… Use git push --force-with-lease Instead of git push --force

  • Prevents accidental overwrites by checking if someone else pushed first

โœ… Tag Releases for Easier Debugging

git tag -a v1.0 -m "Release version 1.0"
git push origin v1.0

โœ… Set Up .gitignore to Avoid Unwanted Files

echo "node_modules/" >> .gitignore
git rm -r --cached node_modules/

โœ… Regularly Prune Old Branches

git branch -d old-branch

โœ… Automate Git Hooks for Code Formatting & Security Checks

cp pre-commit .git/hooks/
chmod +x .git/hooks/pre-commit

๐ŸŽฏ Conclusion

Git doesnโ€™t have to be intimidating! With these best practices, cheats, and SSH setup, you now have a future-ready workflow that will make your life easier as a developer. ๐Ÿš€

๐Ÿ”น Keep your commits clean. ๐Ÿ”น Use branches wisely. ๐Ÿ”น Secure your repo with SSH. ๐Ÿ”น Automate where possible.

Happy coding! ๐ŸŽ‰