Useful Git commands
Use case | Command | Comment/Explanation |
---|---|---|
Download (clone) a remote GIT respository | git clone [URL] | This command does not clone sub-modules. Example: git clone git@github.com:perthi/productivity.git |
Download (clone) a remote GIT repository including all sub-modules | git clone [URL] -b [branch] --recursive | Clones everything including sub-modules. Example: git clone git@github.com:perthi/logmaster.git -b master --recursive |
Merge a feature branch into your development branch | # git checkout develop # git merge feature/myfeature | This assumes that you have renot branch called origin/feature/myfeature that is checked out locally |
Check out an track a remote branch | git checkout --track origin/feature/myfeature | # This will checkout the remote feature branch *feature/myfeature* of the origin and track it. The name of the local branch will be *feature/myfeature* |
Merge a feature branch into your development branch and squash all commits on the feature branch into a single commit on the development branch | # git checkout develop # git merge --squash feature/myfeature | |
Commit all changes and a add a commit message from the command line | git commit -a -m "commit message" | |
Push all local commits to the remote repository (origin) | git push | |
Update your local repository with changes in the remote repository | git pull | |
Failed merge: You have just done a merge that failed an regret it dearly | git reset --hard HEAD | This only works if the you have not yet pushed your changes to the remote |
Remove branches that is still present in your local repo after they have been removed from the remote | #git remote prune origin #git branch -D mybranch ... X times | |
Delete a local branch | # git branch -d # git branch -D | The D (capital) option deletes the branch forcefully, i.e even if it is not fully merged. |
Create a local branch and push it to a remote repository (origin), Set up the local branch to track the corresponding remote | #git checkout -b feature/myfeature #git push -u origin feature/myfeature | The first command creates and checks out a local branch called feature/myfeature. The second command creates a corresponding remote branch (origin/feature/myfeature) and tracks it |
Delete a remote branch | git push origin --delete feature/myfeature | This will delete the branch origin/feature/myfeature at the remote |
Create an annotated tag | #git tag -a v1.4 -m "my version 1.4" #git push --tags |