Git

Initialisation

  • Initialise git repository
    $> git init
  • Exclude files from git
    write the files and directories into the file .gitignore
    Ignore swap files: .*.sw?
  • First import
    $> git add .
    $> git commit -m "import project"
  • Prepare repository for online use
    update .git/description

    $> cd ..
    $> git clone --bare <dir>/.git <dir>.git
    $> scp -r <dir>.git <server>:/var/lib/git
  • Add remote repository to locale one
    $> git remote add origin <user>@<server>:/srv/git/<project>.git
  • Clone repository
    #> git clone <user>@<server>:/srv/git/<project>.git

Branch

  • Show branches
    $> git branch
  • Create a branch
    • Git on console
    • Github
  • Checkout new branch
    • Git on console
      $> git checkout -b <branch>

      You are already in this branch

  • Swich to branch
    $> git checkout <branch>
  • Pull last changes from branch
    $> git fetch
    
    $> git merge origin/<branch>

Stash

  • Show existing stash
    $> git stash list
  • add last changes to stash
    $> git stash
  • Pop stash to existing code
    $> git stash pop stash@{<id>}
  • Show changes
    $> git stash show -p stash@{<id>}

Diff

  • Show last changes
    $> git diff
  • Show last changes between local and remote branch
    $> git diff <masterbranch_path> <remotebranch_path>

Merge

  • Graphical merge
    $> git mergetool --tool=meld
  • Command line
    $> vim <file>
    $> git add <file>
    $> git commit
  • Abort merge
    $> git merge --abort

Migrate a directory into a new repository with history

  • Go to the reposity you want to migrate the directory
    $> git clone --no-hardlinks . <new_git_folder>
  • Go to the <new_git_folder>
    $> git filter-branch --subdirectory-filter <dir_to_migrate> HEAD
    $> git reset --hard
    $> git gc --aggressive
    $> git prune
  • Prepare repository, copy it to server and update configuration
    $> cd ..
    $> git clone --bare <new_git_folder> <new_git_folder>.git
    $> scp -r <new_git_folder>.git <server>:<git_folder>
    $> git remote rm origin
    $> git remote add origin <server>:<git_folder>/<new_git_folder>.git 
    $> git pull; git branch --set-upstream-to=origin/master master; git pull
  • Go back to the original repository and remove files and history from old repository
    $> git filter-branch --tree-filter "rm -rf <dir_to_migrate>" --prune-empty HEAD

Commands

  • Reset to HEAD
    $> git reset --hard origin/master
  • Remove last commit (not pushed)
    $> git reset --soft HEAD~1
  • Remove last commit (already pushed) (not tested)
  • Recreate master directly to origin/master
    $> git checkout -B master origin/master