This repository is a collection of syntax, commands about git.
This cheat sheet contains 50 commonly used git commands.
👌 Feel free to use my repository and star it if you find something interesting 😄
-
-
Download/Install
Download Git from this link: Git - Downloads
After the file is downloaded, install it in the system.
Once installed, select Launch the Git Bash, then click on finish. Or right-click somewhere on desktop and choose Git Bash Here.
The Git Bash Console will be launched.
To check Git is installed or not, or check version, use this command:$ git --version
-
Setup
Set the name and email that will be attached to your commits and tags (change for suitable with your account)$ git config --global user.name "ptnghia3502" $ git config --global user.email "ptnghia3502@gmail.com"
-
Starting a Project with Git
Create a local repo (omit to initialise the current directory as a git repo)$ git init
Or download existed repo
$ git clone <url>
-
Make a Change
Add a file to staging or stage all files (recommend)$ git add <file> $ git add .
Commit all staged files to git
$ git commit -m "commit message"
-
Push to repo
If you create a local repo, you need to create new reposioty on your GitHub
On your GitHub profile, choose Repositories, click New, set name and description for your repo, then Create Repository
Get the HTTPS URL of your repo (Example:https://github.com/ptnghia3502/example.git
)
Remote your local repo to repo on Github and push your content:$ git remote add origin <url> $ git push -u orgin master
If you clone repo from somewhere, just use
git push
, it will auto push to repo where you clone from.$ git push
-
Quick Tip from GitHub
When you create new repository on GitHub, it will show fast command line
Or create a new repository on the command line:$ git init $ git add . $ git commit -m "first commit" $ git branch -M master $ git remote add origin <url> $ git push -u origin master
Or push an existing repository from the command line:
$ git remote add origin <url> $ git branch -M master $ git push -u origin master
-
-
-
Branches
List all local branches. Add-r
flag to show all remote branches,-a
flag for all branches$ git branch
Create a new branch
$ git branch <new-branch>
Switch to a branch & update the working directory
$ git checkout <branch>
Create a new branch and switch to it (combine of 2 above, I recommend use this)
$ git checkout -b <new-branch>
Delete a merged branch, and Delete a branch, whether merged or not
$ git branch -d <branch> $ git branch -D <branch>
Add a tag to current commit (often used for new version releases)
$ git tag <tag-name>
-
Merging
Merge branch a into branch b. Add--no-ff
option for no-fast-forward merge$ git checkout b $ git merge a
Merge & squash all commits into one new commit
$ git merge --squash a
-
-
-
Undoing Things
Move (&/or rename) a file & stage move$ git mv <existing_path> <new_path>
Remove a file from working directory & staging area, then stage the removal
$ git rm <file>
View a previous commit (READ only)
$ git checkout <commit_ID>
Create a new commit, reverting the changes from a specified commit
$ git revert <commit_ID>
Go back to a previous commit & delete all commits ahead of it (revert is safer). Add
--hard
flag to also delete workspace changes (BE VERY CAREFUL)$ git reset <commit_ID>
-
Review your Repo
List new or modified files not yet committed$ git status
Show changes between two commits
$ git diff <commit1_ID> <commit2_ID>
-
Synchronizing
Add a remote repo$ git remote add <alias> <url>
View all remote connections. Add
-v
flag to view urls.$ git remote
Remove a connection
$ git remote remove <alias>
Rename a connection
$ git remote rename <old> <new>
Fetch all branches from remote repo (no merge)
$ git fetch <alias>
Fetch a specific branch
$ git fetch <alias> <branch>
Fetch the remote repo's copy of the current branch, then merge
$ git pull
Upload local content to remote repo
$ git push <alias>
Upload to a branch (can then pull request)
$ git push <alias> <branch>
-
Hope this cheat sheet is useful!