GIT - The 'c*' Git commands

mail

git cherry-pick

Usage

Apply the changes introduced by some existing commits :
  1. make sure the working tree is clean : no changes on HEAD
  2. pick one or more commits
  3. these commits are re-applied, recording a new commit for each picked commit
    Commits are not moved, they are copied.
Which gives :

Don't forget :

  • cherry-pick commits in the chronological order
  • successfully cherry-pick'ed commits are regular commits, which means you can undo a cherry-pick just like you would with any commit (source)

Flags

Flag Usage
--abort cancel the operation and return to the pre-sequence state
--quit Forget about the current operation in progress. Can be used to clear the sequencer state after a failed cherry-pick or revert.
When a cherry-pick is going bananas, what's the difference between doing --abort or --quit ?
mail

git clean

Usage

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

Flags

Flag Usage
-i --interactive show what would be done and clean files interactively
-n --dry-run don't actually remove anything, just show what would be done
mail

git commit

Usage

git commit stores the current contents of the index (+ a log message) in a new commit, into the repository. The data to be committed can be specified either : The usual syntax is :
git commit [options, whether to commit index or listed files] -m "commitMessage"

Flags

Flag Usage
--amend instead of creating a new commit, git commit --amend adds stuff into the previous commit. The typical use cases are when :
  • you forgot to commit a file
  • you discover a minor error just after committing
  • you want to edit the commit message (typo, missing information, ...)
This rewrites the history and, as usual, must not be applied to pushed commits.
--author=author Override the commit author, author can be :
  • a user specification formatted like : Thomas ANDERSON <thomas.anderson@metacortex.com>
  • a pattern used to search for an existing commit by that author (i.e. rev-list --all -i --author=author); the commit author is then copied from the first such commit found
--date 'someDate' Override the author date used in the commit.
-m message
--message=message
Use message as the commit message.
If multiple -m options are given, their values are concatenated as separate paragraphs.
--no-edit To be used with --amend : don't offer to edit the commit message when amending a commit
mail

git checkout

Usage

Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch.

Flags

Flag Usage
(none) git checkout branch : "cd" to branch
-b
git checkout -b newBranch
create + checkout the branch newBranch
git checkout -b newBranch commitId
create a new branch named newBranch from commitId
git checkout -b branch origin/branchName
checkout the remote branch origin/branchName locally as branch (source)
-t --track When creating a new branch, set up upstream configuration
This can be used to "checkout" a branch we got from an upstream repo :
  1. git fetch upstream
  2. git checkout --track upstream/someBranch
mail

git clone

Usage

Clone a repository into a new directory

Flags

Flag Usage
--bare Make a bare Git repository
-b branch
--branch branch
clone the specified repository then checkout the branch branch

Example

How to clone a single branch (source) :

git clone gitUser@gitServer:projectName.git -b branch /destination/directory
mail

git config

Usage

Set Git configuration options, which can be :

Flags

Flag Usage
--get key return the value of the key configuration key
-l --list list all variables set in config file, along with their values

Exit Status

Code Condition Details
1 The specified option does not exist or is not set

Example

  • git config --list | grep configurationOption
  • git config --get user.email
  • git config --get-regexp user.*