Git useful commands
Git bash is a shell that is available to download and install in windows. It’s slimmed version of Cygwin whose only purpose is to provide shell command through command line to work with Git remotely. Please have below Git bash commands which we use frequently and get familiar with it:
Clone git repository:
- $ git clone https://github.com/javahonk/SpringMVCJDBCTemplateTutorial.git
Create branch out of master:
- $ git checkout -b javahonk
Merge origin to newly create branch:
- $ git fetch origin
- $ git merge origin/master
To see how many branch are available:
- $ git branch
Switch branch:
- $ git checkout javahonk
To switch to origin:
- $ git checkout master
See difference between master to branch:
- $ git diff –name-status master..branchName
For example if you want to see difference between master to javahonk:
- $ git diff –name-status master..javahonk
Best way to merge a git branch into master. Example if you want to merge javhaonk to master branch:
- $ git checkout master
- $ git pull origin master
- $ git merge javahonk
- $ git push origin master
Fetch all remote branch also you could see branches available for checkout:
- $ git branch -v -a
How to delete remote branch:
- $ git push origin : example: $ git push origin :javahonk
Push local branch to remote:
- $ git push origin branchname example: $ git push origin javahonk
Merge your changes to master without a fast-forward:
- $ git checkout master
- $ git merge –no-ff branchname example: $ git merge –no-ff javahonk
Merge your changes to master with a fast-forward:
- $ git checkout master
- $ git merge branchname example: $ git merge javahonk
How to delete local branch:
- $ git branch -d local_branch example: $ git branch -d javahonk
If you have some changes locally still want to delete it:
- $ git branch -D branchName example: $ git branch -D javahonk
How to push local changes to remote:
- $ git push
Create branch from another branch:
- $ git checkout -b createbranch anotherbranch example: $ git checkout -b feature develop
Finished features can be merged into the develop branch:
- $ git checkout develop
Switched to branch ‘develop’ - $ git merge –no-ff feature
Updating…
(Summary of changes) - $ git branch -d feature
Deleted branch myfeature (was 9888)
Push branch to remote:
- $ git push origin develop
To see full branch tree:
- $ git show-branch -a
Pull all changes form remote branch:
- $ git pull –rebase
To edit file use vi editor:
- $ vi filename as example below:
- $ vi SpringMVCController.java
Do modification through vi editor in file and use below command to save and exit. You will need to press [Esc] key followed by the colon (:) before using following commands:
Command | Description |
q | Quit |
q! | Quit without saving changes if you want to discard changes |
r fileName | Read data from file called fileName |
wq | Write and quit (save and exit) |
w fileName | Write to file called fileName (save as) |
w! fileName | Overwrite to file called fileName (save as forcefully) |