Skip to content

Instantly share code, notes, and snippets.

@echorohit
Forked from tejnri/Git Commands
Created August 7, 2013 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echorohit/6171441 to your computer and use it in GitHub Desktop.
Save echorohit/6171441 to your computer and use it in GitHub Desktop.
Common git commands
-------------------
Start:
------
git init : to initialize a empty project.
git clone <git url> <path to store> : Get some others project. But this will only give master branch.
git checkout -b <branchname> origin/<other branch name> : This will fetch the branch from remote you want but this command could only be run after 2nd command.
Work on files
-------------
git add : to add file to stating (to put things into commit cache).
git rm --cached <filename> : to remove files from staging.
git reset HEAD <filename> : to remove files from staging.(Another way to unstage files).
git commit -m "Some message" : It will commit all the files in the staging area.
git commit <filename1> <filename2> -m "Some message": This will add and commit the particular file only even if there are others files in the staging area. The file should not be an untracked one. It should have been added some time before.
git diff : it will show the diff of all the modified files
git diff <filename1> <filename2> : it will show the diff of all the files.
git log: to see all the commit logs
git log --oneline : to see only msgs
git show HEAD~3:/path/to/file : To see the content of the file three commits before
git show HEAD~3 : To see the commit diffs in the 3rd commit before.
Branching and Merging:
----------------------
git branch : To list all the branch and the current branch you are into.
git branch <branch name> : To create a new branch
git checkout <branch name to switch to> : To switch to a specific branch.
git merge <branch to be merged> : To merge a specific branch into the branch you are currently in
Pushing the work on the remote server:
--------------------------------------
git remote add <reference name for remote location> <url of the git repository> : To add a remote repository.
git push <remote reference name> <which branch to push> : This command will push all the unpushed commits to the remote repository along with the logs and msgs.
Taking update from remote server:
---------------------------------
1st way:
--------
git fetch : It fetches the content of the current branch from the remote server but do nothing.
git merge : it then merge the local branch with the content fetched from the remote server
2nd way:
--------
git pull : Its the short for of the above commands combined
3rd way:
--------
git fetch : It fetches the content into a tmp dir
git rebase: It then merges the local branch with the content but also take commit order into account.
The output of all the ways are almost identical in all the cases
To add tags:
------------
git tag -a <tagname> -m "comment" : To add tag.
git push <remote reference name> --tags : It will push all the tags to the remote repository
Reverting files:
----------------
git reset --hard <hash of commit> : This will reset the whole branch to the commit corresponding to the hash tag. It will only work for those commits which are not pushed yet.
git reset --hard HEAD~3 : This will reset the whole branch to the 3rd commit from the last. It will only work for those commits which are not pushed yet.
git revert HEAD : This will revert the HEAD commit ie last commit. You may have to run it many times to revert it to your desired level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment