Git Cheat Sheet
Git is a popular version control system that allows developers to keep track of changes made to their codebase, collaborate with others, and manage multiple versions of their code. It can be a powerful tool, but it can also have a steep learning curve for those new to it. That's where a Git cheat sheet can come in handy.
A Git cheat sheet is a quick reference guide that summarizes some of the most commonly used Git commands and their syntax. Here are some of the basic Git commands that you might find on a cheat sheet:
Git Basic commands:
git init: Creates / Initializes a new Git repository within the current directory to start tracking changes.
git clone <repository>: Clone an existing Git repository (on Github, Bitbucket, etc) to a new folder/directory
git add <filename>: Adds a file to the staging area (index) for the next commit
git add -p: Interactively adds changes to the staging area (useful for selectively staging changes within a file)
git commit -m "commit message": To commit changes that have been staged
git status: Displays the current state of both the working directory and the staging area
git log: Displays the commit history
git branch: Lists all local branches in the repository
git checkout <branch>: You can switch to a designated branch within the repository
git checkout -b <new-branch>: Creates a new branch and switches to the created branch
git merge <branch>: Merges the specified branch into the current branch
git pull: Fetch and merge changes from the remote repository to the current branch
git push: Used to send modifications made in the current branch to a remote repository
git stash: Allows users to temporarily store changes that are not yet prepared for commit.
Git Advanced commands:
git rebase <branch>: Reapplies commits from the current branch on top of the specified branch (useful for integrating changes from another branch into your own)
git reset <commit>: Resets the repository to a previous commit (use with caution as it can permanently discard commits)
git reset --hard: Resets the repository to the last commit and discards all changes in the working directory
git cherry-pick <commit>: Applies a single commit from one branch to another
git tag <tag-name> <commit>: Tags a specific commit with a meaningful name
git remote add <name> <url>: Adds a new remote repository with a given name and URL
git remote -v: Using the command displays a list of all remote repositories along with their corresponding URLs.
git push --tags: Pushes all tags to a remote repository
git submodule add <repository> <path>: Adds a Git submodule to the repository at the specified path
git submodule update: Initializes and updates Git submodules in the repository
git bisect: Binary search for a specific commit that introduced a bug (useful for debugging)
Git Tips:
Always start by checking the status of your repository before making any changes using git status
It's advisable to make frequent commits with clear descriptions by using the command 'git commit -m "commit message"
Use branches to keep your work organized and to collaborate with others
Always pull before you push to avoid conflicts using git pull and git push
Use .gitignore to ignore files and directories that don't belong in the repository
When in doubt, consult the Git documentation or seek help from experienced Git users.