| Title | Remark/Code |
|---|---|
| Introduction to Git | What is Git? Why use Git? Difference between Git and GitHub |
| Installation | Install Git on Windows, macOS, Linux |
| Git Basics | Understanding repositories, commits, branches, remotes |
| Setting Up Git | Configure username, email, aliases using git config |
| Alias Setup | Create shortcuts for long Git commands |
| Basic Git Commands | init status commit log add diff checkout reset |
| Creating a Repository | Initialize and configure a new repository |
| Working with Files | Adding, modifying, deleting files in Git |
| .bashrc file | Customize Git command aliases |
| .gitignore file | Ignore files and directories from tracking |
| Editing Files | Read/edit files using cat vim nano code |
| Staging and Committing | add commit commit --amend |
| Branching & Merging | Managing branches and merging changes |
| Creating & Switching Branches | branch checkout switch |
| Merging Branches | merge rebase cherry-pick |
| Rebasing | rebase rebase --interactive |
| Squashing Commits | rebase -i to combine multiple commits |
| Cherry-Picking | Apply specific commits from another branch |
| Working with Remote Repositories | Connecting local Git to remote repositories |
| Adding a Remote Repository | git remote add origin |
| Cloning a Repository | git clone to copy a remote repository |
| Fetching, Pulling & Pushing | git fetch git pull git push |
| Moving Local Repo to Remote | git remote add git push -u origin |
| Undoing & Resetting Changes | Fix mistakes and revert changes |
| Undoing Changes | reset restore checkout reflog |
| Unstaging Files | restore reset HEAD clean |
| Reverting Commits | revert commit --amend |
| Reset, Checkout, Restore | checkout reset restore |
| Restoring a File to an Earlier Version | restore checkout reset |
| Viewing and Comparing Changes | Track modifications in your project |
| Viewing Staged & Unstaged Changes | diff diff --staged difftool |
| Comparing Commits | diff log --oneline |
| Viewing the History | log log --graph |
| Viewing a Commit | show ls-tree |
| Handling Merge Conflicts | Resolve issues when merging branches |
| Troubleshooting Merge Conflicts | Using merge rebase conflict resolution |
| Working with Stashes | stash stash pop stash apply stash drop |
| Git Tagging & Releases | Manage software versions using tags |
| Tagging Commits | git tag git tag -a git tag -d |
| Working with Feature Branches | checkout -b merge rebase |
| Advanced Git Features | Powerful Git tools for workflow |
| Git Hooks | Automate tasks with pre/post-commit hooks |
| Git Submodules | submodule add submodule update |
| Interactive Rebase | rebase -i to modify commit history |
| Bisecting | git bisect for debugging commits |
| Git Collaboration | Best practices for working with teams |
| Forking and Pull Requests | Fork, clone, modify, submit PRs |
| Code Reviews with GitHub | Using GitHub for reviews and approvals |
| Security and Best Practices | Keep your codebase secure |
| Signing Commits | Use GPG to sign Git commits |
| Git Best Practices | Writing good commit messages, branch strategies |
| Git for DevOps & Automation | Using Git in CI/CD pipelines |
| Git in CI/CD Pipelines | Automate testing and deployments |
| Git Internals & Performance | Deep dive into Git |
| Understanding Git Internals | Objects, trees, and commits |
| Optimizing Git Performance | gc fsck repack to clean and speed up |
| Git Wiki Repository | Documenting with Git |
| Wiki Repository | Manage GitHub Wiki repositories |
Git is an essential tool for version control, enabling efficient collaboration and code management. This tutorial provides a detailed walkthrough of key Git commands, including rebasing, amending commits, cleaning up repositories, and force pushing changes.
To ensure Git recognizes your working directory as safe and prevents unnecessary warnings, execute the following command:
git config --global --add safe.directory "*"Rebasing allows you to modify commit history. Follow these steps to edit commits:
-
Initiate an Interactive Rebase:
git rebase -i --root
-
Modify Commit Messages:
- The command opens an interactive rebase interface.
- Change
picktorewordfor the commit you wish to edit.
reword <commit-hash> <commit-message>
-
Save and Exit:
- Press
ESC, then type:wqand pressEnterto save changes.
- Press
-
Continue the Rebase Process:
git rebase --continue
-
Force Push Updated Commits:
git push origin <branch-name> --force
(Replace <branch-name> with your actual branch name.)
To reset or modify commit messages, follow these steps:
-
Soft Reset to a Previous Commit:
git reset --soft <commit-hash>
-
Amend a Commit Message:
git commit --amend -m "Updated commit message" -
Push Amended Commit:
git push origin <branch-name> --force
If Git reports missing objects, perform the following steps:
-
Check Repository Integrity:
git fsck --full
-
Optimize and Cleanup the Repository:
git gc --prune=now
To maintain repository efficiency:
-
Remove Old References:
git reflog expire --expire=now --all
-
Run Garbage Collection:
git gc --prune=now
-
Repack the Repository:
git repack -A -d
-
Validate Repository Health:
git fsck --full
When pulling changes from different repositories, you may need to allow unrelated histories:
git pull origin main --allow-unrelated-historiesTo modify commit messages in Git using interactive rebase, follow these steps:
-
Start the Rebase: Open your commit history in interactive mode:
git rebase -i --root
-
Enter Insert Mode: Git will open the commit history in your configured text editor. To make edits, press
ito enter INSERT mode. -
Change
picktoreword: Locate the commit you want to edit and replacepickwithreword. For example:reword <commit-hash> <commit-message>
This allows you to modify the commit message.
-
Exit Insert Mode: Once you've made the necessary changes, press the
ESCkey to exit INSERT mode. -
Save and Quit: Save the changes and close the editor by typing:
:wq
Then press
Enter. -
Continue the Rebase: After saving, Git will prompt you to confirm the rebase. To proceed, run:
git rebase --continue
This process ensures that your commit history remains structured while allowing you to refine commit messages effectively.
| Task | Command |
|---|---|
| Set safe directory | git config --global --add safe.directory "*" |
| Start interactive rebase | git rebase -i --root |
| Amend commit message | git commit --amend -m "Updated message" |
| Force push changes | git push origin <branch-name> --force |
| Check for missing objects | git fsck --full |
| Run garbage collection | git gc --prune=now |
| Remove old references | git reflog expire --expire=now --all |
| Merge unrelated histories | git pull origin main --allow-unrelated-histories |
-
Long-Running Branches (main, master, develop, staging, production branch )
- exist through the complete lifetime of the project
- often they mirror "stages" in your dev life cycle.
-
Short-lived branches (feature branch)
- for new features, bug fixes, refactorings, experiments...
- will be deleted after integration (merge/rebase)
-
Two Branching Strategies
- GitHub Flow - very simple, very lean: only one long-running branch(main or master) + feature branches.
- GitFlow - more structure, more rules...
- long-running : main + develop
- short-lived : features, releases, hotfixes.
| Title | Remark/code |
|---|---|
| Installation | Setup and Configuration GIT config |
| Alias Setup | shortcut for those long long commands |
| Basic Operation | init status commit cat list ls echo touch rm |
| Create Repository | https://stackoverflow.com/questions/20325089/hosting-a-git-server-on-localhost |
| .bashrc file | Create .bashrc file to store your own alias |
| .gitignore file | https://www.codegrepper.com/code-examples/csharp/aspnet+core+gitignore |
| Editing File | read file cat and edit file vim or code |
| Move your project to Remote | remote push |
| Restoring a File to an Earlier Version | restore |
| Trouble Shooting Conflict | stash |
| Unstaging Files | restore clean |
| Viewing Staged & Unstaged Changes | diff --staged difftool |
| Viewing the History | log |
| Viewing a Commit | show ls-tree |
| Workflow basic | Clone Pull Push from Remote and create branch branch merge |
| Workflow with feature branch | using tag |
| Wiki Repository | |
| Clone Repository | clone all your GitHub repositories and their corresponding wiki repositories at once |
| ■ Create Branch | |
| ■ Pull Requests | Communicating about and reviewing code |
| ■ Merge | |
| ■ Merge Conflicts | When Integrating commits from different Sources |
| ■ Rebase | Integrating with rebase -> a straight line of commits |
| ■ Cherry-Picking | Integrating Single, Specific Commits |
- Create branch
git branch test
git checkout test
- set your remote origin repository
git remote add origin /C/Apache24/htdocs/potatoscript.git
- make some change and commit and push it into the remote branch
- the
--set-upstreamallows you to set the default remote branch for your current local branch - By default, every pull command sets the master as your default remote branch
git push --set-upstream origin test
- without a
Pull Requestyou would jump right to merging your code. - Invites reviewers to provide feedback before merging
- Contributing code to other repositories that you don't have right access
- normally we will
Forkthe other people origin repository that you don't have the right to make changes
and clone it into your local repository to do editing.
- Merging Branches = Integrating one Branch into Another
- To merge branches locally, use git checkoutto switch to the branch you want to merge into.
- Next, use git mergeand specify the name of the other branch to bring into this branch.
git checkout main
git merge feature/feature1
- When contradictory changes happen
- To solve the conflicts use
git rebase,git pull,git cherry-pick,git stash apply- use the git mergetool like
www.git-tower.com
- Interactive Rebase
- Change a commit's message
- delete commits
- reorder commits
- combine multiple commits into one
- edit/ssplit an existing commits into multiple new ones.
- Do NOT use Rebase on commits that you have already pushed/shared on a remote repository!!!
- Instead, use it for cleaning up your local commit history before merging it into a shared team branch.
-
the mechanism of
git rebase branch-B -
step 1 :
- git rebase -i HEAD~3
- removed all commits from branch A and temporary keep them somewhere
-
step 2 : create a straight line of commits
- use
git log --onelineto check the commit status and id - use
git rebase -i HEAD~3to set the range of commit to rebase - next determine the desired action (reword or squash) only and don't edit the comment
- reword -> edit comment
reword 6bcf266 Optimize markup structure in inddpage pick 762317c Change the page structure pick del3564 Improve headline for improve- squash -> combine commit
pick 6bcf266 Optimize markup structure in inddpage squash 762317c Change the page structure pick del3564 Improve headline for improvethis will combine
6bcf266and762317cinto new commit with new Id - use
-
step 3 : return back the commits of branch A with the new Id
- allows you to select individual commits to be integrated
- therfore use it for moving a commit to a different branch
- when you commit to the wrong branch
for example: by using merge C2 and C4 from branch feature-X will all moved to branch master
by using cherry pick you can choice C2 or C4 only to move to branch master
C1--------C3--------C5--------master
\
C2--------C4--------------feature-X
- Usage Example:
- WHen you committed on the wrong branch
- C3 should have been on feature-X
C1--------C3-----------master
\
C2------------------feature-X
- first we shift to
feature-Xbranch and apply cherry pick on the target commit
git checkout feature/neweletter
git cherry-pick 26bf1648
- next clean up the commit from the master branch
git checkout master
git reset --hard HEAD~1 #this will remove the C3 from master branch
-
Open your terminal (Git Bash, macOS Terminal, or Linux Terminal).
-
Generate a new SSH key using the following command, replacing
your_email@example.comwith your GitHub email address:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"This will create a new SSH key. You’ll be prompted to choose a location for saving the key (press Enter to save it to the default location
~/.ssh/id_rsa). -
If you want to add a passphrase for extra security, you can do so. If not, press Enter twice to skip.
Now, you need to add the new SSH key to your SSH agent.
-
Start the SSH agent in the background (for Linux/macOS):
eval "$(ssh-agent -s)"
-
Add the SSH private key to the SSH agent:
ssh-add ~/.ssh/id_rsa
-
To view the SSH public key, run the following command:
cat ~/.ssh/id_rsa.pubThis will display your SSH public key.
-
Copy the entire key (starting from
ssh-rsaand ending with your email address).
- Go to your GitHub account in your browser and sign in.
- Click on your profile picture in the top-right corner and select Settings.
- In the left sidebar, click SSH and GPG keys.
- Click the New SSH key button.
- In the Title field, give your SSH key a recognizable name (e.g., "My Laptop SSH Key").
- Paste the SSH public key you copied earlier into the Key field.
- Click Add SSH key.
Now that your SSH key is added to GitHub, test the connection:
- In your terminal, run the following command:
If everything is set up correctly, you should see a message like this:
ssh -T git@github.com
Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.
Finally, update your repository’s remote URL to use SSH instead of HTTPS:
-
Go to your repository on GitHub and click the Code button.
-
Copy the SSH URL, which will look like this:
git@github.com:potatoscript/your_repository.git -
Update the remote URL in your local repository:
git remote set-url origin git@github.com:potatoscript/your_repository.git
If you haven't already, generate two separate SSH keys for each GitHub account. You can do this using the following commands:
# For the first GitHub account
ssh-keygen -t rsa -b 4096 -C "your_email_1@example.com" -f ~/.ssh/id_rsa_account1
# For the second GitHub account
ssh-keygen -t rsa -b 4096 -C "your_email_2@example.com" -f ~/.ssh/id_rsa_account2- The
-Cflag is used to label the SSH key with the email associated with the account. - The
-fflag allows you to specify a custom filename for the key. The default isid_rsa, but you want to name them differently (e.g.,id_rsa_account1andid_rsa_account2).
Make sure your SSH agent is running, then add both keys to the agent:
# Start the SSH agent (if it's not already running)
eval "$(ssh-agent -s)"
# Add the first key
ssh-add ~/.ssh/id_rsa_account1
# Add the second key
ssh-add ~/.ssh/id_rsa_account2- Log into each GitHub account and add the corresponding public key to each account.
- For Account 1: Go to Settings → SSH and GPG keys → New SSH key, and paste the contents of
~/.ssh/id_rsa_account1.pubinto the form. - For Account 2: Do the same for
~/.ssh/id_rsa_account2.pubon your second GitHub account.
- For Account 1: Go to Settings → SSH and GPG keys → New SSH key, and paste the contents of
Now, edit the SSH config file to specify which key to use for each GitHub account.
# Open the SSH config file for editing
nano ~/.ssh/configAdd the following configuration:
# For the first GitHub account
Host github.com-account1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account1
# For the second GitHub account
Host github.com-account2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account2- The
Hostentry is a unique name you’ll use to differentiate between the two accounts when you interact with GitHub. - The
HostNameis alwaysgithub.com. - The
IdentityFilepoints to the respective private key for each account.
When cloning or interacting with a repository, use the Host alias you specified in the SSH config file:
- For the first account:
git clone git@github.com-account1:username/repository.git- For the second account:
git clone git@github.com-account2:username/repository.gitWhen you use the alias (github.com-account1 or github.com-account2), SSH will automatically use the appropriate key for each GitHub account.
If you're already working with repositories and want to switch between accounts, you just need to ensure the correct Host alias is used in the Git URL.
- For the first account, update the remote URL like this:
git remote set-url origin git@github.com-account1:username/repository.git- For the second account:
git remote set-url origin git@github.com-account2:username/repository.gitgit config --global --edit