A daily basis list of Git commands.
By Giuseppe Maccario on Mon Apr 27 in Programming
Introduction
When I started using Git some years ago, I initially created a text file where I stored all the commands I used, pretty straightforward. Sometimes, though, I work on a different computer, so I decided to publish online my personal daily basis list of Git commands, in order to be able to consult it everywhere.
Install Git
Check the official Git website, download the right version for you. After the installation, check your git version using this command:
- git –version
Start a new repo
- git init
- git add your_files (multiple files are comma separated)
- git commit -m “First backup of my amazing idea”
- git remote add origin https://github.com/user/repo.git
- git push
Remember username and password
- git config –global credential.helper store
- git pull
Switch branches
- git checkout branch_name
Checkout remote branch (you don’t have it on local)
- git fetch origin ‘remote_branch’:’local_branch_name’
Create new branch
- git checkout -b feature_branch_name
Edit, add and commit your files.
- git push -u origin feature_branch_name
Checkout remote branch (that not see on local)
- git checkout –track origin/feature_branch_name
Show only local branches
- git branch
Show only remote branches
- git branch -r
SHOW ALL BRANCH (local and remote)
- git branch -av
MERGE BRANCH
In the case below, I want update master branch with the new changes on test branch:
- git checkout master
Get all the latest changes
- git pull origin master
- git merge test
Push your changes
- git push origin master
Delete branch
Delete branch locally
- git branch -d localBranchName
Delete branch remotely
- git push origin –delete remoteBranchName
Stash/unstash changes
- git stash
- git stash pop
Show files in last stash
- git stash show
Discard unstaged changes
- git stash save –keep-index –include-untracked
Show unpushed Git commits
- git log origin/master..HEAD
- git diff origin/master..HEAD
Inspect a commit (list only file names)
- git show –pretty=”” –name-only hash_id
Undo add file
- git reset file_name
Undoing the Last Commit
Note the –soft flag: this makes sure that the changes in undone revisions are preserved.
- git reset –soft HEAD~1
If you don’t want to keep these changes, simply use the –hard flag.
- git reset –hard HEAD~1
File mode
- git config core.fileMode false
- git config –global core.fileMode false
Remove a folder from git tracking
- git rm -r –cached path_to_your_folder
Conclusion
Thanks for your time for reading my daily basis list of Git commands. Check out my Open Weather App project, you can fork it on Github and modify it!