Git Guide
Ta treść nie jest jeszcze dostępna w Twoim języku.
It is recommended to install Relivator following the detailed instructions in the README.md to feel more confident as you begin learning Git.
It’s true—Git can be complex at first. Consider using resources like this guide, the Git Book, and GitHub Skills to deepen your understanding. The command git commit —help will direct you to information about the git commit
command and its options, so this help command can be beneficial as well. The best way to get comfortable with Git is to use it regularly. Create a small project or use a large web project template like Relivator and experiment with different commands. If you’re ever unsure about something related to Git, refer to this detailed guide to learn more.
Git Initial Setup
By following the details in this guide, you will get a solid start with Git, set up your environment, and use some handy aliases to streamline your workflow. Happy gitting!
Essential Tools
Ensure you have Git installed. It’s also recommended to install: Node.js LTS (Windows/macOS | Linux). Then, run corepack enable pnpm to install pnpm. Additionally, we recommend installing VSCode and GitHub Desktop (Windows/macOS | Linux). If you’re a Windows user, also install PowerShell 7.4+.
Setting Up Your Identity
Before you start creating any commits in Git, you need to set your identity. This is important because your name and email will be added to every commit you make. Since this information is public, use something appropriate.
Checking Your Settings
To see all your Git settings and ensure they are correct, run:
Git References
Writing good commits is a valuable skill. To learn how to write effective commit messages, refer to the following resources:
- Enhance Your Git Log with Conventional Commits
- Karma Commit Messages
- Semantic Commit Messages
- A Note About Git Commit Messages
- Writing Git Commit Messages
Aliases
Git aliases are shortcuts for longer commands. They can save you a lot of typing and make your workflow more efficient.
Receiving Updates
This alias updates your local repository by pulling the latest changes, rebasing, and updating submodules.
Sending Updates
This alias pushes your changes to the remote repository, including tags.
Undo Staging of One or More Files
Sometimes you stage files by mistake. This alias helps you unstage them.
Tagging Releases According to Semantic Versioning (SemVer)
Semantic Versioning is a way to tag your releases with meaningful version numbers. These aliases help automate the process.
Ignoring Redundant git
Binary Names in Commands
You can avoid typing git git
by setting up an alias:
Displaying Changelog Since Latest Tag
To see the changelog from the latest tag to your current commit, use this alias:
Detecting Remnants and Leftovers from Development
Find common leftover markers like TODOs or debug prints in your code:
Recommendation
Using Your Favorite Editor for Git
You can set your favorite text editor to use with Git for writing commit messages, etc. For example, if you prefer gedit
on Ubun tu:
Handling Line Endings Correctly on Windows
Windows and Unix-based systems (like Linux and macOS) handle line endings differently. To avoid issues, configure Git to automatically handle this for you. This will convert line endings to the native Windows format (CRLF) on checkout and back to Unix format (LF) when you push changes.
Remembering (Caching) Passwords for HTTPS
When you clone repositories over HTTPS, you need to enter a username and password each time, unlike SSH keys. To make Git remember your passwords and make your life easier, use the following commands based on your operating system:
Usage
Update a Forked Repository (Sync with the Original)
-
Add the original repository as a remote (do this only once):
-
Get updates from the original repository and push them to your fork:
Reset a Repository to the Forked Repository’s State
-
Add the original repository as a remote (do this only once):
-
Reset your repository’s state:
Show All Ignored Files for a Repository
To list all ignored files:
Get a List of All Remotes for a Repository
To see all remote repositories associated with your local repository:
Remove All Newly Ignored Files
When you’ve added a file to .gitignore
that was previously in the repository, remove it from the repository:
Changing the URL of a Repository’s Remote
To change the remote URL:
Discard Unstaged Changes
To discard all unstaged changes:
To discard changes for a specific file or path:
Undo a Commit That Has Already Been Published
Safe method:
Dangerous method:
Undo a Local Commit (Not Published Yet)
To keep the changes in your working copy:
To discard the changes altogether:
Show Changes Made to the Working Copy
To show unstaged changes only:
To show staged changes only:
To show both unstaged and staged changes:
Delete a Branch
To delete a branch locally:
To delete a branch on the remote:
Adding a Description to a Commit
To add a commit message with both a title and a description:
Remove All Untracked Files and Directories
To preview what will be deleted:
To actually delete the files:
Show the Log in a Short Version
To display the commit log in a condensed format:
Create a Branch
To create a new branch but stay on the current branch:
To create and switch to a new branch:
Switch to Another Branch
To switch to another branch:
Tagging Releases
You can mark specific points in your repository’s history by adding tags. Tags are commonly used for releases but can be used for other purposes as well.
To tag the current commit, use the following commands. Replace <TAG_NAME>
with the unique name for the tag (e.g., v1.0.4
for versioning) and <DESCRIPTION>
with a description of the changes (optional).
Importing Commits, Pull Requests, and Other Changes via Patch Files
-
Get the patch file for the commit, pull request, or change you want to import. For GitHub pull requests, you can get the patch file by appending
.patch
to the URL of the pull request: -
Apply the patch file using
git apply
: -
Optionally, make additional changes to the imported code.
-
Commit the changes, mentioning the original author of the patch:
Copying a Branch
To create a local copy of an old branch under a new name and push it to the remote:
Moving a Branch
To rename a branch locally and on the remote:
Clearing a Branch and Resetting it to an Empty State
To create a new branch with no history and start fresh:
Counting Commits on a Branch
To count the total number of commits on a branch:
To count commits per author:
Undoing Changes
Undo Git Reset
If you mistakenly ran git reset --hard HEAD^
and lost commits, use git reflog
to find the commit and reset to it:
Undo Last Commit
To undo the last commit but keep the changes in your working directory:
Finding Folder Size
To find the size of a folder:
Clearing Git History
To remove files from history, use git filter-branch
:
Or use bfg
:
-
Install
bfg
: -
Run
bfg
to clean commit history: -
Remove files:
To replace text, create a passwords.txt
file with the following format:
Squash Commits
To combine multiple commits into one:
Undo Your Changes
To discard all changes:
Remove node_modules
if Accidentally Checked In
Amend Your Commit Messages
To change the commit message of the most recent commit:
Cherry-Picking
To apply a commit from another branch as a new commit:
Branch Management
Rename a Branch
To rename a branch, you can use the following commands:
Reset Local Repository Branch to Match Remote
To reset your local branch to match the remote repository’s main
branch:
Delete All Merged Remote Branches
To delete all remote branches that have already been merged:
Reset to Origin
To reset your local branch to match the remote:
Get Latest Commit of Repository
To get the latest commit of the repository:
Press Q
to exit the log view.
Get Hash from Latest Commit
To get the full hash of the latest commit:
To get the abbreviated hash:
To store the hash in a variable:
Tagging for Docker Versioning
Tag the repository and perform a commit:
Git Shortcuts
Set up aliases to simplify common Git commands:
Getting the GitHub Repository Name and Owner
To get the repository URL and name:
Delete Branch Locally
To delete a branch locally:
Clear Local Deleted Branches and Fetch All Other Branches
Remove All Local Branches Except the Current One
Sort Branches by Last Commit Date
To list branches sorted by the last commit date:
Commit Management
Git Commit Messages
- feat: A new feature visible to end users.
- fix: A bug fix visible to end users.
- chore: Changes that don’t impact end users (e.g., changes to CI pipeline).
- docs: Changes to documentation.
- refactor: Changes to production code focused on readability, style, or performance.
List Branches that Have Been Merged
List Branches that Have Not Been Merged
Cleanup and Optimize Repository
To clean up unnecessary files and optimize the local repository:
Push Commits with Tags Automatically
Restore a File to a Given Commit
To restore a specific file to its state at a given commit:
Useful Git Commands and Techniques
Download Just a Folder from GitHub with Subversion (SVN)
To download a specific folder from a GitHub repository using SVN:
To create an alias for downloading docker-compose
templates:
Pre-Commit Hooks
Ensure you have a changelog edited in your current branch. Use a pre-commit hook to enforce this:
Git Rebase Favor Current Branch
To favor the current branch during a rebase:
More info on merge strategies for rebase
Git Post-Checkout Hook
To automate tasks after checking out a branch, use a post-checkout hook:
-
Create and set permissions for the hook:
-
Add the following script to
.git/hooks/post-checkout
:
Git Checkout a Single File from Main Commit
To revert a file to its state in the main branch:
Adding New Changes to the Latest Commit
To amend the latest commit with new changes:
Cleaning a Branch PR
If your branch is messy and you want to clean up the commits:
-
Create a new temporary branch:
-
Create a patch file of changes:
-
Apply the patch to the temporary branch:
-
Clean up and rebase as needed, then delete the old branch:
-
Rename the temporary branch to the original name:
-
Force push the cleaned branch:
Better Push Force
Use git push --force-with-lease
instead of git push --force
for safer forced updates.
Learn more about force-with-lease
Git Fixup
To fix up a previous commit:
-
Create a fixup commit:
-
Rebase to squash the fixup commit:
Resources
- Relivator Next.js Template
- Theo’s Post
- Martin Heinz’s Blog
- Delight-IM’s Git Knowledge
- Alex Tan Hong Pin’s Cheat Sheet
The Bottom Line
This guide covers various useful Git commands and techniques, inspired by various resources and composed by Reliverse, making it easier for both beginners and advanced users to manage and optimize their repositories.