Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Last active July 11, 2020 03:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save heiswayi/f8972e55fa952656cf8d64ac8385613a to your computer and use it in GitHub Desktop.
Save heiswayi/f8972e55fa952656cf8d64ac8385613a to your computer and use it in GitHub Desktop.
Commonly Use Git Commands Notes

To delete a branch

Local branch: git branch -d <local_branch> Remote branch: git push origin --delete <remote_branch>


To create new branch from existing branch

git checkout -b <new_branch_name> <source_branch_name>

To push my branch to remote server

git push -u origin <my_branch>

To view commit history logs

git reflog

To reset to particular commit log

git reset HEAD@{N}

*N is target number of commit log to reset to.


To show last git commit message on current checkout branch

git show --summary

To revert commits in certain branch to particular commit

git checkout <branch>
git reset --hard <commit-hash>
git push -f origin <branch>

To fetch all git branches

git pull --all

To create local branch to tracks remote branch

git checkout --track origin/john_branch

where --track is shorthand for git checkout -b [branch] [remotename]/[branch]


To reset local branch to match latest remote branch

git fetch origin
git reset --hard origin/master
git clean -fd

To rename a branch

git branch -m old_branch new_branch         # Rename branch locally    
git push origin :old_branch                 # Delete the old branch    
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Successful git branching

Ref: https://nvie.com/posts/a-successful-git-branching-model/

Creating a feature branch

git checkout -b myfeature develop

Incorporating a finished feature on develop

git checkout develop
git merge --no-ff myfeature
.
.
.
git branch -d myfeature
git push origin develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment