Git is often introduced as a list of commands, which is why the first conflict or accidental reset feels like a crisis. It makes more sense once you see the three places a change can live—your working tree, the staging area, and a commit—and remember that GitHub is a separate service built around Git repositories.
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.
Terminal window git initThis 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.
Terminal window git clone <repository_url> -
git add Stages files to be committed in the next commit.
Terminal window git add <filename>To stage changes beneath the current directory:
Terminal window git add . -
git commit Commits staged changes to the local repository with a descriptive message.
Terminal window git commit -m "your commit message" -
git status Displays the status of the working directory and staging area (tracked, untracked, modified files).
Terminal window git status -
git push Pushes committed changes from the local repository to the remote repository.
Terminal window git push origin <branch> -
git pull Fetches changes from a remote and integrates them into the current branch. Depending on your configuration and options, Git may merge, rebase, or perform a fast-forward update.
Terminal window git pull -
git branch Lists, creates, or deletes branches.
Terminal window git branchTo create a new branch:
Terminal window git branch <branch_name> -
git switch Switches to a different branch.
git checkoutcan also do this, butswitchmakes the intent clearer in modern Git.Terminal window git switch <branch_name> -
git merge Merges the specified branch into the current branch.
Terminal window git merge <branch_name> -
git log Displays the commit history.
Terminal window git log -
git remote Manages connections to remote repositories.
Terminal window git remote -v -
git diff Shows the changes between commits, branches, or the working directory.
Terminal window git diff -
git restore Removes a file’s staged changes while leaving the working copy intact. Older tutorials often use
git reset <filename>for this job.Terminal window git restore --staged <filename> -
git rm Removes files from the working directory and the index.
Terminal window git rm <filename> -
git mv Renames or moves files.
Terminal window git mv <old_filename> <new_filename> -
git stash Temporarily saves tracked changes that are not ready to commit. Untracked files are excluded unless
-uor--include-untrackedis used.Terminal window git stash -
git tag Creates a tag for marking specific points in the repository’s history (e.g., version releases).
Terminal window git tag <tag_name> -
git config Configures Git settings, such as the user name and email.
Terminal window git config --global user.name "Your Name"git config --global user.email "youremail@example.com" -
git show Displays information about a specific commit.
Terminal window git show <commit_id>
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. This rewrites the reapplied commit IDs, so rebasing work that others already use requires coordination.
Terminal window git rebase <branch_name> -
git cherry-pick Applies a specific commit from one branch to another.
Terminal window git cherry-pick <commit_hash> -
git bisect Finds the commit that introduced a bug by performing a binary search between commits.
Terminal window git bisect startgit bisect badgit bisect good <commit_hash># Repeat good/bad after each test, then leave the bisect session:git bisect reset -
git submodule Manages repositories inside other repositories as submodules.
Terminal window git submodule add <repository_url> -
git reflog Shows recent updates to local references such as
HEADand branch tips. It can help recover a commit after an accidental reset or rebase, provided the relevant reflog entry has not expired.Terminal window git reflog -
git filter-repo Rewrites repository history, for example to remove a file from every commit. Install
git-filter-reposeparately and make a backup or fresh clone first. Rewriting published history changes commit IDs and requires coordination with collaborators.Terminal window git filter-repo --path <filename> --invert-pathsGit’s documentation warns against using
git filter-branchfor most history rewrites because it is easy to produce confusing or incomplete results. -
git blame Shows who made the last modification to each line in a file.
Terminal window git blame <filename> -
git archive Creates an archive of the files in a particular commit or branch.
Terminal window git archive --format=zip HEAD > latest.zip -
git gc Cleans up unnecessary files and optimizes the local repository.
Terminal window git gc -
git clean Removes untracked files from the working directory.
Terminal window git clean -nd # preview firstgit clean -fd # remove the listed untracked files and directoriesgit cleanis destructive. Files that were never committed are not recoverable through Git. -
git revert Reverses the effects of a previous commit without modifying the history.
Terminal window git revert <commit_hash> -
git reset —hard Resets the current branch, index, and tracked working-tree files to a commit. It discards uncommitted changes to tracked files, but it does not normally remove untracked files.
Terminal window git reset --hard <commit_hash>Check
git statusand create a commit or stash before running it. If the branch has been shared, moving it backward can also disrupt collaborators. -
git ls-tree Lists the contents of a tree object, including file names, paths, and modes.
Terminal window git ls-tree <branch_name> -
git fsck Verifies the integrity of the repository.
Terminal window git fsck -
git describe Provides a human-readable name for a commit based on tags and the number of commits since the last tag.
Terminal window git describe -
git pull —rebase Rebase instead of merging changes from the remote repository when pulling updates.
Terminal window git pull --rebase origin <branch> -
git diff —cached Shows differences between the staging area and the last commit.
Terminal window git diff --cached -
git remote prune Removes references to branches that no longer exist on the remote.
Terminal window git remote prune origin -
git fetch —all Fetches updates from all remotes.
Terminal window git fetch --all -
git cherry Shows which commits have not yet been applied to a branch.
Terminal window git cherry -v
Conclusion
Git becomes much less mysterious once every command is tied back to the working tree, staging area, commits, branches, and remotes. You do not need to memorize this whole list. Learn the commands you use every day, read git status before changing history, and check the documentation before running an unfamiliar destructive option. GitHub adds a useful collaboration layer, but the repository and its history still belong to Git.