Git - lucyberryhub/WPF.Tutorial GitHub Wiki

๐Ÿ“ Git Tutorial! ๐Ÿ“

๐ŸŒŸ Today, weโ€™re going to learn how to do rebasing, amending commits, cleaning up your repository, and force pushing changes โ€” all with a little cherry magic! ๐Ÿ’โœจ


1. Setting Your Safe Directory in Git

Before we start, letโ€™s make sure Git knows that youโ€™re working in a safe directory! ๐Ÿ˜‡ This step is important to avoid warnings in your terminal. ๐Ÿงธ

  • Run this command to allow Git to safely work anywhere:
git config --global --add safe.directory "*"

Now, Git will know that youโ€™re working in a safe environment! ๐ŸŒธ


2. Rebasing with Git: Editing Commits

Want to go back in time and change something you wrote in your Git history? ๐ŸŒŸ You can rebase to edit your commits! Letโ€™s do it together step by step! ๐Ÿ’–

  1. Start the rebase: Type this command to open up your commit history:

    git rebase -i --root
    
  2. Enter Insert Mode: Now, Git will open the commit history. To edit, press i to enter INSERT mode! ๐ŸŽ€

  3. Change pick to reword: Find the commit you want to edit and change the word pick to reword. Like this:

    reword <commit-hash> <commit-message>
    

    This will allow you to change the message of that commit! ๐ŸŒธโœจ

  4. Exit Insert Mode: After editing, press the ESC key to exit INSERT mode. ๐Ÿ’•

  5. Save and Quit: Now, to save your changes and close the editor, type :wq and press Enter. ๐Ÿ“

  6. Continue the rebase: After saving, Git will pause and ask if youโ€™re ready to finish the rebase. To continue, type this:

    git rebase --continue
    
  7. Force Push the Changes: Once the rebase is finished, we need to push the changes! But since we edited history, we need to force push to update the remote branch:

    git push origin <branch-name> --force
    

    (Replace <branch-name> with your actual branch name, like main or feature-branch.)


3. Resetting and Amending Your Commit

Sometimes, you need to reset your commits or change a commit message. ๐Ÿ’ Letโ€™s do that now! ๐Ÿ’–

  1. Soft Reset: This command will allow you to move to a previous commit and keep your changes:

    git reset --soft <commit-hash>
    

    (Replace <commit-hash> with the ID of the commit you want to go back to.)

  2. Amend the Commit: Now, letโ€™s edit your commit message:

    git commit --amend -m "New commit message"
    
  3. Force Push: After amending the commit, donโ€™t forget to force push:

    git push origin <branch-name> --force
    

4. Fixing Missing Objects in Git

If you ever run into missing objects in your repository, donโ€™t worry! We can fix that with some cherry berry magic! ๐Ÿ’โœจ

  1. Run Git fsck: This command will check the integrity of your repository and make sure there are no missing objects:

    git fsck --full
    
  2. Run Git GC (Garbage Collection): To clean up unnecessary files and optimize your repository:

    git gc --prune=now
    

5. Packing and Cleaning Up Your Git Repository

Keep your repository nice and tidy! Hereโ€™s how to pack and clean up the files:

  1. Expire Old Reflog Entries: To remove old references:

    git reflog expire --expire=now --all
    
  2. Run Git GC Again: Clean up all the unnecessary files and optimize your repo again:

    git gc --prune=now
    
  3. Repack the Repository: Pack up all the objects in the repository to make it more efficient:

    git repack -A -d
    
  4. Check for Errors Again: Finally, make sure everything is working fine:

    git fsck --full
    

6. Handling Unrelated Histories (Git Pull)

Sometimes, when you're trying to pull changes, you might get an error about unrelated histories. ๐Ÿ“ Donโ€™t worry! You can fix it like a pro! ๐Ÿ’•

  • To pull changes from the main branch while allowing unrelated histories:
git pull origin main --allow-unrelated-histories

๐ŸŒธ Cherry Berry Recap! ๐ŸŒธ

Hereโ€™s a super quick recap of all the steps! ๐Ÿ’

  1. Setting the Safe Directory:

    git config --global --add safe.directory "*"
    
  2. Rebase Your Commits:

    git rebase -i --root
    
    • Change pick to reword and edit your commit message! ๐Ÿ’–
    • Save and quit with :wq ๐ŸŽ€
    • Continue with git rebase --continue
    • Force push with git push origin <branch-name> --force
  3. Reset and Amend Your Commit:

    git reset --soft <commit-hash>
    git commit --amend -m "New commit message"
    git push origin <branch-name> --force
    
  4. Fix Missing Objects:

    git fsck --full
    git gc --prune=now
    
  5. Clean Up Your Repository:

    git reflog expire --expire=now --all
    git gc --prune=now
    git repack -A -d
    git fsck --full
    
  6. Pull Unrelated Histories:

    git pull origin main --allow-unrelated-histories