Notes on Git Commands
This article was originally published on my old blog.
This article is more a personal note on standard git commands that I regularly use or sometimes forget. It might be useful to you in your source control flow with git. If you want to know about more git commands checkout gitready.com
Create local branch
git checkout -b newfeature
Delete local branch
git branch -d newfeature
Merge branches (newfeature branch into master branch)
git checkout master
git merge newfeature
Switch to specific branch
git checkout newfeature
List all branches (remote and local branches)
git branch -a
Create remote branch (origin being the remote git host repository)
git push origin newfeature
Delete a remote branch.
git push origin :newfeature
Get remote branch latest changes
git checkout master
List remote branches
git branch -r
Update your local dev branch to be a mirror of what’s in the main repository.
git checkout dev
git pull --rebase upstream dev
Manage file changes
Add new file
git add newfile.js
Commit all changes.
git commit -am "A very nice commit message."
Revert one file back to its original state
git checkout -- file.js
Temporary exclude the file from the commit.
git update-index --assume-unchanged path/to/file.txt
git commit -a -m "Commit Comment."
git update-index --no-assume-unchanged path/to/file.txt
Create a tag
git tag -a v22.214.171.124 -m "First release"
Show specific tag
git show v126.96.36.199
git describe --tags
Push tags to remote repository
git push --tags
Push one tag to remote repository
git push origin v188.8.131.52
Move git repository to a new server host
The best article that I found on how to do this is the Smashing Magazine article.
Short notes from the article:
Fetch all branches and tags from remote repository into local index
git fetch origin
Check out remote branches locally
git checkout -b develop origin/develop
Add new remote repository
git remote add new-origin email@example.com:blahblah.git
Push all local branches to new remote repository
git push --all new-origin
Push all local tags to new remote repository
git push --tags new-origin
Make new remote repository the default remote repository
git remote rm origin
git remote rename new-origin origin
You are done. Thanks git for making it easy!
Let me know which other git commands you use on a regular basis.
Here are some fun ones you can look up yourself:
git diff --name-status master..branch
git diff master..branch
git log master..branch
git shortlog master..branch