Git

https://git-scm.com/book/ru/v2

Dealing with line endings:

# https://help.github.com/articles/dealing-with-line-endings/

git add . -u
git commit -m "Saving files before refreshing line endings"
rm .git/index
git reset
git status
git add -u
git add .gitattributes
git commit -m "Normalize all the line endings"

# git push

Custom SSH key for git command:

GIT_SSH_COMMAND="ssh -i /path/to/key" git pull

Edit global Git config file:

git config --global --edit

Credentials store

Store credentials for current git:

git config credential.helper store

# or add in ./git/config
[credential]
    helper = store

# default credentials file location
~/.git-credentials

Development scheme

https://habrahabr.ru/post/106912/

Branches:

master - permanent, stable. With tag develop - permanent feature - temporary release - temporary hotfix - temporary

Feature branches. Can start from develop. Must pull to develop.:

# Start new feature
git checkout -b myfeature develop

# Do changes, commit to myfeature

# End new feature
git checkout develop        # change branch
git merge --no-ff myfeature # merge branches
git branch -d myfeature     # delete feature branch
git push origin develop     # push changes to develop

Release branches. Can start from develop. Must pull to develop and master. Can be named alike release-*.:

# Start release
git checkout -b release-1.2 develop
./bump-version.sh 1.2                           # some changes in project
git commit -a -m "Bumped version number to 1.2"

# End release
git checkout master             # change branch
git merge --no-ff release-1.2   # merge branches
git tag -a 1.2                  # set tag

# Keep actual develop branch
git checkout develop
git merge --no-ff release-1.2

# Delete release branch
git branch -d release-1.2

Hotfix branches. Can start from master. Must pull to develop and master. Can be named alike hotfix-*.:

# Start hotfix
git checkout -b hotfix-1.2.1 master
./bump-version.sh 1.2.1             # some fixes in project
git commit -a -m "Bumped version number to 1.2.1"

git commit -m "Fixed severe production problem"

# End hotfix
git checkout master
git merge --no-ff hotfix-1.2.1
git tag -a 1.2.1

# Keep actual develop branch
git checkout develop
git merge --no-ff hotfix-1.2.1

# Delete hotfix branch
git branch -d hotfix-1.2.1

Git tips and tricks

See all of the changes from latest pull:

git diff master@{1} master

Push current changes to new branch (auto create it on remote and local):

# done some changes
git checkout -b <new-branch>
git add .
git commit -m "added feature"
git push origin <new-branch>

Avoid Are you sure you want to continue connecting (yes/no) on connect via ssh keys:

# bitbucket
ssh-keyscan -t rsa bitbucket.org >> ~/.ssh/known_hosts

Delete credentials from GIT repository [rus] - https://the-bosha.ru/2016/07/01/udaliaem-sluchaino-zakomichennye-privatnye-dannye-iz-git-repozitoriia/

Force “git pull” to overwrite local files:

# Downloads the latest from remote without trying to merge or rebase anything
git fetch --all

# Resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master
git reset --hard origin/master

# OR If you are on some other branch
git reset --hard origin/<branch_name>

# Maintain current local commits by creating a branch from master before resetting
git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master
git push origin serverfix               # remote server/branch
git push origin serverfix:awesomebranch     # local branch serverfix will transfer to branch awesomebranch of remote project

git remote      # Show configured remote servers
                -v  # Show match URL/name
    add <name>

Clone only single branch:

git clone -b <branch> --single-branch git://sub.domain.com/repo.git

Change url:

git remote set-url origin git@bitbucket.org:johnsmith/test.git
git remote add origin https://github.com/johnsmith/test.git
git push origin --delete serverfix  # delete branch on server

git fetch [имя удал. сервера]       # pull from server, but not merge with local changes

git branch                          # show all branches
                -v                      # also show last commits on branches
                --merged/--no-merged    # show merged/unmerged branches with current

git branch testing      # new branch, without switching
                   -d hotfix    # delete unused branch
                   -D testing   # delete unused branch also if not merged

git checkout testing        # switching to branch testing (HEAD transfer to branch testing)
                         -b iss53       # create and switching branch
git checkout path/to/file   # switching file version from server

git merge hotfix        # merge current branch with hotfix

Shows commit history and branch indexes and branch history:

git log --oneline --decorate --graph --all

HEAD indicates on current branch

Command aliases:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'