#Git command line cheat sheet
In a typical workflow, you might perform the following steps in approximately this order:
- Fetch metadata about new branches from the remote repo;
- Checkout a remote branch locally;
- Stage changes;
- Commit changes (and repeat);
- Push your changes to the remote topic branch;
- Pull the origin master into your topic branch;
- Resolve any conflicts.
Fetch metadata about changes in the remote repo.
git fetch -p
The
-p
or--prune
option removes references to remote branches in your repo that have been been removed in the remote repo.
Checkout a topic branch you've created in the remote as a new branch in your local repo, that tracks the remote topic branch.
git checkout -t origin/[remote-branch-name]
The
-t
option indicates that you want to checkout a new tracking branch. You could create a new branch locally (that doesn't track a remote branch) with:
git checkout -b [branch-name]
Add or Stage files ready for commit. Stage all files with:
git add .
Stage a particular file with:
git add Path\To\MyFile.cs
Wildcards can be used with add, so in the above example, you could use:
git add *MyFile.cs
Commit staged files to the repo.
git commit -m "My commit message"
The
-m
option indicates that you want to provide a commit message inline. You can omit this and enter a multi-line message directly in the console.
If you want to commit all changes without adding them first, use:
git commit -am "My commit message"
Push you're changes up to the remote tracking branch.
git push
You would have set a remote tracking branch when you checked out the branch with
git checkout -t
. If you're branch is not tracking a remote branch, you can use:
git push -u origin [remote-branch-name]
Pull changes from the remote tracking branch (e.g. collaborating on the same branch):
git pull
Pull from the master branch in the remote (bring your topic branch up-to-date):
git pull origin master
To resolve conflicts one by one using you're configured merge tool:
git mergetool