- Agenda
- Version Control Systems (VCS)
- What is Git?
- How Git Works
- Git Setup & Important Git commands
- Visualizing Git
- Resources
- Track Changes:
- Helps developers manage changes to source code over time.
- Records every modification made to the codebase.
- Collaboration:
- Helps multiple developers to work on the same project simultaneously without overwriting each other’s work.
- Important because code development is not a linear process.
- History and Reversion:
- Maintains history of changes, allowing developers to revert to previous versions if needed.
- Single Central Repository: All users work with a single central repository, typically hosted on a server.
- Commit and Update: Developers commit their changes to this central repository and update their local copies to stay in sync with others.
- Pro:
- Simplified Management - Since all changes are tracked in a single central repository, it’s easier to manage and control the code base.
- Con:
- Single Point of Failure - If the central server goes down or becomes inaccessible, no one can commit changes or access the latest version of the code.
- Local Repositories: Each developer has a complete copy of the repository, including the full history of changes.
- Peer-to-Peer: Changes are shared between repositories, allowing for more flexible workflows and offline work.
- Pro:
- Enhanced Collaboration - Each developer has a full copy of the repository, allowing for more flexible workflows, offline work, and easier branching and merging.
- Con:
- Complexity - Managing multiple copies of the repository and coordinating changes between them can be more complex and require more sophisticated tools and practices.
- Git is a popular, free, and open-source Distributed Version Control System!
- Collaboration
- Version History
- Backup and Recovery
- Offline Work
- Conflict Resolution
[!NOTE] Git is everywhere! According to the Stack Overflow Developer Survey 2022, 95% of developers reported Git as their primary version control system.
- Local Repository: You have a repository on your local machine where you keep your files. This is your personal workspace where you make changes and save versions of your files.
- Remote Repository: There’s also a repository on a server (like GitHub, GitLab, or Bitbucket). This acts as a central hub where you can store your files and share them with others.
- Synchronization: You can synchronize your local repository with the remote one. This means you can upload your changes to the server (push) and download changes from the server (pull).
- Collaboration: If you’re working with others, they can also upload their changes to the remote repository. You can then download these changes to your local repository.
-
Install Git
- Download and install Git from git-scm.
- Verify installation by running the command below in your terminal:
git --version
-
Configure Git
- Set your username and email:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
- Set your username and email:
-
Initialize a Repository
- Navigate to your project directory and initialize Git:
cd /path/to/your/project git init
- Navigate to your project directory and initialize Git:
-
Check status
git status
-
Add Git Ignore (optional)
-
Stage changed files
git add filename
-
Commit staged files
git commit -m "Your commit message"
-
Create a Remote Repository
- Create a repository on a platform like GitHub.
- Link your local repo to the remote repo:
git remote add origin https://github.com/yourusername/your-repo.git
-
Push changes to the Remote Repository
- Push your changes:
git push -u origin master
[!NOTE] Git Forks
-
The
-u
(same as--set-upstream
) ingit push
is used to set the fork to be used. In the previous command,origin
is the name of the fork andmaster
is the name of the branch. -
What are Git Forks?
- Independent Copies: A Git fork is a complete copy of a repository that exists independently from the original.
- Separate Development: It allows developers to work on their own version of the project without affecting the original repository.
- Branches: Each fork can have its own branches, enabling multiple lines of development.
-
Why are Git Forks Used?
- Experimentation: Developers can test new features or changes in their own fork without risking the stability of the main project.
- Collaboration: Forks enable multiple developers to work on the same project simultaneously, each in their own space, before merging changes back into the original repository.
- Push your changes:
-
Pull Changes from the Remote Repository
git pull origin master
-
Branching
- Create a new branch
git branch new-branch
- Checkout (switch) to the new or an existing branch
git checkout new-branch
- Create a new branch
-
Merge Branches
- Switch to the main branch
git checkout master
- Merge the new branch into the main branch
git merge new-branch
- Switch to the main branch
-
View Commit History
git log
-
Cloning an existing Repository
git clone https://github.com/yourusername/your-repo.git
[!NOTE] Git Submodules
- Git submodules are a way to include one Git repository inside another as a subdirectory. This allows you to keep a separate project within your main project while maintaining its own version control.
- To clone a Git Repo with Submodules:
git clone --recurse-submodules https://github.com/yourusername/your-repo.git
-
Git Merge
- Switch to the branch you want to merge into
git checkout master
- Merge the feature branch into the master branch
git merge feature-branch
[!NOTE] Better Practice: Use Pull (Merge) Requests
- Code Review: Pull requests give other developers a chance to review the code before merging.
- Discussion: They provide a platform for discussing changes and suggesting improvements.
- Testing: Ensures that the code passes all tests and meets the project's standards before being merged. (CI/CD)
- Documentation: Pull requests serve as documentation for why changes were made and how they were implemented.
- Switch to the branch you want to merge into