Skip to content

Instantly share code, notes, and snippets.

@andykuszyk
Last active May 6, 2016 08:47
Show Gist options
  • Save andykuszyk/6fbd4ff8894c1a11e8cd50bb97c9165a to your computer and use it in GitHub Desktop.
Save andykuszyk/6fbd4ff8894c1a11e8cd50bb97c9165a to your computer and use it in GitHub Desktop.
Git command line cheat sheet

#Git command line cheat sheet

Workflow

In a typical workflow, you might perform the following steps in approximately this order:

  1. Fetch metadata about new branches from the remote repo;
  2. Checkout a remote branch locally;
  3. Stage changes;
  4. Commit changes (and repeat);
  5. Push your changes to the remote topic branch;
  6. Pull the origin master into your topic branch;
  7. Resolve any conflicts.

Fetch

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

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]

Stage

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

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

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

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

Resolve conflicts

To resolve conflicts one by one using you're configured merge tool:

git mergetool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment