Git is an essential tool for developers, enabling smooth version control, collaboration, and code management across projects. This cheat sheet breaks down the basics of Git, from setup to advanced commands, so you can navigate Git’s functionality with ease.
1. Setting Up Git
To get started with Git, you first need to install it and configure some basic settings.
Installation
- On Linux:
sudo apt-get install git
- On Mac:
brew install git
- On Windows: Download from git-scm.com/downloads
Configuration
After installation, configure your identity for commits:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
You can also enable helpful settings like colorized output:
git config --global color.ui auto
2. Starting a Project with Git
Git can initialize a repository for new projects or clone an existing one.
-
Initialize a Repository:
git init [project_name]
This command creates a new Git repository in your project folder.
-
Clone a Repository:
git clone <repository_url>
Cloning downloads the entire project history from the remote repository.
3. Basic Git Commands
For day-to-day work, Git’s main commands help manage changes effectively.
-
Check Status:
git status
Displays the current state of your working directory.
-
Add Changes:
git add [file]
Add a file to the staging area. To add all files, use
git add .
. -
Commit Changes:
git commit -m "Commit message"
Creates a new commit from the changes in the staging area.
-
View Differences:
- To see unstaged changes:
git diff [file]
- For staged changes:
git diff --staged [file]
- To see unstaged changes:
4. Branching and Merging
Git’s branching model makes it easy to work on separate features or fixes without affecting the main project.
-
Create a Branch:
git branch [branch_name]
This command creates a new branch based on the current branch.
-
Switch Branches:
git checkout [branch_name]
To switch to an existing branch. Use
-b
to create and switch to a new branch simultaneously. -
Merge Branches:
git merge [branch_name]
Merges the specified branch into the current branch.
-
Delete a Branch:
git branch -d [branch_name]
Deletes a branch if it has already been merged into another.
5. Working with Remote Repositories
Remote repositories allow teams to collaborate. Here’s how to manage them:
- Setup Remote Repository
git remote add <name> <url> # This command adds a new remote connection with the alias <name> pointing to <url>. # You can then use <name> as a shortcut for <url> in other Git commands.
-
Fetch Changes:
git fetch [remote]
This command updates your local repository with changes from the remote, without merging them.
-
Pull Changes:
git pull [remote]
Fetches changes and merges them into the current branch.
-
Push Changes:
git push [remote] [branch]
Pushes your local changes to the remote branch.
- Setting an Upstream Branch
You can link your local branch with a remote branch using thegit push -u <remote> <branch> # for example git push -u origin main
-u
or--set-upstream
flag. This makes futuregit push
andgit pull
commands simpler by associating the local branch with a specific remote branch.
6. Git Stash
Git’s stash feature lets you temporarily store changes without committing them.
-
Stash Changes:
git stash
Stashes current changes, so you can work on something else.
-
Apply Stashed Changes:
git stash pop
Reapplies the most recent stash and removes it from the stash list.
-
Delete a Stash:
git stash drop
Deletes the specified stash.
7. Inspecting Project History
Git’s logging tools allow for an in-depth look at the project history.
-
View Commit Log:
git log
Shows the commit history.
-
Compact View:
git log --oneline --graph --decorate
Displays a compact, graphical view of commits with branch and tag references.
-
View Recent Commits:
git log -n [count]
Limits the output to the last
n
commits.
8. Tagging Commits
Tags are useful for marking specific points in history, like version releases.
-
Create a Tag:
git tag [tag_name]
Adds a lightweight tag to the current commit.
-
Annotated Tag:
git tag -a [tag_name] -m "Tag message"
Annotated tags include additional metadata, like messages.
9. Undoing Changes
Git provides commands to undo mistakes, but use them with caution.
-
Unstage Changes:
git reset [file]
Removes the file from the staging area without changing the working directory.
-
Revert a Commit:
git revert [commit_hash]
Creates a new commit that undoes changes from a specific commit.
-
Discard All Local Changes:
git reset --hard
Resets the working directory and staging area to the last commit.
10. Ignoring Files with .gitignore
A .gitignore
file specifies files and directories Git should ignore.
# Ignore log files
logs/*
# Keep specific file
!logs/.gitkeep
# Ignore temp files
*.tmp
Add patterns to exclude specific files, like logs or temporary files, from Git tracking.
Final Tips
Mastering Git takes practice, and each project will offer new challenges to grow your Git skills. Try using this cheat sheet as a reference for a smoother, more efficient Git workflow.
For more detailed learning, consider checking the free Pro Git book or downloading this cheatsheet. Happy coding!
Tech Wizard
Published on
How do i bookmark this