Git and GitHub
Git and GitHub are essential tools in modern software development, enabling version control and collaboration among developers.
Git
Version Control System:
Git is a distributed version control system (DVCS) that helps track changes in source code during software development.
Concepts of Git:
- Repository (Repo): A storage location for a project's files and revision history.
- Clone: Copying a repository to create a local copy on your machine.
- Commit: A snapshot of changes made to the code.
- Branch: A separate line of development that allows changes to be made without affecting the main codebase.
- Merge: Combining changes from one branch into another.
- Pull: Fetching changes from a remote repository to update the local copy.
- Push: Uploading local changes to a remote repository.
Git Commands:
SETUP
Configuring user information used across all local repositories
- Define the author name to be used for all commits by the current user.
git config --global user.name <Username>
- Define the author email to be used for all commits by the current user.
git config --global user.email <User_email>
SETUP & INIT
Configuring user information, initializing and cloning repositories
- initialize an existing directory as a Git repository
git init
- retrieve an entire repository from a hosted location via URL
git clone <Url>
STAGE & SNAPSHOT
Working with snapshots and the Git staging area
- List which files are staged, un staged, and untracked.
git status
- Add a file as it looks now to your next commit (stage)
git add <file>
- Remove <file> from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes
git reset <file>
- Show un staged changes between your index and working directory.
git diff
- diff of what is staged but not yet committed
git diff --staged
- Commit the staged snapshot, but instead of launching a text editor, use <descriptive_message> as the commit message
git commit -m <descriptive_message>
BRANCH & MERGE
Isolating work in branches, changing context, and integrating changes
- list your branches. a * will appear next to the currently active branch
git branch
- create a new branch at the current commit
git branch <branch_name>
- switch to another branch and check it out into your working directory
git checkout
- merge the specified branch’s history into the current one
git merge <branch>
- Display the entire commit history using the default format. For customization see additional options.
git log
REMOTE REPOSITORIES
Retrieving updates from another repository and updating local repos
- Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.
git remote add <name> <url>
- fetch down all the branches from that Git remote
git fetch <name>
- merge a remote branch into your current branch to bring it up to date
git merge <name>/<branch>
- Transmit local branch commits to the remote repository branch
git push <name> <branch>
- fetch and merge any commits from the tracking remote branch
git pull
GitHub
GitHub is a web-based platform that provides hosting for Git repositories and additional Collaboration features.
Collaboration in GitHub:
- Pull Requests (PRs):
- Proposed changes to a repository submitted by a developer for review before being merged.
- Issues:
- Track tasks, enhancements, bugs, and other kinds of questions.
- Forks:
- Personal copies of another user's repository for making changes without affecting the original project.
Workflow:
- Forking a Repository:
- Create a personal copy of someone else's project.
- Cloning:
git clone <repository_url>
- Branching, Committing, and Pushing:
- Similar to the Git workflow on your local machine.
- Pull Request:
- Submit changes for review and merge.
- Merging Pull Request:
- Once changes are approved, they can be merged into the main repository.
GitHub provides a user-friendly interface for Git operations and enhances collaboration in a team setting. Many open-source projects and organizations use GitHub as a platform for hosting and managing their repositories.
Differences between Git and GitHub
| Feature | Git | GitHub |
| Definition | Distributed Version Control System (DVCS) | Web-based platform for hosting Git repositories |
| Type | Software installed locally | Web-based service |
| Functionality | Manages version control locally | Adds collaboration features, remote hosting |
| Repository Hosting | Local machine | GitHub servers |
| Collaboration | Limited (local team) | Extensive (global collaboration) |
| Remote Repositories | Can be hosted on any server | Hosted on GitHub servers |
| Graphical Interface | Command-line | Web-based with a graphical interface |
| Pull Requests | Not applicable | Facilitates code review and collaboration |
| Issues and Tracking | Not applicable | Integrated issue tracking system |
| Forking | Not applicable | Allows users to create personal copies (forks) |
| User Management | Not applicable | User permissions, teams, and organization |
| Visibility | Limited to local machine | Public and private repositories |
| Use Cases | Local version control and development | Collaborative software development, open source |