Git Cheatsheet
One-stop shop for your common Git needs.
--
Basic commands
git init
: Initialize a new Git repositorygit clone <repository>
: Clone an existing repositorygit add <file>
: Add a file to the staging areagit commit -m "<message>"
: Commit changes with a messagegit push
: Push changes to the remote repositorygit pull
: Pull changes from the remote repositorygit status
: Check the status of the repository
Branching
git branch <branch>
: Create a new branchgit checkout <branch>
: Switch to a different branchgit merge <branch>
: Merge a branch into the current branchgit branch -d <branch>
: Delete a branch
Other useful commands
git log
: View the commit historygit diff
: View the changes made in the repositorygit reset <file>
: Unstage a filegit stash
: Save changes temporarilygit stash pop
: Restore the most recently stashed changes
Basic workflow
- Make changes to your local repository: Create or edit files, add them to the staging area using
git add
, and then commit the changes usinggit commit
. - Push your changes to the remote repository: Use
git push
to send your committed changes to the remote repository. - Update your local repository with changes from the remote repository: Use
git pull
to fetch and merge changes from the remote repository into your local repository.
Branching workflow
- Create a new branch: Use
git branch <branch>
to create a new branch. - Switch to the new branch: Use
git checkout <branch>
to switch to the new branch. - Make changes and commit them as usual.
- Merge the branch into the main branch: When you are ready to incorporate the changes from your new branch into the main branch (usually
master
), switch back to the main branch usinggit checkout <branch>
and then usegit merge <branch>
to merge the changes from your new branch into the main branch.
Undo a commit thats already pushed
If you want to leave the original commit in place, but just undo the changes it made, you can use the git revert
command to create a new commit that undoes the changes made by the original commit. For example:
git revert <commit-hash>
This will create a new commit that undoes the changes made in the original commit. You can then push the revert commit to the remote repository to undo the changes on the remote.
Common Developer Mistakes when using Git
Forgetting to commit changes
It’s important to commit your changes regularly so that you have a record of the progress you have made. If you forget to commit your changes, you may lose your work if something goes wrong.
Not committing often enough
On the other hand, it’s also important to commit your changes often enough so that you don’t have too many changes in a single commit. Large commits can be harder to review and troubleshoot if there are problems.
Try to commit atleast once per hour
Not branching
It’s a good idea to use branches when working on new features or making significant changes to your codebase. This helps to keep your main branch (usually master
) stable and allows you to work on multiple features at the same time.
Not keeping the main branch clean
It’s important to keep your main branch (usually master
) clean and only include well-tested code. This makes it easier to roll back changes if necessary and reduces the risk of introducing bugs into your codebase.
Not reviewing changes before merging
It’s a good idea to review changes before merging them into the main branch, especially if you are working with a team. This helps to ensure that the code is of high quality and does not introduce any issues.