GIT Basic Commands

From EdWiki

Tell Git who you are

git config --global "J.Shankarappa"
git config --global

Select your favorite text editor

git config --global core.editor vim

Tracking New Files

git add filename
git add . # recersively adds files and folders

Checking the Status of Your Files

git status

Staging New/Modified Files

git add filename
git add .

Git stages a file exactly as it is when you run the 'git add' command. If you modify a file, after you run 'git add', you have to run git add again to stage the latest version of the file.

Viewing Your Staged and Unstaged Changes

git status
git diff
git diff --cached
git diff --staged

Committing Your Changes

git commit
git commit -m "commit string"
Only staged file(s) will be committed

Skipping the Staging Area

git -am "New commit string"

Removing files

git rm filename

  • The 'git rm' command removes the file(s) from the staging area and also from the working directory.
  • If You have modified the file and added it to the 'index' already,
  • You must force the removal with the -f option.
  • If you want to keep the file in your hard drive (working tree) but not have Git track any more,
  • Add the file(s) to your '.gitignore' file or use the --cached option.

git rm --cached filename
git rm log/\*.log

Moving (Rename) Files

git mv from_file to_file
git mv README.txt README
git rm README.txt

Viewing the Commit History

git log # lists the commits made in that repository in revers chronological order.
git log -p -2 # last 2-commits diff
git log --stat # abbreviated stats
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph

Changing Your Last Commit

git commit -m 'Initial commit'
git add forgotten_file
git commit --amend

Unstaging a Staged File

git reset HEAD filename

Unmodifying a Modified File

git checkout -- filename

Getting Help

git help <verb>
git <verb> --help
man git -<verb>