Must known Git commands

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency – http://git-scm.com/

Git has all the attributes of a distributed version control systems, it works in a on machine level but also in a collaborative way which is accessible for the network.

Suggested reading:

Command Description
git config –global user.name “YOUR_NAME” Your name will be displayed in the history.
git config –global user.email “YOUR_EMAIL” Your email will be displayed in the history.
git init <project name> Creates a local Git project ready for adding and committing file, including history but a local level.
git clone <path to git repository> Clone a remote git repository.
git remote List remote connection to other repos, using the context directory
git remote -v List remote connection to other repos, using the context directory, including URL
git remote add <name> <url> Create a new connection with <name> to a remote repo hosted at <url>
git remote rm <name> Remove remote connection with <name>
git remote rename <old-name> <new-name> Rename a connection
git fetch <remote> Fetch all of the branches from the repository identified by remote name. Resulting commits are stored as remote branches instead of local branches.
git fetch <remote> <branch> Fetch specified branch
git branch -r List all remote branches
git add . Add all the new files in the local repository – not yet committed in the local repository.
git commit -m “commit message” Commit all new additions and changed files in the local repository.
git push <remote> <branch>
git push -u origin <branch>
Push the specified <branch> to <remote>. In a git context the most used is the second command.
git status
git status –s
Shows what’s going on with the working directory. Added, modified, deleted. Adding –s will show the results in a concisely mode.
git diff Shows diffs of the affected files.
git branch Shows all the braches .
git checkout –b <new-branch> Create a a new-branch and switch to that new-branch.
git checkout <existing-branch> Switch to existing-branch.
git merge <branch-name> Merge branch-name into the context branch. See context branch using git branch command.
git log Display entire commit history.
git log –n 3 Show only last 3 commits.
git log  –oneline History is displayed in a single line.
git log –stat Include in history affected files.
git log –p Shows commit history in a detailed view.
git log <file> Shows commit history for specified file.
git log –author=”<pattern>” Shows commit history for specified author.
git revert <commit> Revert to the specified commit. List of commits can be viewed using git log.
git clean Remove untracked files from working directory
git clean -n Remove untracked files without actually removing files
git clean –f <path> Remove untracked files from specified path
git clean –df Remove untracked files and directories from the current directory
git clean -xf Remove untracked files from the current directory and any files that Git usually ignores
git reset –hard
git clean -f
Running both of them will make your working directory match the most recent commit, giving you a clean slate to work with