Version control, also known as source control, is the practice of recording and managing changes to software code. It’s a crucial aspect of software development that allows teams to collaborate, track revisions, and revert to previous versions when needed. While there are many version control systems available—like Apache Subversion, Concurrent Versions System (CVS), and BitKeeper—Git has become the preferred choice for most developers. As a free and open-source distributed version control system, Git is designed for speed, efficiency, and handling projects of all sizes.
This cheat sheet covers the most commonly used Git commands to help you get started and master version control in your development workflow.
Installation
To install Git, use the command for your specific operating system:
- On Fedora/RedHat systems:
dnf install git
- For other operating systems, you can download Git from git-scm.com.
Setup
Before using Git, configure your user information, which will be used across all local repositories.
| Command | Description |
|-----------------------------------------------------|---------------------------------------------------------------|
| git config --global user.name "[firstname lastname]"
| Set your name to be used in the version history |
| git config --global user.email "[valid-email]"
| Set your email for your commits |
| git config --global color.ui auto
| Enable colorized output for easier command line review |
Initialization
Set up and initialize new repositories or clone existing ones.
| Command | Description |
|---------------------------------|---------------------------------------------------------------|
| git init
| Create a new Git repository in an existing directory |
| git clone [url]
| Clone an existing repository from a remote URL |
Stage & Snapshot
Use snapshots and the staging area to manage changes.
| Command | Description |
|------------------------------------|---------------------------------------------------------------------|
| git status
| Show the status of changes in your working directory |
| git add [file]
| Add the specified file to the staging area (stage) |
| git reset [file]
| Unstage a file, keeping the changes in your working directory |
| git diff
| Show changes in the working directory that haven't been staged |
| git diff --staged
| Show changes between the staging area and the last commit |
| git commit -m "[message]"
| Commit the staged changes with a descriptive message |
Branch & Merge
Manage branches and integrate changes effectively.
| Command | Description |
|------------------------------------|---------------------------------------------------------------------------|
| git branch
| List all branches in the repository; current branch is marked with *
|
| git branch [branch-name]
| Create a new branch |
| git checkout [branch-name]
| Switch to the specified branch |
| git merge [branch-name]
| Merge the specified branch into the current branch |
| git log
| View the commit history for the current branch |
Inspect & Compare
Analyze logs, diffs, and object data to better understand your code changes.
| Command | Description |
|---------------------------------------|---------------------------------------------------------------------|
| git log
| Show the commit history for the current branch |
| git log branchY...branchX
| List commits that are in branchX
but not in branchY
|
| git log --follow [file]
| Show changes to a file across different commits |
| git diff branchY...branchX
| Show the difference between two branches |
| git show [SHA]
| Display the content of a specific commit in a readable format |
Share & Update
Keep your local repository in sync with a remote repository.
| Command | Description |
|------------------------------------------|------------------------------------------------------------------------|
| git remote add [alias] [url]
| Add a remote repository under a specified alias |
| git fetch [alias]
| Download all branches from the remote repository |
| git merge [alias]/[branch]
| Merge a remote branch into your current branch |
| git push [alias] [branch]
| Push commits from the local branch to the remote branch |
| git pull
| Fetch and merge changes from the remote repository into the current branch |
Tracking Path Changes
Manage file removals and relocations.
| Command | Description |
|---------------------------------------------|--------------------------------------------------------------------|
| git rm [file]
| Remove a file from the working directory and stage the removal |
| git mv [existing-path] [new-path]
| Rename or move a file |
| git log --stat -M
| Show commit logs with details on files that have been moved |
Ignoring Patterns
Avoid staging or committing files unintentionally.
| Pattern Examples | Description |
|--------------------------------------------|-------------------------------------------------------------------------------|
| logs/
, *.notes
, pattern*/
| Patterns to add to .gitignore
to avoid tracking files |
| git config --global core.excludesfile [file]
| Set a global ignore file for patterns across all repositories on your system |
Rewrite History
Modify commits and clean up your repository's history.
| Command | Description |
|----------------------------------------------|----------------------------------------------------------------------|
| git rebase [branch]
| Reapply commits on top of another base commit |
| git reset --hard [commit]
| Reset the current branch to the specified commit, discarding changes |
Temporary Commits
Store your changes temporarily to switch branches without committing.
| Command | Description |
|------------------------------------|--------------------------------------------------------------------|
| git stash
| Save your changes and clean the working directory |
| git stash pop
| Reapply the most recently stashed changes |
| git stash list
| List all stashed changes |
Mastering these Git commands can help you effectively manage your codebase, collaborate with team members, and keep your projects organized. Whether you’re working on a solo project or a massive open-source initiative, understanding Git’s capabilities will empower you to develop software more efficiently. Happy coding!
REFERENCES
- https://git-scm.com
- https://git.kernel.org/pub/scm/git/git.git
- https://about.gitlab.com/images/press/git-cheat-sheet.pdf (Motivation to Write this Article)
- https://gitlab.com/
- https://github.com/