Git is an indispensable tool for developers, enabling you to track changes, collaborate with others, and manage your codebase efficiently. This cheat sheet covers essential Git commands that every developer should know, along with some advanced commands for more complex tasks.
1. Setting Up Your Git Repository
Initialize a New Repository
git init
This command initializes a new Git repository in your project folder. Use it when starting a new project or to begin tracking an existing one.
Clone an Existing Repository
git clone <repository-url>
Clone an existing repository from a remote source like GitHub or Bitbucket. This is useful for contributing to a project or using an open-source library.
2. Configuring Git
Set Your Username
git config --global user.name "Your Name"
Sets your username for all Git repositories on your system.
Set Your Email
git config --global user.email "your.email@example.com"
Sets your email address for commits.
View Your Configuration
git config --list
Lists all the Git configurations currently set.
3. Working with Your Repository
Checking the Status of Your Repository
git status
Displays the state of your working directory and staging area, showing which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
Viewing a Log of Commits
git log
Shows a detailed history of commits, including author, date, and commit message.
Adding Changes to the Staging Area
git add <file-name>
Stages specific files for a commit. To stage all changes, use:
git add .
Committing Changes
git commit -m "Your commit message"
Commits the staged changes with a descriptive message. It’s good practice to write meaningful commit messages.
Commit All Changes Directly
git commit -am "Your commit message"
Stages and commits all tracked files in one step. This command does not include untracked files.
Viewing Changes
git diff
Shows the changes between your working directory and the staging area. Useful for reviewing what you've modified before committing.
Viewing a Specific File’s Changes
git diff <file-name>
Displays the changes made to a specific file.
Removing Files
git rm <file-name>
Removes a file from your working directory and stages the deletion.
Moving/Renaming Files
git mv <old-file-name> <new-file-name>
Moves or renames a file and stages the change.
4. Branching and Merging
Create a New Branch
git branch <branch-name>
Creates a new branch. Branches are used to develop features or fixes in isolation from the main codebase.
List All Branches
git branch
Lists all branches in your repository, highlighting the current branch.
Switch to a Branch
git checkout <branch-name>
Switches to the specified branch.
Create and Switch to a New Branch
git checkout -b <branch-name>
Creates a new branch and immediately switches to it.
Merge Branches
git merge <branch-name>
Merges changes from the specified branch into your current branch. This is typically used to integrate a feature branch into the main branch.
Delete a Branch
git branch -d <branch-name>
Deletes a branch that has been merged. Use `-D` to force-delete a branch that hasn't been merged.
Rebase Branches
git rebase <branch-name>
Rebases the current branch onto the specified branch. Rebasing can lead to a cleaner project history but should be used with caution, especially with shared branches.
5. Working with Remotes
Adding a Remote Repository
git remote add origin <repository-url>
Connects your local repository to a remote server, typically a repository on GitHub, GitLab, or Bitbucket.
Viewing Remote Repositories
git remote -v
Lists all remotes and their URLs.
Pushing Changes to Remote
git push origin <branch-name>
Pushes your commits to the remote repository. If it’s your first push, you might need to set the upstream branch:
git push --set-upstream origin <branch-name>
Pulling Changes from Remote
git pull
Fetches and merges changes from the remote repository into your current branch.
Fetching Changes from Remote
git fetch
Downloads objects and refs from another repository. Unlike `git pull`, `git fetch` does not merge changes into your local branch automatically.
Removing a Remote Repository
git remote remove <remote-name>
Removes a remote from your repository.
6. Undoing Changes
Unstage Changes
git reset <file-name>
Removes changes from the staging area but leaves them in your working directory.
Discard Unstaged Changes
git checkout -- <file-name>
Reverts unstaged changes in your working directory to the last commit.
Revert a Commit
git revert <commit-hash>
Creates a new commit that undoes changes from a previous commit without altering the project history.
Reset to a Previous Commit
git reset --hard <commit-hash>
Rolls back your project to a specific commit, discarding all changes made after that point. Warning: This is irreversible.
Stash Changes
git stash
Temporarily saves changes that you don’t want to commit yet, allowing you to work on something else.
Apply Stashed Changes
git stash apply
Reapplies the most recently stashed changes to your working directory.
Drop a Stash
git stash drop
Removes a specific stash from your list of saved changes.
7. Advanced Git Commands
Cherry-Pick a Commit
git cherry-pick <commit-hash>
Applies the changes from a specific commit to your current branch.
Squash Commits
git rebase -i HEAD~n
Combines multiple commits into one. Replace `n` with the number of commits you want to squash.
View Commit History as a Graph
git log --graph --oneline --all
Displays the commit history in a graph format, showing branches and merges.
Cleaning Up Untracked Files
git clean -f
Deletes untracked files from your working directory. Add `-d` to remove directories and `-n` to perform a dry run.
Conclusion
This cheat sheet covers a broad range of Git commands that will help you manage your projects efficiently. Whether you're committing changes, branching, merging, or working with remote repositories, mastering these commands will make you more effective as a developer. Keep practicing, and these commands will soon become second nature.
If you found this helpful, don’t forget to bookmark this post so you can come back to it later when you're wondering what command you should use. Also, please share it with your fellow developers so they can use it too!
Now if you really found it helpful, check out my weekly newsletter where I help hundreds of other developers level up in their careers and make more money! Also, give me a follow on X where I’m active daily!
This post is part of The Developer Bootcamp collection.
Happy coding!