GTI and GitHub Documentation: This documentation provides comprehensive guidelines for using GTI (Git Tool Interface) and GitHub, covering key functionalities, setup processes, and best practices.
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.
git init
git status
git add <file>
git commit -m "commit message"
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:
brew install git or download from Git for Mac.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:
Change Default Code Editor:
git config --global core.editor "code --wait"
git init
git add <file>
git add .
git commit -m "commit message"
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.
git remote add origin <repository_URL>
git branch -M main
git push -u origin main
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.
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.
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_NAMEbranch.
git switch BRANCH_NAME
git switch -c another_branch- This command creates a new branch. the-cflag is used to create a new branch.
git switch -c another_branch
git checkout BRANCH_NAME- This command switches to theBRANCH_NAMEbranch.
git checkout BRANCH_NAME
git checkout -b BRANCH_NAME- This command create and switches to theBRANCH_NAMEbranch.
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.
git branch
The current branch will have an asterisk (*) next to it.
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:
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.
main or master):git checkout main
git pull origin main
git merge feature-branch
Conflicts occur when changes in different branches overlap. Here’s how to manage them:
Identify the conflict:
<<<<<<< HEAD).Resolve the conflict:
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:
Complete Rebase:
Cherry-picking allows you to select specific commits from one branch and apply them to another branch.
Identify Commit:
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
Cherry-picking in Git allows you to apply the changes introduced by some existing commits to another branch.
git cherry-pick abc1234
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
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"
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:
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:
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
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.
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.