Mastering Git
Mastering Git
2. Aliases
Git allows you to create aliases for frequently used commands, making
your workflow more efficient. To set up an alias, use the git config
command or directly edit the .gitconfig file. For example:
3. Amend
The git commit --amend command lets you modify the most recent commit.
If you forgot to add some changes or need to update the commit message,
use this command:
4. Force Push
Sometimes you may need to overwrite the remote repository's history
with your local changes. Use the git push command with the -f or
--force flag:
5. Revert
The git revert command is used to create a new commit that undoes the
changes introduced by a specific commit. It is a safer way to undo
changes compared to git reset. Example usage:
6. Codespaces
GitHub Codespaces allows you to develop in an online, cloud-based
environment. It's useful for quickly testing and collaborating on code
without setting up a local development environment.
7. Stash
The git stash command helps you save changes that are not ready to be
committed yet, allowing you to switch branches without committing
everything. Later, you can apply the stash back to your working branch.
Basic usage:
git stash
git stash apply
9. Pretty Logs
Improve the appearance of your Git log with pretty formatting:
10. Bisect
Git bisect is a helpful tool to find the commit that introduced a bug or
issue. It performs a binary search through the commit history to find
the faulty commit. The process involves marking good and bad commits
until the problematic commit is identified:
11. Autosquash
When using git rebase -i, you can automatically squash or fixup commits
using the autosquash feature. Set the autosquash option in your git
config:
Now, when you run git rebase -i, the commits marked with "fixup!" or
"squash!" in the message will be automatically squashed.
12. Hooks
Git hooks allow you to run custom scripts before or after specific Git
actions (e.g., commit, push, merge). You can find hook scripts inside
the .git/hooks directory of your repository.
git checkout -
git add -p
16. Reflog
The git reflog command shows a log of all reference updates, including
branch checkouts and resets. It's helpful for recovering lost commits
or branches.
git reflog
17. Cherry-pick
Cherry-picking allows you to apply a specific commit from one branch to
another. This can be helpful when you want to include a single fix or
feature from another branch without merging the entire branch.
git clean -i