รวมคำสั่ง Git Commands ที่ใช้งานบ่อยที่ Dev ควรรู้จัก
· 3 min read
Table of contents
Intro
สวัสดีครับ บทความนี้จะพามารู้จักกับ Git Commands ที่ Developers ทุกคนควรรู้จักโดย Git นั้นเป็นส่วนหนึ่งที่สำคัญของการเขียนโปรแกรมเรียกได้ว่าเป็นเครื่องมือสามัญประจำ Dev แต่เนื่องจากคำสั่งต่าง ๆ นั้นมีมากมาย และการเรียนรู้ Git จึงต้องใช้เวลา ซึ่งบางคำสั่งมักใช้บ่อย ๆ บทความนี้จึงได้รวบรวมคำสั่ง Git ที่มีประโยชน์ที่สุดที่นักพัฒนาทุกคนควรรู้
Git Commands List
Initialize a local Git repository
Initialize a local Git repository
git init
Clone repository
Clone repository
#Public
git clone [repo_url]
#Private
git clone ssh://[email protected]/[username]/[repository-name].git
Check files status
Check files status
git status
Add a file to VCS
Add a file to VCS
#Single file
git add [file-name]
#Multiple files
git add [file-path]
#All new and changed files
git add -A
Commit changes
Commit changes
git commit -m "[commit message]"
Remove a file (or folder)
Remove a file (or folder)
git rm -r [file-name.txt]
Branch Management
Branch Management
#List of branches
git branch
#List all branches (local and remote)
git branch -a
#Create a new branch (Local)
git branch [branch-name]
#Delete a branch (Local)
git branch -d [branch-name]
#Force Delete a branch (Local)
git branch -D [branch-name]
#Delete a remote branch
git push origin --delete [branch-name]
#Create a new branch and switch to it (Local)
git checkout -b [branch-name]
#Clone a remote branch and switch to it
git checkout -b [branch-name] origin/[branch-name]
#Rename a branch (Local)
git branch -m [old branch-name] [new branch-name]
#Switch to a branch
git checkout [branch-name]
#Switch to the branch last checked out
git checkout -
Revert
Revert
#Discard changes to a file
git checkout -- [file-name.txt]
#Discard changes to multiple files
git checkout -- [file0.txt] [file1.txt] [file2.txt]
#Discard changes folder
git checkout -- [folder-path]
#Stash changes (in working directory)
git stash
#Remove all stashed entries
git stash clear
#Revert commit changes
git revert [commitId]
#Revert the changes specified by the fourth last commit in HEAD and create a new commit with the reverted change
git revert HEAD~3
#Rewind the master branch to get rid of those three commits
git reset --hard HEAD~3
#Fix Merge conflict
git reset --hard
Fetch
Fetch
#Fetch changed state all branch
git fetch <remote>
#Fetch a branch
git fetch <remote> <branch>
#Fetch but not apply them
git fetch --dry-run
Merge
Merge
#Merge a branch into the active branch
git merge [branch-name]
#Merge a branch into a target branch
git merge [source branch] [target branch]
Pull
Pull
#Update local repository to the newest commit
git pull
#Pull changes from remote repository
git pull origin [branch-name]
#Similar
git fetch <remote>
git merge origin/<current-branch>
#Fetch but does not create a new merge commit
git pull --no-commi
#Fetch with rebase
git pull --rebase
Push
Push
#Push changes to remote repository (current branch)
git push
Config
Config
#Get global config
git config --global --list
#Set globally Username
git config --global user.name "your_username"
#Set globally Email
git config --global user.email "[email protected]"
#Add a remote repository
git remote add origin ssh://[email protected]/[username]/[repository-name].git
#Set a repository's origin branch to SSH
git remote set-url origin ssh://[email protected]/[username]/[repository-name].git
Preview changes before merging
Preview changes before merging
git diff [source branch] [target branch]
Logs
Logs
#View changes
git log
#View changes (detailed)
git log --summary
#View changes (briefly)
git log --oneline