Pages

Sunday, October 6, 2013

Useful git commands

Here are some useful git commands :

File and directories related commands :
git add filename.txt  -> adds file to staging area index
git status -> shows the status of files in staging area
git reset HEAD filename.txt  -> removes file from current staging area index
git add . -> Looks at working tree and adds all those paths to the staged changes that are either changed or new (and not ignored). It does not state any ‘rm’ actions.
git add -u -> looks at all the currently tracked files and stages changes to those files if they are different or if they have been removed. It does not add any new files, it only stages changes to already tracked files.
git add -A -> equivalent to git add. and git add -u
git add * -> adds all files under the current folder
git rm file1.txt -> Removes a file


Branch related commands :
git branch –a  -> lists all local and remote branches
git branch –r   -> list only remote branches
git checkout mjk_branch1  -> Switch to mjk_branch1
git pull origin develop-> Pull ‘develop’ branch from origin. If it was already pulled earlier, then, it gets the latest content

Changing branches : Let’s say, you are working on a branch1, made some changes. Now, without checking your code in, you want to switch to another branch. Here are two ways of doing it :
If you want to discard the changes :
git reset --hard HEAD
git checkout newbranch
If you want to keep the changes:
git stash save
git checkout new_branch
//do code changes
git checkout old_branch
git stash pop

Check-in corrections :
Removing mistakenly added file from staging : git reset
Moving mistakenly committed files in local repo back to staging: get reset --soft HEAD^.  Next, you can also remove it from staging using git reset HEAD

Cleaning up a branch and removing all untracked files :
git reset --hard HEAD (restores the branch back to last push)
git clean -f -d (deletes all un-tracked, files in the current workspace)
git pull origin