Skip to content

Instantly share code, notes, and snippets.

@raymzag
Last active October 2, 2019 00:21
Show Gist options
  • Save raymzag/c0d25646d9a78a94ec0baad5aa435dd6 to your computer and use it in GitHub Desktop.
Save raymzag/c0d25646d9a78a94ec0baad5aa435dd6 to your computer and use it in GitHub Desktop.
Git Useful Commands
# list config
git config -l
# update git user in current repo
git config user.email "[email protected]"
git config user.name "Raymond Aung"
# create new branch
git checkout -b new-branch
# switch branch
git checkout branch-name
# commit and push
git add .
git commit -m "change readme file"
git push origin branch-name
# pull codes, merge and push
# Preferably if it is a major change, instead of pushing directly into master,
# create a pull request to merge your branch to master branch, if necessary request request review before merging
git checkout master
git pull origin master # This will pull the latest codes from repo and update local master branch.
# Make sure you are in master branch when you do this, as pull will auto-merge
# if current branch you are in is different. pull = (fetch and merge)
git merge my-new-branch # This will merge my-new-branch with master. Resolve conflicts if there are any
git push origin master # This will update the master branch in repo
# saving local changes temporary, and pull remote changes and reapplying with rebase
git stash # This will save local uncommited changes temporarily in the stash,
# so that you can pull latest changes from remote repo first.
git pull --rebase origin master # This will bring latest changes from master into your current branch.
# if you have committed changes in your branch already, will rebase your changes
# after the latest changes from master
git stash pop # This will reapply your saved changes earlier into your current branch
# replace local copy with remote
git fetch origin
git reset --hard origin/master
# instead of creating a new commit, this will update previous commit with new changes
# will also have the option to amend the commit message
git commit --amend
# just amend the author for last commit
git commit --amend --author="Hello World <[email protected]>"
# diff between last two commits
git diff HEAD^ HEAD
# list is compiled from stackoverflow answers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment