Add "Useful Git Commands" page

Hadi Nategh 2018-02-15 16:08:57 +01:00
parent edbe8bae29
commit a77a34873b

@ -1,29 +1,53 @@
### Branches
* list all branches (incl. remotes)
> [Wiki](Home) ▸ [Development](development) ▸ **Useful Git Commands**
> `git branch -a`
>
> * master
>
> remotes/origin/14.2
Useful Git Commands
=======
* switch to a branch you never checked out before, eg. above 14.2, and set up tracking
### Status
Getting current status of repository which would show modified/merged/stashed files or commits that need to be pushed.
> `git branch --track <branch> origin/<branch>`
```git status```
***
* switch to branch existing on your local repo
### Branch
In order to get list of branches or create one.
> `git checkout <branch>`
List of branches: ```git branch -a```
* merge a commit from a different branch (after pulling/fetching!)
Create branch: ```git branch <your branch name>```
> `git cherry-pick <sha1>`
Delete branch: ```git branch -b <branch name>```
* merge a not pushed commit from a different local repository
switch to another branch: ```git checkout <branch name>```
<br> e.g.: ```git checkout 17.1```
> `git --git-dir=../<some_other_repo>/.git format-patch -k -1 --stdout <commit SHA> | git am -3 -k`
***
### Merge
Sometimes we need to merge our branch with master.
```
git checkout <your branch>
git rebase master
```
* remove empty commits after import of partial repo via Github importer
(has to be done with every branch and everyone has to clone the repo again!)
or when we want to merge only a single commit into our branch from another branch (e.g. backporting):<br>
```
git cherry-pick <commit hash>
```
`--no-commit`: this will help if you don't want your cherry-pick be committed right after.
> git filter-branch -f --commit-filter 'git_commit_non_empty_tree "$@"' HEAD; git push -f
***
### Revert
For reverting uncommitted changes on files you may run the following:
```
git checkout <filename>
```
and for reverting commits which are not pushed you may use:
```
git reset --hard <commit hash>
```
or if you want to go back to latest previous commit:
```
git reset --hard HEAD^
```
> **IMPORTANT:** *reset with `--hard` parameter reset your commits and you may no longer have access to your changes.*