What is Git?
Git is a distributed version control system (DVCS) that helps developers track changes in their codebase over time. Created by Linus Torvalds in 2005, Git enables collaboration on software projects by allowing multiple contributors to work on the same code, track revisions, and merge changes efficiently. Git’s key strength lies in its ability to manage large, branching code repositories while preserving the integrity and history of the code.
What is GitHub?
GitHub is a cloud-based platform that hosts Git repositories. While Git is the version control tool, GitHub adds a social, collaborative layer by providing a web-based interface for storing and managing Git repositories. GitHub offers features like pull requests, issue tracking, project management, and code reviews, making it a go-to tool for open-source projects and professional development teams.
Key Differences Between Git and GitHub:
- Tool vs. Platform: Git is the actual version control tool, while GitHub is a web-based platform that leverages Git.
- Local vs. Cloud: Git operates locally on your machine, enabling version control and branching, whereas GitHub allows for hosting repositories remotely and facilitates team collaboration.
- Functionality: GitHub extends Git’s functionality with additional features like a visual interface, project management tools, and cloud-based collaboration.
- Hosting: GitHub stores code remotely, allowing contributors to work from different locations, whereas Git manages code repositories locally unless configured to push to a remote server.
Now, let’s explore how to use Git with a focus on both basic and advanced commands.
Basic Git Commands
Below are essential Git commands that every developer should know. These commands help in managing repositories, committing changes, and collaborating with teams.
-
git init Initializes a new Git repository in your project directory.
This command creates a .git directory to start version control.
-
git clone Clones a repository from a remote location (e.g., GitHub) to your local machine.
-
git add Stages files to be committed in the next commit.
To add all files in the directory:
-
git commit Commits staged changes to the local repository with a descriptive message.
-
git status Displays the status of the working directory and staging area (tracked, untracked, modified files).
-
git push Pushes committed changes from the local repository to the remote repository.
-
git pull Fetches and merges changes from the remote repository to your local branch.
-
git branch Lists, creates, or deletes branches.
To create a new branch:
-
git checkout Switches to a different branch.
-
git merge Merges the specified branch into the current branch.
-
git log Displays the commit history.
-
git remote Manages connections to remote repositories.
-
git diff Shows the changes between commits, branches, or the working directory.
-
git reset Unstages files from the index (staging area).
-
git rm Removes files from the working directory and the index.
-
git mv Renames or moves files.
-
git stash Temporarily saves changes that are not ready to commit.
-
git tag Creates a tag for marking specific points in the repository’s history (e.g., version releases).
-
git config Configures Git settings, such as the user name and email.
-
git show Displays information about a specific commit.
Advanced Git Commands
These commands help you dive deeper into more complex scenarios like rebasing, patching, and working with specific histories or submodules.
-
git rebase Reapplies commits on top of another base tip to maintain a cleaner commit history.
-
git cherry-pick Applies a specific commit from one branch to another.
-
git bisect Finds the commit that introduced a bug by performing a binary search between commits.
-
git submodule Manages repositories inside other repositories as submodules.
-
git reflog Shows a log of changes to the local repository, including commits that are no longer visible in the commit history.
-
git filter-branch Rewrites branches by applying filters on the commit history (e.g., removing sensitive data).
-
git blame Shows who made the last modification to each line in a file.
-
git archive Creates an archive of the files in a particular commit or branch.
-
git gc Cleans up unnecessary files and optimizes the local repository.
-
git clean Removes untracked files from the working directory.
-
git revert Reverses the effects of a previous commit without modifying the history.
-
git reset —hard Resets the index and working directory to match a specific commit, discarding all changes.
-
git ls-tree Lists the contents of a tree object, including file names, paths, and modes.
-
git fsck Verifies the integrity of the repository.
-
git describe Provides a human-readable name for a commit based on tags and the number of commits since the last tag.
-
git pull —rebase Rebase instead of merging changes from the remote repository when pulling updates.
-
git diff —cached Shows differences between the staging area and the last commit.
-
git remote prune Removes references to branches that no longer exist on the remote.
-
git fetch —all Fetches updates from all remotes.
-
git cherry Shows which commits have not yet been applied to a branch.
Conclusion
Git is a powerful version control system that, when combined with GitHub, makes collaborative development smoother. Understanding the fundamental and advanced commands will help you manage codebases effectively, allowing for seamless collaboration and code integrity. Whether you’re working on a solo project or part of a larger team, mastering Git commands is essential to becoming a proficient developer.