Useful Git commands


A list of Git commands that I consider really useful when we work with this great version control  system. Enjoy it!

Configuring your Git

// Set username and email (global)
git config --global "My user name"
git config --global ""
// Display configuration
git config -l 
// Get some help :)
git contig -h
// Disable autocrlf to avoid warnings in command line
git config --global core.autocrlf false

Managing Git repositories

Initializing a repository. Execute the following command within a directory to make it "Git-ready".

git init

Show the working tree status.

git status

Adding files for tracking or to the staging area.
// Add all .txt files
git add *.txt

// Add all in the current directory
git add .

// Add a single file
git add

Doing your changes persistent in the local repository.

// Do a commit with all files in staging area
git commit -m "My custom message for this commit"

Watching previous version of files.

// Watch a version of a very specific file (using the commit ID)
git checkout YOUR_COMMIT_ID -- path/my_file.js

// Return to the version of that file in HEAD
git checkout HEAD -- path/my_file.js

Watching logs.

// Watch the log (details)
git log

// Watch the log in one line per commit
git log --oneline

// See two last commits
git log --oneline -2

// Show diff between files
git show

Merging a branch (my-branch) into other (master).

// Where do you want to merge?
git checkout master

// Which branch do you want to merge?
git merge my-branch

Working with branches

Creating, deleting and switching to a branch.

// Get a list of branches in the repository
git branch

// Create a new branch
git branch my-branch

// Switch to a branch
git checkout my-branch

// A shorthand for two previous commands (create and switch)
git checkout -b other-branch 

// Delete a branch
git branch -d my-branch
// Get a remote branch
git checkout -b <local_branch_name> <remote>/<remote_branch_name>
// The names for "my-local-branch" and "my-remote-branch" are usually the same
git fetch
git branch --all
git checkout -b my-local-branch origin/my-remote-branch

Working with remotes

Forcing a push in origin (remote) to a previous version as in our local repository.

// Suppose you want to return to a previous version
// in your local repository
git reset --hard YOUR_COMMIT_ID

// Then, probably you will need to force the remote to stay 
// in the same commit as your local repository
git push origin master --force

// Or
git push origin master -f
// Change the URL of a remote
git remote set-url <REMOTE_NAME> <REMOTE_URL>
// For example:
git remote set-url origin

Original source:

About the author

Enterprise Web Developer

Alex is an experienced technology professional. His background includes:

  • Web Technologies: Semantic Web (RDF, OWL), MVC Frameworks (CakePHP, CodeIgniter) Content Management Systems (Drupal, Joomla, WordPress), JavaScript libraries (jQuery, Mootools), CSS3, HTML5, mobile web (Sencha, jQuery mobile), Operating Systems, and,
  • Research: Complex Systems (Cellular Automata in specific), Artificial Intelligence, Computer Graphics and Computer Simulation to create computer models of living.