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 user.name "My user name"
git config --global user.email "email@example.com"
// 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".
Show the working tree 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 README.md
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
// Watch the log (details)
// Watch the log in one line per commit
git log --oneline
// See two last commits
git log --oneline -2
// Show diff between files
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
// 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 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
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 https://firstname.lastname@example.org/scm/btgblue/base22blueprint3basestructure.git
Original source: http://www.alex-arriaga.com/some-useful-git-commands/