Intro
Git is a very popular version control system, while GitHub is a service that allows you to upload, host, and manage your code using Git with a nice web interface.
Resources:
Pro Git Book
![]()
Ch. 1.1-1.4: learn the differences between local, centralized, and distributed version control systems.
- Git Cheat Sheets
- The Odin Project- Introduction to Git
- Git Tutorial for Beginners: Learn Git in 1 Hour
- Collaborating with pull requests
- MRG Training- Version Control
Getting Started
Tutorial: Setting Up Git, GitHub, and SSH
Common Git Workflow
Set up an SSH key for GitHub authentication
- See SSH
Git Basics
Tagging
Tagging is a functionality in Git used to mark specific commits as significant, often to denote release points (e.g., v1.0, v2.0). https://git-scm.com/book/en/v2/Git-Basics-Tagging
- View all existing tags in the repository.
git tag
# Sample output
v1.0
v2.0
- Create a lightweight tag to a specific commit.
git tag <tag-name>
- Tag a previous commit with annotation:
- Create a tag with additional metadata such as tagger name, email, date, and message.
git tag -a <tag-name> <commit-id>
- Show tag information:
git show <tag-name>
- Share tag with remote repository:
- Push a specific tag to the remote repository.
git push origin <tag-name>
Revert to Previous Commit
Using Git Reset
https://stackoverflow.com/questions/4372435/how-can-i-rollback-a-git-repository-to-a-specific-commit
- Revert back to a previous commit, deleting all changes since that commit (Dangerous in collaboration):
git reset --hard <old-commit-id> git push -f <remote-name> <branch-name>
Using Git Merge
Reverting to Previous Commit with Git
- Revert the file to the state of the commit prior to the specified
<commit-id>
:- Creates a new commit for the reverted changes and preserves the commit history.
git revert <commit-id>
Git Branching
Checkout a Remote Branch
https://www.freecodecamp.org/news/git-checkout-remote-branch-tutorial/ https://www.educative.io/answers/how-to-checkout-a-remote-branch-in-git
- Fetch All Remote Branches
git fetch origin
- List Branches Available for Checkout
git branch -a
- Make a Local Copy of a Remote Branch
git checkout -b <name-your-branch> origin/<name-of-remote-branch>
Git Tools
Submodules
- References
Submodules allow you to keep a Git repository as a subdirectory of another Git repository. Submodules are simply a reference to a specific commit in another repository
Add a submodule
- Add submodule
- By default, submodules will add the subproject into a directory named the same as the repository
- You can add a different path at the end of the command if you want it to go elsewhere
git submodule add <repo URL>
- init the submodule and clone the code to it
git submodule update --init --recursive
- Commit the changes
- On GitHub, the submodule folder will have a little indicator showing that it is a submodule
Update a submodule
Here’s a typical workflow to update a submodule:
-
Navigate to the Submodule Directory:
cd path/to/submodule
-
Pull the Latest Changes:
git pull origin main
-
Return to the Main Repository:
cd ../..
-
Stage the Updated Submodule Reference:
git add path/to/submodule
-
Commit the Update:
git commit -m "Update submodule to latest commit"
Cloning a Project with Submodules
- When you clone a repository that contains submodules, the submodules are not automatically cloned or initialized.
- You need to initialize your local configuration file, and run
git submodule update
to fetch all the data from that project and check out the appropriate commit listed in your superproject
- You need to initialize your local configuration file, and run
git clone <repository-url>
cd <repository-directory>
git submodule update --init --recursive