Comandi Git

//initialization

cd devdir
git init .
git add <filename>
<edit/copy/modify>

git commit -a
git commit -a -m "comment"

git revert <commit#>

git log
git log --oneline --graph

git diff

gitk
gitk --all

//tags

git tag tagname <commit#>
git tag -d tagname
git tag -f tagname     // to forcely move tag
git tag

//branch management

git checkout to-branch
git checkout -b new-branch
git merge from-branch

git branch
git branch -r

git branch it-i18n origin/master

git checkout it-i18n

//clean up

git gc
git fsck

// configure from remote

git clone git://..../....git

git fetch

git checkout it-i18n
git rebase origin/master

// merge commits

git rebase -i HEAD~5 // for latest 5, for example
// in the editor which appears, pick first commit and then squash all the other ones
// save&close, then new editor appears with all comments to be merged



// or for remote squash 
git rebase -i origin/master
git pull --rebase
git push git@gitorious.org:~catanzag/qt-creator/qt-creator-catanzag.git it-i18n

// if unstaged changed/dirty tree => stash "park" changes, then rebase
// the pop changes again

git stash
git rebase origin/master
git stash apply


// if something goes wrong, eventually fix and
git rebase --abort   // or 
git rebase --continue

// see also https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git

// to modify last commit history

git commit --amend          // for latest

// to modify history, commit order, sqashing...

git rebase -i HEAD~5        // for latest 5, for example, then choice
// pick (use commit), edit (amend) or squash (merge w prev & amend),
// fixup (merge with prev & prev comment) and then
git rebase --amend         // to correct history or
git rebase --continue      // when finished


// cherry pick commands

git cherry-pick <commit#>
git cherry-pick -n <commit#> // no commit, only merge
git cherry-pick --abort 
git cherry-pick --continue 

// undo last commit (soft)

git reset -soft HEAD~1 <files> //leaves staged changes, then

// undo last staging

git restore --staged <files> //to unstage erroneoulsy added changes

// undo last commit (hard)

git reset -hard HEAD <files> //removing files too

// undo last changes

git checkout -- <files>    //or
git checkout -- .          // all working dir

// rm files

git rm <files/DIR>   // untrack & DELETE
                     // rm files under directory & tracking (default behaviour )
                     

// ignore (untrack) already commited directory contents without deleting them
// https://stackoverflow.com/questions/1329291/ignoring-an-already-checked-in-directorys-contents

git rm --cached <files/DIR>  
// untrack LEAVING THE FILE
                            // The --cached option causes the files to only be
                            // removed from git's index, not your working copy
                            // add -r to really delete files under directory
git rm -r                    // recursively


// Local Repository COMMANDS

// REPO directory
git init --bare reponame.git //create empty repo
// WORK directory
git init .
git config [--global] push.default simple
git remote add origin <relative or absolute path>
// if empty
git push --set-upstream origin master // only first time
git push [master]                     // all other times
// if already filled
git pull origin master // only first time
git pull               // all other times

// Recovering from detached HEAD state

// (e.g. after a checkout from a specific SHA-1 commit hash & modify/perform commit
// https://circleci.com/blog/git-detached-head-state/

git checkout -b temp-branch // create a new branch
git commit                 // if eventually there are changes to be saved
git checkout master         // back to main branch
git merge temp-branch
git checkout -m “after merging (old detached) temp-branch"

// revert HEAD state (use on remote/public branches)

git revert HEAD      // w autocomment

// reset HEAD state (use on private branches)

HARD // reset HEAD and DELETE tracked files -- leaves files untracked in work dir

git reset -hard HEAD^      // one commit before HEAD
git reset -hard HEAD~1     // as above
git reset -hard HEAD~2     // two commits before HEAD as example

// UNDO HARD reset

git reset --hard HEAD@{1}  // do it not too late, garbage collection will
                                 // remove the commit

MIXED // reset HEAD and staging area (index) KEEPING files

git reset -mixed HEAD~2     // two commits before HEAD as example

SOFT // reset HEAD, KEEPING staging area (index) and files

git reset -soft HEAD~2     // two commits before HEAD as example; in this
                                 // case, the 2 commits can be commited as only one


// plumbing (under-the-hood) commands

git cat-file -p HEAD      // 
git ls-tree -r HEAD       // 

git ls-files -s     // behind the scenes of what your index looks like



Git as a system manages and manipulates three trees in its normal operation:

TreeRole

HEAD

Last commit snapshot, next parent

Index

Proposed next commit snapshot

(Files are marked to commit)

Working Directory

Sandbox




*ESC[33m*commit 0a02124a0fd85c1f0094bcdf15bac1645e3c8630ESC[m

unset LESS                             #or
export LESS=-R                         #or
git config --global core.pager "less -R"

Nessun commento: