Version control is essential in modern software development, enabling teams to collaborate efficiently, track changes, and manage projects. Two tools that have become synonymous with version control are Git and GitHub. In this blog, we’ll dive into what Git and GitHub are, explore their differences, and provide a green sheet with essential Git commands to help you get started.
What is Git?
Git is a distributed version control system (DVCS) designed to track changes in the source code during software development. Created by Linus Torvalds in 2005, Git allows multiple developers to collaborate on projects, make changes, and merge them without overwriting each other’s work. Git operates on your local machine, giving you full control over your version history.
Key Features of Git:
- Branching: Git allows you to create separate branches to develop new features or fix bugs without affecting the main codebase.
- Distributed Versioning: Every copy of the repository is a full-fledged version-controlled project, enabling offline work.
- Commit History: Git maintains a history of all changes made, allowing you to go back and inspect or restore previous versions.
- Efficient Merging: With Git’s branching system, merging changes is efficient and streamlined.
What is GitHub?
GitHub is a web-based platform that hosts Git repositories and adds collaborative features like issue tracking, pull requests, code reviews, and project management tools. While Git operates locally, GitHub enables remote repositories, allowing teams to share code over the internet.
Key Features of GitHub:
- Collaboration: GitHub facilitates collaboration by letting developers clone repositories, work on branches, and submit changes through pull requests.
- Code Hosting: GitHub stores your Git repositories in the cloud, making them accessible to other developers.
- Version Control in the Cloud: GitHub allows you to host, back up, and collaborate on projects online.
- Community Engagement: GitHub has a social aspect where developers can follow projects, contribute to open-source, and network with other developers.
Git vs GitHub: What’s the Difference?
- Git is the version control system used to manage source code history, while GitHub is a platform for hosting and collaborating on Git repositories.
- Git is used locally; GitHub adds online collaboration features such as pull requests, issue tracking, and reviews.
- You can use Git without GitHub, but GitHub requires Git (or another VCS like Mercurial) to manage repositories.
Git Command Green Sheet
Here’s a quick reference sheet with essential Git commands to help you navigate the Git environment:
Git Basics
Command | Description |
---|---|
git init |
Initializes a new Git repository in your current directory. |
git clone <repo_url> |
Clones an existing repository from a URL. |
git status |
Shows the status of files in the working directory. |
git add <file> |
Stages changes in the specified file for the next commit. |
git add . |
Stages all changes (new, modified, deleted files). |
git commit -m "message" |
Commits staged changes with a message. |
git log |
Displays the commit history. |
Branching and Merging
Command | Description |
---|---|
git branch |
Lists all local branches. |
git branch <branch_name> |
Creates a new branch. |
git checkout <branch_name> |
Switches to the specified branch. |
git checkout -b <branch> |
Creates and switches to a new branch. |
git merge <branch> |
Merges changes from the specified branch into the current one. |
git branch -d <branch> |
Deletes the specified branch locally. |
Remote Repositories (GitHub)
Command | Description |
---|---|
git remote add origin <repo_url> |
Adds a remote repository (commonly named origin ) for pushing and pulling. |
git push origin <branch> |
Pushes local branch commits to the remote repository. |
git pull origin <branch> |
Fetches and merges changes from the remote branch to your local branch. |
git fetch origin |
Fetches all changes from the remote repository without merging them. |
git remote -v |
Lists remote repositories linked to the local repository. |
Stashing and Undoing Changes
Command | Description |
---|---|
git stash |
Temporarily saves changes without committing them. |
git stash pop |
Applies the most recent stashed changes and removes the stash. |
git stash list |
Shows a list of all stashed changes. |
git reset <file> |
Unstages the specified file (keeps changes in the working directory). |
git reset --hard <commit> |
Resets to a specific commit, discarding all changes. |
git revert <commit> |
Reverts a specific commit by creating a new commit with the changes undone. |
Git Workflows
1. Cloning a Repository:
git clone <repo_url>
cd <repository>
This command clones an existing repository to your local machine and navigates into the repository folder.
2. Committing Changes:
git add <file>
git commit -m "descriptive message"
Stage your changes using git add, then commit them with a message that describes what you’ve done.
3. Pushing Changes to GitHub:
git push origin <branch>
Once your changes are committed locally, push them to the remote repository on GitHub.
4. Creating a New Branch:
git checkout -b <new_branch>
Create and switch to a new branch to isolate your work. This is useful for feature development or bug fixes.
5. Merging a Branch:
git checkout <target_branch>
git merge <branch_to_merge>
Switch to the target branch (often main or master) and merge the changes from your feature branch.
Conclusion
Git and GitHub are indispensable tools for modern software development, streamlining version control, collaboration, and project management. While Git provides a robust, distributed system for tracking code changes, GitHub extends those capabilities into the cloud, making it easier to work with teams or contribute to open-source projects. Armed with this green sheet of Git commands, you’re well on your way to mastering version control.
Background image by Mohammad Rahmani on Unsplash.
Únase a nuestra lista de correo electrónico y reciba notificaciones sobre nuevos contenidos.
Sé el primero en recibir nuestro contenido. Prometemos no enviar spam a su bandeja de entrada ni compartir su correo electrónico con terceros.