This documentation provides comprehensive guidelines for using GTI (Git Tool Interface) and GitHub, covering key functionalities, setup processes, and best practices.
Git is a version control system that allows you to track changes to your files and collaborate with others. It manages the history of your code and merges changes from different branches. In this tutorial, we'll cover essential Git and GitHub concepts and commands.
Git is like the superhero of version control systems, created by Linus Torvalds in 2005 and now maintained by Junio Hamano. Git helps track code changes, identify contributors, and simplify coding collaboration.
- Manages projects with repositories.
- Tracks changes with features like staging and committing.
- Supports branching and merging.
- Facilitates pulling and pushing updates.
- Tracking code changes.
- Tracking who made changes.
- Coding collaboration.
- Initialize Git: Transform a regular folder into a Git repository.
git init
- Track Changes: Git tracks when files are changed, added, or deleted.
git status
- Stage and Commit: Select changes to keep (stage) and create a permanent snapshot (commit).
git add <file> git commit -m "commit message"
- Time Travel: Revert to any previous version of your project.
- Popularity: Over 70% of developers use Git.
- Global Collaboration: Facilitates teamwork across distances.
- Full Project History: Tracks every change in your project.
- Undo Button: Allows rolling back to previous versions.
GitHub, owned by Microsoft, complements Git by providing additional tools and features. It hosts millions of repositories and supports collaboration on a global scale.
A repository is a central location for storing and managing code. It tracks changes, manages versions, and facilitates collaboration among developers.
-
Install Git:
- Windows: Download from Git for Windows.
- Mac: Use Homebrew with
brew install git
or download from Git for Mac. - Linux: Install via package manager, e.g.,
sudo apt-get install git
for Debian-based distributions.
-
Verify Installation:
git --version
-
Configure Git:
git config --global user.name "Your Name" git config --global user.email "[email protected]" git config --list
-
Install Visual Studio Code: Download from Visual Studio Code.
-
Install Git Extension in VS Code:
- Open VS Code.
- Go to the Extensions view and install "GitLens" for enhanced Git capabilities.
-
Change Default Code Editor:
git config --global core.editor "code --wait"
- Create a folder and open it in VS Code.
- Open the terminal in VS Code.
- Initialize a new Git repository:
git init
- Write Code: Make changes to your files.
- Add Files:
git add <file> git add .
- Commit Changes:
git commit -m "commit message"
- Check Status (optional):
git status
The git log
command displays the commit history of your repository. Using the --oneline
option condenses the output to show each commit in a single line.
git log --oneline
The .gitignore
file specifies which files or directories to ignore, ensuring they are not committed to the repository.
# Ignore node_modules directory
node_modules/
# Ignore all .log files
*.log
# Ignore specific file
secret.txt
The .gitkeep
file is an empty file that ensures a directory is tracked by Git, even if it has no files inside it.
- Create Repository: Start a new repository on GitHub.
- Push Changes: Connect your local repository to GitHub.
git remote add origin <repository_URL> git branch -M main git push -u origin main
- Clone Repository: Download a copy of a repository from GitHub to your local machine.
git clone <repository_URL>
GitHub Flow is a lightweight, branch-based workflow that supports teams and projects where deployments are made regularly. It encourages continuous integration and collaboration.
- Create a Branch: Start new work in a branch.
- Add Commits: Make regular commits to track progress.
- Open a Pull Request: Discuss changes with others before merging.
- Discuss and Review Code: Collaborate on feedback and improvements.
- Merge Pull Request: Incorporate changes into the main branch.
- Deploy: Automate deployments after merging.
Git branches are a core feature in version control, allowing developers to work on different parts of a project simultaneously without interfering with the main codebase.
A Git branch is a separate line of development. Think of it as a new copy of your project where you can make changes without affecting the main codebase.
- Parallel Developmentv : Work on multiple features or bug fixes at the same time.
- Isolation : Keep changes separate until theyβre ready to be merged.
- Collaboration : Multiple developers can work on different branches without conflicts.
To create a new branch, you can use the following command:
git branch
- This command lists all the branches in the current repository.
git branch
git branch BRANCH_NAME
- This command creates a new branch calledBRANCH_NAME
.
git branch BRANCH_NAME
git switch BRANCH_NAME
- This command switches to theBRANCH_NAME
branch.
git switch BRANCH_NAME
git switch -c another_branch
- This command creates a new branch. the-c
flag is used to create a new branch.
git switch -c another_branch
git checkout BRANCH_NAME
- This command switches to theBRANCH_NAME
branch.
git checkout BRANCH_NAME
git checkout -b BRANCH_NAME
- This command create and switches to theBRANCH_NAME
branch.
git checkout -b BRANCH_NAME
Renaming branches in Git can be an essential part of maintaining a clean and organized repository. Here's a simple guide to help you through the process.
- Clarify branch purpose: A clearer name can better describe the branch's function or contents.
- Fix typos: Correct any mistakes made when the branch was first created.
- Follow naming conventions: Ensure consistency and adherence to project guidelines.
- Rename the Current Branch
If you are on the branch you want to rename, follow these steps:
- Check the current branch:
git branch
The current branch will have an asterisk (*) next to it.
- Rename the branch:
git branch -m new-branch-name
Replace new-branch-name
with your desired branch name.
-
Rename a Different Local Branch If you are not on the branch you want to rename:
- Rename the branch:
git branch -m old-branch-name new-branch-name
Replace old-branch-name
with the current name and new-branch-name
with the desired name.
You can delete a branch using the following command:
git branch -d <branch-name>
Checkout a branch You can checkout a branch using the following command:
git checkout <branch-name>
Checkout a branch means that you are going to work on that branch. You can checkout any branch you want.
List all branches You can list all branches using the following command:
git branch
List all branches means that you are going to see all the branches in your repository.
Merging branches in Git is a common task that helps integrate changes from different lines of development. However, it can sometimes lead to conflicts that need to be resolved. Hereβs a simple guide to help you navigate through merging and handling conflicts effectively.
Merging combines the changes from two branches into one. Itβs essential for collaboration, ensuring that all contributions are integrated into the main codebase.
- Fast-forward merge: When the target branch has no new commits since the feature branch was created.
- Three-way merge: When both branches have unique commits.
- Checkout the target branch (e.g.,
main
ormaster
):git checkout main
- Pull the latest changes from the remote repository:
git pull origin main
- Merge the feature branch into the target branch:
git merge feature-branch
Conflicts occur when changes in different branches overlap. Hereβs how to manage them:
-
Identify the conflict:
- Git will mark conflicts in the files with special markers (e.g.,
<<<<<<< HEAD
).
- Git will mark conflicts in the files with special markers (e.g.,
-
Resolve the conflict:
- Open the conflicting file and edit the marked sections to choose or combine the changes.
-
Mark the conflict as resolved:
git add resolved-file
-
Commit the merge:
git commit -m "Resolved merge conflict"
Interactive rebase allows you to modify commit history by editing, combining, and deleting commits before pushing changes to the remote repository.
-
Start Interactive Rebase:
git rebase -i HEAD~n
-
Edit Commits:
- Pick commits to edit, reorder, or squash.
-
Complete Rebase:
- Save changes and exit.
- pick: Use commit as-is.
- reword: Use commit and edit commit message.
- edit: Use commit and stop for amending.
- squash: Combine with previous commit.
- drop: Remove commit.
Cherry-picking allows you to select specific commits from one branch and apply them to another branch.
-
Identify Commit:
- Find commit hash or reference.
-
Apply Commit:
git cherry-pick <commit_hash>
Git submodules allow you to include and manage external repositories within your own repository. They are useful for modularizing code and dependencies.
git submodule add <repository_URL> <path>
git submodule update --remote
GitHub Actions automate workflows, enabling you to build, test, and deploy your projects on GitHub.
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
Interactive rebase is a powerful Git tool that allows you to rewrite commit history in a flexible and controlled way. You can reorder, edit, combine, or delete commits.
-
Start an interactive rebase:
git rebase -i HEAD~3
This command will open an editor for the last 3 commits.
-
Pick a commit:
pick abc1234 Commit message
-
Edit a commit:
edit abc1234 Commit message
-
Squash commits:
squash abc1234 Commit message
- Start an interactive rebase for the last 3 commits.
- Change the order of commits or mark commits to be edited or squashed.
- Save and close the editor.
- Make necessary changes and continue the rebase.
Cherry-picking in Git allows you to apply the changes introduced by some existing commits to another branch.
- Cherry-pick a commit:
git cherry-pick abc1234
- Identify the commit you want to cherry-pick.
- Execute the cherry-pick command with the commit hash.
- Resolve any conflicts and commit the changes.
Submodules allow you to include and manage a repository within another repository.
-
Add a submodule:
git submodule add https://github.com/example/repo.git
-
Update submodules:
git submodule update --init --recursive
- Add a submodule to your repository.
- Commit the submodule addition.
- Clone the repository with submodules.
gitGraph
commit id: "Initial Commit"
branch develop
commit id: "Added README"
branch feature-1
checkout feature-1
commit id: "Feature 1 Commit 1"
commit id: "Feature 1 Commit 2"
checkout develop
merge feature-1 id: "Merge feature-1 into develop"
commit id: "Develop Commit 1"
branch feature-2
checkout feature-2
commit id: "Feature 2 Commit 1"
commit id: "Feature 2 Commit 2"
checkout develop
merge feature-2 id: "Merge feature-2 into develop"
commit id: "Develop Commit 2"
checkout main
merge develop id: "Merge develop into main"
commit id: "Release v1.0.0"
- Include diagrams to illustrate Git workflows.
- Use flowcharts to show the rebase or cherry-picking process.
- Provide screenshots of Git commands in action.
- Show examples of conflict resolution and submodule updates.
Imagine a development team working on a complex software project preparing for a major release. They used interactive rebase extensively to clean up their commit history. This process allowed them to:
- Squash Commits: Combine related changes into cohesive commits.
- Reorder Commits: Arrange commits logically for better readability.
- Edit Commit Messages: Ensure clear and informative commit messages.
By using interactive rebase, the team streamlined their commit history, making it easier to review and understand the changes made over the development cycle.
In another scenario, a critical bug was discovered in a production environment requiring immediate attention. The team needed to apply the fix to multiple branches, including both the current release and a previous stable version. Cherry-picking enabled them to:
- Select Specific Commits: Identify and apply the exact fix without including unrelated changes.
- Maintain Branch Isolation: Ensure the fix was applied cleanly to each targeted branch.
This approach allowed the team to deploy the hotfix swiftly across different environments while maintaining code stability.
The feature branch workflow with rebase is a common practice in agile development. Hereβs how it works:
-
Create a Feature Branch: Start new development on a dedicated branch.
git checkout -b feature/my-feature
-
Commit Changes: Make iterative commits on the feature branch.
git add . git commit -m "Implement feature XYZ"
-
Rebase with Main Branch: Keep the feature branch up to date with the main branch.
git fetch origin git rebase origin/main
-
Resolve Conflicts: Address any conflicts that arise during the rebase process.
-
Merge into Main Branch: Once development is complete and reviewed, merge the feature branch into the main branch.
git checkout main git merge --no-ff feature/my-feature
Submodules are invaluable for managing project dependencies, especially when integrating external libraries or repositories. Hereβs how you can use submodules effectively:
-
Add Submodule: Integrate an external repository into your project.
git submodule add <repository_URL> <path>
-
Update Submodule: Keep submodule content up to date.
git submodule update --remote
-
Clone with Submodules: Clone a repository and initialize submodules.
git clone --recurse-submodules <repository_URL>
-
Collaborate with Submodules: Collaborate seamlessly with teams using submodules, ensuring consistent versions and dependencies across environments.
These workflows and examples illustrate practical applications of Git features in real-world scenarios, enhancing collaboration and code management in software development projects.
This section adds detailed case studies and workflows with emojis and symbols to enhance readability and engagement in your README.
## Troubleshooting π οΈ
### Common Errors
- **Merge conflicts during rebase**:
```sh
git rebase --abort
git rebase --continue
- Submodule cloning issues:
git submodule update --init --recursive
-
How to resolve rebase conflicts? Resolve conflicts in the files and use
git rebase --continue
. -
How to remove a submodule? Remove the relevant section from the
.gitmodules
file and delete the submodule directory.
- Use SSH keys for authentication.
- Enable two-factor authentication (2FA) on your Git service.
- Review and limit repository access.
- Regularly update dependencies to patch vulnerabilities.
- Sign commits with GPG to verify authorship:
git commit -S -m "Signed commit"
Understanding advanced Git topics like interactive rebase, cherry-picking, and submodules can greatly enhance your version control workflows. Remember to use visual aids, refer to real-world examples, troubleshoot common issues, and follow best security practices to ensure a smooth and secure Git experience.