Overview
- Install Git Bash first
- Use the TAB button to reduce the typo
Frequently Used Commands
STORE Useful Config Settings
- These useful config settings are recommended to store them before using git
# Show configurations you already set
$ git config --list
# Set USERNAME and EMAIL into config
$ git config --global user.name [Your User Name]
$ git config --global user.email [Your Email]
# Set CREDENTIAL.HELPER into config
$ git config --global credential.helper store
# Set pull command always with rebase argument
$ git config --global pull.rebase true
# Reset credential.helper if token expired
$ git config --unset credential.helper
CLONE
# CLONE is one of the most important commands in Git if you want to clone an existing repository from remote to local.
# [repository path] is necessary, get the link from Git storage, it could be HTTPS or SSH style.
# [destination folder name] is optional, it will use the repository name by default.
# [Dest_Folder] is optional,
$ git clone [repository path] [destination folder name] [Dest_Folder]
e.g.
# git clone https://***@dev.azure.com/***/***/_git/RobotFramework TEST_FOLDER
# It will pops up a dialog for credential, such as password or PAT, the clone step will starts after type it.
CHECKOUT (CREATE) A New Local And Remote Branch
- Use the CHECKOUT command to create a new local branch
# -b <branch> create and checkout a new branch, it will create but keep in the current branch when "-b" command
# <branch name> could be anything but non-repeatable, and use use "_"(underline) to connect each word is recommended
$ git checkout (-b) <branch name>
PRUNE, To Cut Off Branches From A Plant
# If the branch has been deleted in one of the device, but the branch info stills exists in other devices, you need to use "prune" command to re-sync it.
$ git remote prune <repo>
e.g.
$ git remote prune origin
# Further, it will lists the branches has been deleted, add "--dry-run"
$ git remote prune origin --dry-run
PUSH The Local Branch To The Remote
# After checkout the new branch, it only exists in your local repository, and it should be put into remote repository as synchronize
# -u, --[no-]set-upstream set upstream for git pull/status
# <repository name> the default repository usually is "origin" if user doesn't change it
# <branch name> should be the same as the local branch
$ git push -u origin <branch name>
CHECKOUT (Switch) To Target Branch
- List branches in your repository and it has a star sign before your current branch.
# Ensure where your current branch is before editing
# -a, --all list both remote-tracking and local branches
# -l, --[no-]list list branch names
$ git branch -al
e.g.
DEV_Victor
MASTER
* add_api_case
main
# CHECKOUT to your target branch
$ git checkout <existed branch name>
# If it shows "Aborting" after you checkout to another branch, means you have some un-commit changes in the current branch, it's NOT allowed to checkout before fixing it, which we called "CONFLICT"
# Fix the conflict, try discard changes, or commit and push changes
# DON'T override them forcefully
Basis Commands
BRANCH
# This section introduces "git branch" command and its arguments
# Create a new branch
# <branchname> sholud not be duplicated
$ git branch <branchname>
# -b <branch> create and checkout a new branch, it will create but keep in the current branch when "-b" command
$ git checkout (-b) <branch name>
# -m, --[no-]move move/rename a branch and its reflog
# -M move/rename a branch, even if target exists
# <oldbranch> the current branch name
# <newbranch> the target branch name
$ git branch -m <oldbranch> <newbranch>
# DELETE local branch
# -d, --[no-]delete delete fully merged branch
# -D delete branch (even if not merged)
$ git branch -d <branchname>
CHECKOUT
# If you don't know the exactly branch names can be checkout, use "git branch -a" to check it
# Checkout to the branch stills alive after delete
$ git checkout [branch name]
# Create a local branch and connect it with an existed remote branch
$ git checkout -b [branch name] [repo name + branch name]
e.g.
$ git checkout -b test456 origin/test123
# Switched "Head" to previous commit
# [num] should be a positive integer, and it will head to the number of pervious commit from current head position.
$ git checkout [branch name]~[num]
e.g.
$ git checkout HEAD~2
COMMIT
# Commit command only works after any changes has been staged
# Commit message options
# -m, --[no-]message <message> commit message
# Commit contents options
# --[no-]amend amend previous commit (override)
# "commit message" should NOT be blank
$ git commit -m "commit message"
# It should be written as an imperative sentence
# Starting with a verb (e.g. Add, Update, Change, Delete and Remove...etc)
e.g.
$ git commit -m "Added 2 personal settings files into Resource dir"
DELETE
# This section introduces how to delete branch locally and remotely
# DELETE local branch
# -d, --[no-]delete delete fully merged branch
# -D delete branch (even if not merged)
$ git branch -d <branchname>
e.g.
$ git branch -d test123
# DELETE remote branch
# -d, --[no-]delete delete refs
# The TAB button doesn't works in this command to prevent accident, you need type all string by self
$ git push -d [repository name] [branch name]
e.g.
$ git push -d origin test123
FETCH
- FETCH command will download commits, files, and refs from remote repos, but will NOT MERGE(override) changes.
- PULL = FETCH + MERGE, if you need more info about PULL, please refer to it.
$ git fetch
# To remove redundant branches, try this command.
$ git fetch -p origin
MERGE
# 1. Merge with commit message.
# --[no-]ff allow fast-forward (default)
# --no-ff non-Fast-forward, and default is disable, if you don't input this command
# -m, --[no-]message <message> merge commit message (for a non-fast-forward merge)
$ git merge --no-ff DEV -m "Commit Message"
# If conflict occurs when merge, it may shows message like this:
CONFLICT (add/add): Merge conflict in TestArea/test123.py
Automatic merge failed; fix conflicts and then commit the result.
So you have to fix conflict.
# IDE will shows conflict codes, and we have to delete redundant, after conflict resolved, we have to re-push new code to branch
$ git add [fixed conflict file]
$ git commit -m "commit message"
$ git push -u origin [branch name]
PULL
# "PULL with REBASE argument" is more recommended to use than only "PULL"
# If multiple users commited the same branch, the pull command will auto-created a commit and merge all changes into it, so it's hard to trace errors
# The "PULL with rebase" will pull the changes already exists from remote and put your changes behind it, so no more redundant commits exists
$ git pull --rebase
# PULL = FETCH + MERGE, if you need more info about FETCH, please refer to it
# It will override your current local branch up-to-date
# If files show in both remote and local branches and are mismatched, it's called CONFLICT
$ git pull
# If it shows "Aborting" after you checkout to another branch, means you have some un-commit changes in the current branch, it's NOT allowed to checkout before fixing it, which we called "CONFLICT"
# Fix the conflict, try discard changes, or commit and push changes
# For people who agrees "PULL with rebase" but lazy to type extra argument
# Set pull command always with rebase argument as default
$ git config --global pull.rebase true
PUSH
# PUSH changes to remote branch
# -u, --[no-]set-upstream set upstream for git pull/status
# <repository name> the default repository usually is "origin" if user doesn't change it
# <branch name> should be the same as the local branch
$ git push -u origin <branch name>
# DELETE remote branch
# -d, --[no-]delete delete refs
# The TAB button doesn't works in this command to prevent accident, you need type all string by self
$ git push [repository name] -d [branch name]
e.g.
$ git push origin -d test123
# To be verified
# If your <repository name> isn't "origin", you can query it via this command
$ git remote -v
e.g.
It could be likes this:
origin https://***@dev.azure.com/***/***/_git/RobotFramework (fetch)
If your <repository name> isn't "origin", you can query it via this command.
REMOTE
# It shows advanced info about your git remote repository, such as tracking branch, local branches existed and their status...etc
$ git remote show origin
* remote origin
Fetch URL: https://***@dev.azure.com/***/***/_git/RobotFramework
Push URL: https://***@dev.azure.com/***/***/_git/RobotFramework
HEAD branch: MASTER
Remote branches:
DEV_Victor tracked
MASTER tracked
add_api_case tracked
main tracked
Local branches configured for 'git pull':
DEV_Victor merges with remote DEV_Victor
MASTER merges with remote MASTER
add_api_case merges with remote add_api_case
main merges with remote main
Local refs configured for 'git push':
DEV_Victor pushes to DEV_Victor (up to date)
MASTER pushes to MASTER (up to date)
add_api_case pushes to add_api_case (up to date)
main pushes to main (up to date)
STAGE Changes
# When you edited the code and want to stage changes
# Use "."(dot) to stage all changes under the directory
$ git add <file / directory>
e.g.
# If you input "Resources/.", which means you staged all changes under Resources directory
$ git add Resources/.
# You can input a "."(dot) below ADD command, which means all changes will be staged. (POWERFUL but UNRECOMMENDED)
$ git add .
STATUS
# These commands will show current status of remote and local
# -b, --[no-]branch show branch information
# -h, --[no-]help show usage commands
# -s, --[no-]short show status concisely
e.g.
$ git status -b
On branch add_api_case
Your branch is up to date with 'origin/add_api_case'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: TestArea/try_catch.robot
no changes added to commit (use "git add" and/or "git commit -a")
e.g.
$ git status -s
M TestArea/try_catch.robot
Reference
- Git Tutorial 1 - Git Basis Use
- Git Tutorial 2 - Easy Learn Git
- Git Tutorial 3 - Git Steps
- Git Tutorial 4 - Git Command Lists
- Checkout and Reset
- Installing Git on Windows
- Installing Git on Ubuntu
- git-branch-commands
*https://nulab.com/zh-tw/learn/software-development/git-tutorial/git-commands-settings/git-branch-commands/