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

Local branches

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

Remote branches

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  
git pull

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

Tagging

Create a tag

git tag -a v1.0.0.0 -m "First release"

Show specific tag

git show v1.0.0.0

Show Tags

git describe --tags

Push tags to remote repository

git push --tags

Push one tag to remote repository

git push origin v1.0.0.0

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 git@github.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!

Summary

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

Written by

Passion for Software and Enterprise Architecture. I like to play with Azure and programming languages (C#, Rust, JavaScript and Angular).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store