Git Tips and Tricks
Here are some Git tips and tricks that you might find useful:
Use aliases to save time. Aliases are shortcuts for common Git commands. For example, you could create an alias for
git checkout
that you could use to quickly switch to a different branch. To create an alias, open your .gitconfig
file and add a line like this: [alias]
co = checkout
- Use the
git add -p
command to stage changes interactively. This command lets you review each change before you commit it. This can be helpful if you want to make sure that you’re only committing the changes that you want to commit. - Use the
git rebase -i
command to squash commits. This command lets you combine multiple commits into one. This can be helpful if you want to clean up your commit history or if you want to make a single commit that represents a specific feature or change. - Use the
git stash
command to save your work. This command lets you temporarily save your work so that you can switch to a different branch or work on a different feature. To use thegit stash
command, simply typegit stash
. To restore your work, typegit stash pop
. - Use the
git bisect
command to find the source of a bug. This command can be helpful if you’re trying to track down a bug in your code. To use thegit bisect
command, simply typegit bisect start
. Then, typegit bisect good
to mark the commit that you know is working correctly andgit bisect bad
to mark the commit that you know is not working correctly. Git will then bisect your commit history to find the commit that introduced the bug. - Use the
git log
command to view your commit history. This command can be helpful if you want to see what changes have been made to your code over time. To use thegit log
command, simply typegit log
. You can also use thegit log
command to view specific commits or ranges of commits. For example, to view the last 10 commits, typegit log -10
. - Use the
git diff
command to compare two versions of a file. This command can be helpful if you want to see what changes have been made to a file since a certain commit. To use thegit diff
command, simply typegit diff
. You can also use thegit diff
command to compare two different files. For example, to compare the current version of a file to the version of the file in the previous commit, typegit diff HEAD^
. - Use the
git merge
command to merge two branches together. This command can be helpful if you’re working on multiple branches and you want to combine your changes. To use thegit merge
command, simply typegit merge
. You can also use thegit merge
command to merge a specific commit into your current branch. For example, to merge the commit with the SHA1 of1234567890abcdef
, typegit merge 1234567890abcdef
. - Use the
git push
command to push your changes to a remote repository. This command can be helpful if you’re working on a project with other people. To use thegit push
command, simply typegit push
. You can also use thegit push
command to push to a specific remote repository. For example, to push to the remote repository with the name origin, typegit push origin
. - Use the
git pull
command to fetch changes from a remote repository and merge them into your current branch. This command can be helpful if you want to keep your local repository up to date with the latest changes from a remote repository. To use thegit pull
command, simply typegit pull
. You can also use thegit pull
command to fetch changes from a specific remote repository and merge them into your current branch. For example, to fetch changes from the remote repository with the nameorigin
and merge them into your current branch, typegit pull origin
.