Git command summary

From Jens' Hjørne
Revision as of 11:00, 29 May 2017 by Jens (talk | contribs)
Jump to navigation Jump to search

Configuration

Editing the global configuration:

$git config --global --edit

Set git up to use Sublime Text

Create a batch/bash script that executes Sublime like this:

$subl -new-window -wait parameters to script
  • -new-window makes Sublime open a new window for the edit.
  • -wait makes sure that Sublime has focus until you close the file, this means that the script will start Sublime and wait, in stead of starting Sublime as a sub-process and then go on.

Make git call the script file (I assume it's not in your path):

$git config --global core.editor script incl. path

Cloning

Clone a GitHub project:

$git clone URL from GitHub

This will create a directory for the cloned code in the current directory, and pull all the code from GitHub and into the directory.

Branching

List branches:

$git branch

Creating a branch:

$git branch branch-name

Switch branch:

$git checkout branch-name

Remove changes

Remove changes to the current branch:

$git reset --hard HEAD

Staging

This should be done for both new and changed files

$git add file-name

Please also see the section on Committing for an alternate method when no new files have been added.

Merging

If changes has been made to the master which you want to merge into your branch:

$git merge master

Committing

Commit changes:

$git commit -m "Commit message goes here."

If you only have changed files and no new added files, you can do this without adding the changed files:

$git commit -am "Commit message goes here."

Push committed changes:

$git push

Diffing

To see a list of changes made to the files on disk:

$git diff

To see a list of changes made to the files on disk, ignoring differences in white spaces at the start and end of the line:

$git diff -w

To see a list of changes that have been staged:

$git diff --cached

Update changed code

To update code that has changed on the master branch, you need to 'stash' the changed code, pull the updated code and 'unstash' the stashed code. You may also have to switch branches in the process:

$git stash
$git checkout master
$git pull --rebase
$git checkout branch-you-came-from
$git stash apply