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 -pcommand 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 -icommand 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 stashcommand 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 stashcommand, simply typegit stash. To restore your work, typegit stash pop. - Use the
git bisectcommand 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 bisectcommand, simply typegit bisect start. Then, typegit bisect goodto mark the commit that you know is working correctly andgit bisect badto 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 logcommand 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 logcommand, simply typegit log. You can also use thegit logcommand to view specific commits or ranges of commits. For example, to view the last 10 commits, typegit log -10. - Use the
git diffcommand 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 diffcommand, simply typegit diff. You can also use thegit diffcommand 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 mergecommand 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 mergecommand, simply typegit merge. You can also use thegit mergecommand 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 pushcommand 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 pushcommand, simply typegit push. You can also use thegit pushcommand 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 pullcommand 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 pullcommand, simply typegit pull. You can also use thegit pullcommand 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 nameoriginand merge them into your current branch, typegit pull origin.