A List of Helpful Git Commands
Here are a few Git commands I use daily:
$ git checkout -b <new_branch_name>
$ git push -u origin <new_branch_name>
These two commands create a new local branch (from the currently checked out branch), and push it upstream (publish it).
$ git checkout -t origin/<remote_branch>
This command allows you to checkout a remote branch. The -t
flag tells Git you wish to track the branch.
$ git reset HEAD <file_path>
This command will remove files from the working directory (before committing). If <file_path>
is simply a .
all files in the working directory will be removed.
$ git reset --hard <commit_hash>
With the --hard
flag, this command moves the pointer back to the commit specified (the <commit_hash>
) and deletes all changes made after.
$ git commit -a -m "Commit message"
This commit command adds all files to the working directory before committing.
$ git add -A
This command combines git add .
and git add -u
. It stages all files in the working tree that are either modified, deleted, or new.
$ git checkout <parent_branch>; git pull; git checkout <child_branch>; git merge <parent_branch>
These commands are used to ensure all changes to the current branch are up to date, by merging in the parent branches recent changes.
$ git stash
Stashes all files in the working tree into a stack. This allows you to jump between branches without losing any work.
$ git stash pop
Since the stash is a stack, you can pop to reinstate the most recent work into the working tree.
$ git stash list
Or, you can simply look at the stack.
$ git pull -p
This pulls all changes from a remote, whilst also removing the remote branches that don’t have a local counterpart.
$ git checkout -- <file_path>
This command is really helpful. It allows you to completely remove all changes made on a given file (or just use .
to remove all changes).
To see a full list of git commands, check out either:
http://gitref.org/ or http://git-scm.com/docs/
Written by Joshua Croad
Related protips
2 Responses
FYI: git commit -a -m "Commit message" := git commit -am "Commit message"
Great tips :)
Nice one for git basics commands