On the road again

The list of useful Git commands with short description

Git checkout remote branch:
git checkout -b test origin/test
 
Clone with submodules:
git clone --recursive https://gerrit.mcp.mirantis.net/salt-models/mcp-virtual-lab
 
Clone branch:
git clone -b -- recursive <branch> <remote_repo>
 
Set user name and email (local):
 
git config --global user.name "Alex Grudev"
git config --global user.email "Ця електронна адреса захищена від спам-ботів. Вам необхідно увімкнути JavaScript, щоб побачити її."
git config --global --list
 
Change committer address for particular commit:
git config --local user.email "email.address"
git commit --amend --author="Oleksii Grudev <email.address>"
 
Check status (will also give a branch):
git status
 
Adding file to staging area (no commit):
git add start.txt
git add -A
 
Push:
git push origin master
 
Start new project (locally, will create a folder):
git init fresh-project
 
Pull:
git pull origin master
 
Fetch (non-destructive):
git fetch origin master
 
Add and commit:
commit -am "adding new file"
 
Un-stage the file ( get from staging area, not to change itself):
git reset HEAD newfile.txt
 
Discard changes (locally):
git checkout -- newfile.txt
 
Move (rename) with git (in s
git mv file1.txt file2.txt
taging area):
 
 
History:
git log
git log --oneline --graph --decorate --all
git log --since="3 days ago"
git log -- newline.txt
 
Alias:
git config --global alias.hist "log --oneline --graph --decorate"
 
Exclude files:
create .gitignore file with names of files, folders, wildcards (one per line)
 
Comparisons:
git diff (working area and staging area)
git diff HEAD (working area and local rit repo)
git diff --staged HEAD (staging area and local git repo)
git diff commit_id HEAD (between certain commit and HEAD commit in the branch)
git diff HEAD HEAD^ (commit HEAD and HEAD-1)
git diff commit_id commit_id (between certain commits)
git diff master origin/master (between local repo and remote)
 
Branches and merging:
git branch -a (view)
git branch dev (add dev branch)
git checkout dev (swith to dev branch)
git diff master dev (changes between master and dev branches) # usually before merge
git merge dev (merge dev to master, fast-forward); possible with a message: git merge dev -m "merging branch"
git merge dev --no-ff (no fast forward)
git branch -d dev (delete dev branch)
git diff master dev (comparing master and dev branches)
 
Rebasing:
git rebase master (rebase master branch to dev branch, then need to merge dev to master)
git rebase --abort (abort rebase in case of conflict)
git rebase --continue (continue rebase after resolving a conflict)
git pull --rebase origin master (rebase from remote (github))
 
Stashing (working progress):
git stash (after making changes), then git stash apply
git stash list
git stash drop (deletes unneeded stash since we already issued stash apply)
git stash -u (add stash for untracked file)
git stash pop (==git stash apply && git stash drop)
git stash save "message" (names if u have several stashes)
git stash show stash@{1) (show stash with index taken from git stash list)
git stash apply stash@{1) (apply certain stash)
git stash drop stash@{1) (drop certain stash)
git stash clear (delete all stashes)
git stash branch newbranch (move stashes to new branch)
 
Tagging:
git tag myTag (just a marker for commit, able to see in git log)
git tag --list
git show myTag (show particular commit)
git tag --delete myTag (delete tag)
 
Reset:
git reflog (log==history, reflog==full history)
git reset ID (e.g from reflog - so you can revert everything)
 
Команда git cherry-pick  позволяет забрать один коммит из другой ветки и вставить его в текущую. В качестве параметра
передаётся идентификатор коммита в другой ветке.
git cherry-pick ID
 
View history on the file:
git blame ./haproxy/files/haproxy.cfg
git show 48d3830
 
Undo last commit:
git reset --soft HEAD~1
 
Add comment