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
Set up custom SSH key for specific git project:
# clone new project
GIT_SSH_COMMAND="ssh -i /path/to/ssh_key" git clone git@*****/project.git
# go to project dir
cd project
# set project-specific option
git config core.sshCommand "ssh -i /path/to/ssh_key -F /dev/null"
# you can see project config here .git/config
# nice to set right name and email for specific project
git config user.name "User Name"
git config user.email "email"
Edit global/local Git config file:
git config --global --edit
git config -e
# list params
git config [--global] -l
# set params
git config [--global] user.name "User Name"
git config [--global] user.email "email"
# get param
git config [--global] user.name
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
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
Add remote repository:
git remote add origin https://github.com/johnsmith/test.git
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
git add <file_name> # add file to git index (stage changes)
. # stage all changes (all files)
git push origin --delete serverfix # delete branch on server
git fetch [имя удал. сервера] # pull from server, but not merge with local changes
git branch # show all local branches
-v # also show last commits on branches
--merged/--no-merged # show merged/unmerged branches with current
-a # show also remote branches
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 newbranch # create and switching branch
COMMIT_HASH # switch to specific commit
git checkout path/to/file # switching file version from server
git merge hotfix # merge hotfix into current branch
Git log:
# Shows commit history and branch indexes and branch history
git log --oneline --decorate --graph --all
# get commit history
git log
git log --pretty=oneline
git log --pretty=oneline --author="Valentin Siryk"
# formatting
git log --pretty=format:"%h - %s : %ad %an"
Git stash:
# Stash unstaged local changes (changes which were not added to index by 'git add')
git stash
git stash list # show all stashed changes
git stash pop # unstash last changes and drop these from stash
git stash apply # unstash last changes (without dropping from stash)
git stash show # show last changes from stash
git stash drop # drop last stashed changes from stash
git stash clear # drop all stashed changes
Git reset:
git reset [--mixed] COMMIT_HASH # reset to specific commit. Changes will be removed from index.
git reset --soft COMMIT_HASH # reset to specific commit. Changes will be ready to commit - staged state.
git reset --hard COMMIT_HASH # reset to specific commit. You will lose all changes!
Delete commits from remote server:
# Make sure that nobody could not pull commits before deleting!
git reset COMMIT_HASH # remove all commits after COMMIT_HASH locally
git push -f # force push commit history to remote server
Git revert:
# Creates commit to revert commit
git revert HEAD # create commit to revert last commit
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 --'