dsaiztc
7/3/2015 - 6:14 PM

Basic Git. https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf http://mislav.uniqpath.com/2010/07/git-tips/

Configure tooling

Configure user information for all local repositories.

  • git config --global user.name "[name]" Sets the name you want atached to your commit transactions
  • git config --global user.email "[email address]" Sets the email you want atached to your commit transactions
  • git config --global color.ui auto Enables helpful colorization of command line output
  • git commit --author="Name <email>" -m "whatever"

Create repositories

  • git init [project-name] Creates a new local repository with the specified name
  • git clone [url] Downloads a project and its entire version history

Make changes

  • git status Lists all new or modified files to be commited
  • git status -sb Short status output
  • git status -u Show untracked files
  • git diff Shows file differences not yet staged
  • git diff --word-diff Diff by highlighting inline word changes instead of whole lines
  • git diff [file/folder] Shows the modifications to the specified file/folder
  • git add [file|folder|.|wildcard] Snapshots the file in preparation for versioning, wildcards allowed (*.txt)
  • git diff --staged Shows file differences between staging and the last file version
  • git reset [file] Unstages the file, but preserve its contents
  • git commit -m "[descriptive message]" Records file snapshots permanently in version history

Remote

  • git remote add origin https://github.com/user/repo.git Add a remote with name origin for the url
  • git remote -v View remotes
  • git remote rename origin destination Rename remote origin to destination
  • git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git Change URL of a remote
  • git remote rm destination Remove remote destination
  • git checkout -b test <name of remote>/test Checkout remote branch into local

Group Changes

  • git branch Lists all local branches in the current repository
  • git branch [branch-name] Creates a new branch
  • git checkout [branch-name] Switches to the specified branch and updates the working directory
  • git checkout -b [branch_name] Create a new brach and switch to it
  • git merge [branch] Combines the specified branch’s history into the current branch
  • git branch -d [branch-name] Deletes the specified branch
  • git checkout -t origin/feature Easily track a remote branch from someone else
  • git push origin --delete <branchName> Delete remote branch

Refactor Filenames

  • git rm [file] Deletes the file from the working directory and stages the deletion
  • git rm -r [folder] Delete recursively the folder and its files
  • git rm --cached [file] Removes the file from version control but preserves the file locally
  • git mv [file-original] [file-renamed] Changes the file name and prepares it for commit

Save Fragments

Shelve and restore incomplete changes.

  • git stash Temporarily stores all modified tracked files
  • git stash pop Restores the most recently stashed files
  • git stash list Lists all stashed changesets
  • git stash drop Discards the most recently stashed changeset

Review History

  • git log Lists version history for the current branch
  • git log --oneline --decorate Show branches, tags in git log
  • git log --follow [file] Lists version history for a file, including renames
  • git diff [first-branch]...[second-branch] Shows content differences between two branches
  • git show [commit] Outputs metadata and content changes of the specified commit
  • git diff --name-status Shows only the name of the changed files
  • git log --pretty=format:"%h%x09%an%x09%ad%x09%s" Show author and date
  • git show @:path/to/file > path/to/file.old Get the file from other commit (has to be forward slash /)

Redo Commits

  • git reset [commit] Undoes all commits afer [commit], preserving changes locally
  • git reset --hard [commit] Discards all history and changes back to the specified commit

Synchronize Changes

  • git fetch [bookmark] Downloads all history from the repository bookmark
  • git merge [bookmark]/[branch] Combines bookmark’s branch into current local branch
  • git push [alias] [branch] Uploads all local branch commits to the remote alias in the specified branch
  • git push -u origin master Push a branch and automatically set tracking (link between a local and remote branch; git push will by default push all branches that have the same name on the remote)
  • git pull Downloads bookmark history and incorporates changes (fetch + merge)
  • git pull --rebase Pull with rebase instead of merge

Because branch merges in git are recorded with a merge commit, they are supposed to be meaningful—for example, to indicate when a feature has been merged to a release branch. However, during a regular daily workflow where several team members sync a single branch often, the timeline gets polluted with unnecessary micro-merges on regular git pull. Rebasing ensures that the commits are always re-applied so that the history stays linear. More about rebasing vs merging here and here.