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! ๐
-
Start the rebase: Type this command to open up your commit history:
git rebase -i --root
-
Enter Insert Mode: Now, Git will open the commit history. To edit, press
i
to enter INSERT mode! ๐ -
Change
pick
toreword
: Find the commit you want to edit and change the wordpick
toreword
. Like this:reword <commit-hash> <commit-message>
This will allow you to change the message of that commit! ๐ธโจ
-
Exit Insert Mode: After editing, press the
ESC
key to exit INSERT mode. ๐ -
Save and Quit: Now, to save your changes and close the editor, type
:wq
and pressEnter
. ๐ -
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
-
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, likemain
orfeature-branch
.)
3. Resetting and Amending Your Commit
Sometimes, you need to reset your commits or change a commit message. ๐ Letโs do that now! ๐
-
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.) -
Amend the Commit: Now, letโs edit your commit message:
git commit --amend -m "New commit message"
-
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! ๐โจ
-
Run Git fsck: This command will check the integrity of your repository and make sure there are no missing objects:
git fsck --full
-
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:
-
Expire Old Reflog Entries: To remove old references:
git reflog expire --expire=now --all
-
Run Git GC Again: Clean up all the unnecessary files and optimize your repo again:
git gc --prune=now
-
Repack the Repository: Pack up all the objects in the repository to make it more efficient:
git repack -A -d
-
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! ๐
-
Setting the Safe Directory:
git config --global --add safe.directory "*"
-
Rebase Your Commits:
git rebase -i --root
- Change
pick
toreword
and edit your commit message! ๐ - Save and quit with
:wq
๐ - Continue with
git rebase --continue
- Force push with
git push origin <branch-name> --force
- Change
-
Reset and Amend Your Commit:
git reset --soft <commit-hash> git commit --amend -m "New commit message" git push origin <branch-name> --force
-
Fix Missing Objects:
git fsck --full git gc --prune=now
-
Clean Up Your Repository:
git reflog expire --expire=now --all git gc --prune=now git repack -A -d git fsck --full
-
Pull Unrelated Histories:
git pull origin main --allow-unrelated-histories