Git Commands

mkdir invention
cd invention
git init
touch newton.txt
touch a.txt
git add newton.txt
git add a.txt
git rm -f a.txt
git commit -m “first commit”
git remote add red
git remote
git remote -v
git status
git stash
git status
git stash list
git stash apply
git push -u red master
git log
git stash
git revert HEAD~1
git log
git reset –hard HEAD~1         //It will remove the last commit. Hard means remove                                                             // entry from index also.
git log
git pull
git checkout -b v1
git checkout -b v2
git checkout v2
git branch (tell all branches in repo)
git branch -m master(rename current branch to master)
git branch -D v2                        // To delete branch. Make you are not in the same branch                                                       //while  deleting.
git merge v1
git diff

git clone

git reset --hard HEAD^  //to undo revert command

Squash last N commits ( say 3 )

git rebase -i HEAD~3

An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. Keep the first one as “pick” and on the second and subsequent commits with “squash”. After saving, another editor will be fired up with all the messages of commits which you want to squash. Clean up all the messages and add a new message to be displayed for the single commit.

Command to forcibly push the commits to remote branch.

git push –force remote_name branch_name

Command to update the commit message of last commit.

git commit -a --amend

Rename branch locally

git branch -m new_name //If currently on that branch

git branch -m old_name new_name //If currently not on that branch

Rename branch remotely

git push origin :old_name new_name

Remove files from git using gitignore

Solution: Write that file name in .gitignore file.

What if you already committed a file and now realised that it should be removed from git. So, here is the solution.

git rm --cached <file>
git add -A
git commit -am 'Removing ignored files'

It will remove the cache history of file from.

If you want to remove multiple files in a directory then

git rm -r --cached .

Then add and commit as done above.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s