Table of Contents
8.1 Introduction to Version Control
Version Control is a system that helps manage changes to source code over time. It tracks modifications, enables collaboration among multiple developers, and ensures the ability to revert to previous versions if necessary.
Key Concepts:
- Versioning: Keeps track of different versions of files or codebase.
- Commit History: Logs file changes and includes metadata such as author, date, and comments.
- Branching: Allows multiple lines of development to proceed concurrently.
- Merging: Combines changes from different branches into a single branch.
- Reversion: Ability to roll back to previous versions of files or code.
Version control is essential for managing the complexities of software development, especially in collaborative environments.
8.2 Setting Up Git and GitHub
Git is a distributed version control system, and GitHub is a platform for hosting Git repositories, enabling collaborative development.
Setting Up Git:
- Download and Install Git: Visit the official Git website and download the installer for your operating system.
- Configure Git:
git config –global username “Your Name”
git config –global user.email “your.email@example.com”
Setting Up GitHub:
- Create a GitHub Account: Sign up at GitHub.
- Create a New Repository:
- Go to your GitHub dashboard.
- Click “New” to create a new repository.
- Fill in the repository name and description, and choose visibility (public or private).
- Link-Local Repository to GitHub:
git remote add origin https://github.com/username/repository.git
8.3 Basic Git Commands and Operations
Git Commands allow you to perform version control operations efficiently.
Common Commands:
- Clone a Repository: Create a local copy of a remote repository.
git clone https://github.com/username/repository.git - Check Status: View changes in the working directory.
git status - Add Changes: Stage changes for commit.
git add filename - Commit Changes: Save changes to the local repository.
git commit -m “Commit message” - Push Changes: Upload changes to the remote repository.
git push origin branch-name - Pull Changes: Download changes from the remote repository.
git pull origin branch-name - View Commit History: Show a log of commits.
git log - Revert Changes: Undo changes.
git revert commit-hash
8.4 Branching and Merging in Git
Branching and Merging allow for parallel development and integration of features or fixes.
Creating a Branch:
git branch branch-name
Switching Branches:
git checkout branch-name
Merging Branches:
- Switch to Target Branch:
git checkout main
- Merge Branch:
git merge branch-name
- Resolve Conflicts (if any): Manually edit the conflicting files, then stage and commit the resolved changes.
Deleting a Branch:
git branch -d branch-name
8.5 Collaborating in Teams Using GitHub
GitHub facilitates collaboration through features like pull requests, issues, and project boards.
Key Collaboration Features:
- Pull Requests:
- Create a Pull Request:
- Go to the GitHub repository.
- Click on “Pull Requests” and then “New Pull Request”.
- Compare the branches and submit the pull request with a description.
- Review and Merge:
- Team members review the pull request, comment, and suggest changes.
- Once approved, the pull request can be merged into the main branch.
- Create a Pull Request:
- Issues: Track bugs, enhancements, and tasks.
- Create issues to document and assign tasks or bugs.
- Use labels and milestones to organize and prioritize work.
- Project Boards: Manage tasks and workflows visually.
- Create boards to organize tasks into columns like “To Do”, “In Progress”, and “Done”.
8.6 Integrating Git with Jenkins for CI/CD
Jenkins is an open-source automation server that integrates with Git for continuous integration and delivery (CI/CD).
Integrating Git with Jenkins:
- Install Jenkins: Download and install Jenkins from Jenkins website.
- Configure Git Plugin:
- Go to “Manage Jenkins” > “Manage Plugins” > “Available”.
- Search for and install “Git Plugin”.
- Create a Jenkins Job:
- Click “New Item” on the Jenkins dashboard.
- Select “Freestyle project” or other project types and configure job settings.
- Set Up Source Code Management:
- In the job configuration, select “Git” under “Source Code Management”.
- Enter the repository URL and credentials.
- Add Build Steps:
- Add build steps to compile code, run tests, or deploy applications.
- Configure build triggers, such as “Poll SCM” or “Build periodically”.
- Configure Post-Build Actions:
- Add post-build actions like archiving artifacts or publishing test results.
- Save and Build:
- Save the job configuration.
- Click “Build Now” to start the build process.
Example Pipeline Configuration:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/username/repository.git'
}
}
stage('Build') {
steps {
sh './build.sh'
}
}
stage('Test') {
steps {
sh './run_tests.sh'
}
}
stage('Deploy') {
steps {
sh './deploy.sh'
}
}
}
post {
always {
archiveArtifacts '**/target/*.jar'
junit '**/target/test-classes/*.xml'
}
}
}
Conclusion
Version control and collaboration tools are fundamental for modern software development. Mastering Git and GitHub enhances your ability to manage code changes, collaborate with team members, and maintain a stable development workflow. Integrating these tools with Jenkins for CI/CD further streamlines the development process, ensuring efficient and reliable software delivery.