![]() |
Git and GitHub are important tools for modern software development, enabling version control, collaboration, and efficient code management. This guide provides an overview of how to use Git and GitHub, from setting up your environment to contributing to projects. Table of Content Prerequisites
git config --global user.name "Your Name" What is Git?Git is a distributed version control system that helps developers track changes in their code, manage project history, and collaborate with others. It allows you to maintain multiple versions of your code, branch off to experiment, and merge changes seamlessly. What is GitHub?GitHub is a web-based platform that uses Git for version control. It provides a collaborative environment for developers to host, review, and manage their projects. GitHub offers features like pull requests, issues, and GitHub Actions to automate workflows. Getting Started with GitStep 1. Install GitDownload and install Git from Git’s official website. Follow the instructions for your operating system. Step 2. Configure GitSet up your name and email to identify your commits: git config --global user.name "Your Name" Step 3. Create a New RepositoryTo start a new project, create a new directory and initialize it as a Git repository: mkdir my-project Step 4. Clone an Existing RepositoryTo contribute to an existing project, clone a repository from GitHub: git clone https://github.com/username/repository.git Step 5. Track ChangesAdd files to your repository and track changes: touch file.txt Step 6. View HistoryTo view the history of your commits: git log
Working with GitHubStep 1. Create a GitHub Repository
Step 2. Connect Local Repository to GitHubLink your local Git repository to the GitHub repository: git remote add origin https://github.com/username/repository.git
Step 3. Push Changes to GitHubTo push your local commits to GitHub: git push -u origin main
Replace main with the name of your main branch if different. Step 4. Pull Changes from GitHubTo update your local repository with changes from GitHub: git pull origin main
Step 5. Create a BranchBranching allows you to work on new features without affecting the main code: git checkout -b new-branch
Step 6. Push a Branch to GitHubPush your new branch to GitHub: git push -u origin new-branch
Step 7. Create a Pull RequestA pull request (PR) is a way to propose changes to a repository. To create a PR:
Step 8. Review and Merge a Pull RequestCollaborators can review PRs, suggest changes, and approve them. Once reviewed, you can merge the PR:
Step 9. Handle Merge ConflictsMerge conflicts occur when changes in different branches clash. To resolve conflicts:
git pull origin main
git add . Step 10. Use IssuesGitHub Issues help track bugs, enhancements, or tasks. To create an issue:
Step 11. Use GitHub ActionsGitHub Actions allows you to automate workflows. To set up an action:
Advanced Git and GitHub Tips1. Rebase for a Cleaner HistoryRebasing integrates changes from one branch into another and provides a linear commit history: git checkout main 2. Squash CommitsSquashing combines multiple commits into one, simplifying your commit history: git rebase -i HEAD~n
Replace n with the number of commits to squash. 3. Use .gitignoreCreate a .gitignore file to exclude files from being tracked by Git. Add file patterns to this file: *.log 4. Stash ChangesIf you need to switch branches but have uncommitted changes, use git stash to save them temporarily: git stash
Retrieve stashed changes later with: git stash pop
Best Practices
|
Reffered: https://www.geeksforgeeks.org
Git |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |