Git is an essential tool for version control, used by developers worldwide to manage code and track changes across projects. Here’s a cheat sheet to help you get started with Git commands. Keep this handy reference to streamline your workflow!
1. Initialize a New Git Repository
To start using Git in a project, you’ll first need to initialize a new repository. This command sets up the Git repository in your project folder:
git init
2. Set Up Your Username and Email
Before you start making commits, configure Git with your username and email to identify changes made by you:
git config --global user.name "<your-name>" git config --global user.email "<your-email>"
3. Clone an Existing Repository
If you want to work on a project already hosted in a Git repository, you can clone it to your local machine:
git clone <repository-url>
4. Add Files to the Staging Area
To stage changes (prepare them for a commit), add files to the staging area. For a specific file, use:
git add <file>
Or, to add all files and changes:
git add .
5. Check for Unstaged Changes
To see the differences between your working directory and the last commit, use:
git diff
6. Commit Staged Changes
After staging changes, you’ll want to commit them. Include a meaningful message to describe the commit:
git commit -m "Message"
7. Reset the Staging Area
If you need to remove changes from the staging area and return to the last commit, use:
git reset
8. Check the Repository Status
To see the state of your working directory and staging area, including untracked files and pending changes, run:
git status
9. Remove a File from the Index and Working Directory
To delete a file from both the index and the working directory, use:
git rm <file>
10. View the Commit History
To see a list of past commits, use:
git log
11. View Commit Metadata
To see details about a specific commit, including changes made, run:
git show <commit-hash>
12. Manage Branches
Git allows you to work on separate branches to manage different versions or features of a project.
- List all branches:
git branch
- Create a new branch:
git branch <branch-name>
- Rename the current branch:
git branch -m <new-branch-name>
- Delete a branch:
git branch -d <branch-name>
- Switch to another branch:
git checkout <branch-name>
13. Merge Branches
To merge the changes from another branch into the current branch:
git merge <branch-name>
14. Work with Remote Repositories
Collaborating with others often involves connecting to remote repositories.
- Add a remote connection:
git remote add <name> <repository-url>
- Push changes to a remote repository:
git push <remote> <branch>
- Pull changes from a remote repository:
git pull <remote>
15. Clean Up with Git Garbage Collection
Remove unnecessary files and optimize your local repository:
git gc
16. Stash Changes
Temporarily set aside uncommitted changes without committing them. Use stash
to save your work temporarily and apply it later.
- Stash changes:
git stash
- Apply the most recent stash:
git stash apply
This cheat sheet provides a quick overview of essential Git commands to keep your work organized, efficient, and versioned. With these commands, you can manage and collaborate on projects with ease! Happy coding!