Git Cheatsheet
Hello everyone, here is a quick reference guide of the git commands:
git config
Setup author name and mail to be used for all commits in current repository
git config --global user.name "username"
git config --global user.email "[email protected]"
git clone
Clone the remote repository with HTTPS or with SSH. This should create a new child directory for the project sources.
git clone <REPO CLONE URL>
git status
Shows the status of your current branch. It will tell you which files you have modified, which files are stages for commit and which files are not. It will also let you know if your local branch is behind, or not up to date with, its remote tracking branch. This command is particularly useful in combination with?git add?when you only want to commit certain files.
git branch
Create a branch, or show a list of local or remote branches. With git branch, you can add options to do branch-related things:
git fetch
Fetches branches from the server, along with their history and information. You can also use?git fetch origin?to update your remote-tracking branches.
git pull
Update your working directory by performing a git fetch, followed by a git merge. This is useful if you want to bring your branch up to date with the remote-tracking branch.
You can also use the?--rebase?option to perform a rebase rather than a merge.
git checkout
This command has a few functions. It can be used to create a new branch off of the current branch, it can be used to checkout a branch from the server, or switch to another branch.
git add
You made changes to your branch, and you want to stage your changes for committing. You will need to add them to what is known as the?index. Here are two ways to do this:
git commit
You want to make a commit! Awesome! Here's how you do it:
领英推荐
git push
Once you make a commit, you will need to push your changes to the server to make it visible to the world. Once you push, your branch and commit(s) will be visible to others.
git log
Show the history of all branches and their commits. Useful for seeing the most recent commit and getting commit hashes.
git rebase
Rebasing is a bit more advanced, but incredibly useful when you need it. When you perform a rebase, you are changing the base of your branch. In essence, a rebase will look at each commit on your branch and update the code to make it seem like you've been working off the new base all along. Sometimes, a rebase will barf if it encounters a situation in which it tries to update a piece of code that you have already modified. In this case, it doesn't know which version of the code to use, so it leaves it to you to resolve them manually.
Although they have similar uses, a rebase differs from a merge in that a rebase updates your branch by tweaking each commit, and merge will update your branch by creating a new commit at the tip of your branch. Usually, there are standards and practices employed in a project or team around which method is preferred. Have a discussion with your team about which workflow they prefer.
git stash
Stashing allows you to save your current unstaged changes and bring your branch back to an unmodified state. When you stash, your changes are pushed onto a stack. This is especially useful if you need to quickly switch to another branch without having to commit your incomplete changes.
git reset
Git reset is used to unstage files or remove commits. It does so by changing what the tip of the branch (HEAD) points to.
Three?trees?are affected by a reset:
-> git add .
-> git reset -- file.html?(unstage single file, or)
-> git reset?(to unstage all)
-> git commit -a -m "commit message"
-> git reset --soft HEAD^?(undo commit that is at the tip of the branch)
-> git commit -a -m "new commit message"
-> git reset --hard HEAD~2?(destroy the last two commits, i.e. HEAD and HEAD^)