Skip to content

Latest commit

 

History

History
461 lines (308 loc) · 6.42 KB

git.md

File metadata and controls

461 lines (308 loc) · 6.42 KB

Git

local command

Add

  • add file
git add [file name]
  • add all
git add -A
  • or add all file in this folder
git add .

Commit

  • save addlist to history
git commit -m"[descriptive message]"

Log

  • see the history of commits
git log
  • its less command in linux
    • q ->quite
    • /[search] and enter, n to next, N to Previous

Status

  • current status
git status
  • can send argument, can send two or more
    • oneline for see every commit in one line
    • decorate show all the reference
    • graph display ascii graph for branch structure
    • p patch, show the change of every commit
    • stat show the number of change of files
  • can send option
    • number to show the last-number, 2 for last 2 commit
    • after or since for after certain time, after="yesterday", 30 minute ago, last tuesday, last week, 2 week ago, 3-15-16, 3/15/16
    • before or until for commit before certain time, before="yesterday"
    • author author name, author="mohsen" all commit with author and email contain mohsen
    • grep search in commit message
    • S search for string when add or remove in code, S"mat", better use with p arguman to see where used
    • G search with regex for change in code dont need comma,-GMath|Random -> search for Math or Random
    • i ignore case sensitive
    • no-merges see without merge commit
    • .. commit with range of references [ref1]..[ref2]
    • [file name] the commit involve that files
git log --oneline --graph

git log -p -i -S"mat"

git log -3 -i --author="mohsen" README.md

git log -S"Math" --after="2 months ago" --online --stat

diff

  • use without argument or name show un-commit change
git diff
  • cashed show difference between working directory and stashed
git diff --cashed
  • can send branch name to see diff
git diff [branch name]
  • can send file name
git diff origin/master README.md

blame

  • who modify the file and when
git blame [file name]

reset current change

git reset --hard [name]

stash

  • save temporary the change added to history and clean the status
git stash
  • unstash or come back the stash change
git stash apply

remote access

clone

git clone http://github.com/mohsen12999/mycheat

add remote

git remote add origin [email protected]:fhfh/fgfg.git

see remotes

git remote -v

pull, push

  • get the history from remote
git pull
  • its equal to
git fetch && git merge
  • send change to remote
git push

Branch

  • add branch
git branch [new branch name]
  • see all branch
git branch
  • go to branch
git checkout [branch name]
  • make and go to new branch
git checkout -b [new branch name]
  • go to Previous branch
git checkout -
  • merge branch to this branch [add feature of other branch to this branch]
git merge [branch name]
  • delete branch
git checkout -d [branch name]

rebase

git rebase [branch name]
  • use i for interactive mode
git rebase -i [branch name]
  • combine all commit to one commit -> hold first to pick and change other to squash or only s, after save and exit you can add one commit message for all of that
  • after this change must push with f or force flag
git push -f

conflict

< < < HEAD
our change
= = =
change come from server
> > >

other

binary search

  • for start
git bisect
  • mark this commit as bad commit
git bisect bad
  • mark the good commit
git bisect good [commit id]
  • git start searching for bad commit, answering them:
git bisect good
git bisect bad
  • exit from bisect search
git bisect reset

tag

  • add tag
git tag v1.0.0
  • see all tag
git tag
  • push tag to remote
git push --tags
  • annotate comment for tag with a, if not use m it open in default editor
git tag -a v2.0.0 -m "tag message"

save password

git config credential.helper store

config ssh

  • generate key
ssh-keygen -t rsa -b 4096 -C [email protected]
  • add to ssh program
ssh-agent -s
  • add rsa to ssh
ssh-add ~/.ssh/id_rsa
  • copy rsa key to clip board
clip <~/.ssh/id_rsa
  • go to github -> profile -> edit profile -> ssh keys -> add ssh key -> title: like the name of the computer or machine, past the key
  • verify the key

git hook

  • in .git/hooks folder, there is some sample files, for run you must make file with out sample
  • for example in file .git/hooks/pre-commit, run test and lint before test
#!/bin/bash
npm test && npm run lint
  • after that make file executable
chmod +x .git/hooks/pre-commit
  • if one of them failed, we can not commit
  • this setting just use locally and cant send to remote repository

Global Setting

  • save in .gitconfig file in home directory
git config --global user.name 'mohsen shabanian'
  • config default editor
git config --global core.editor vim
  • add alias
git config --global alias.graph 'log --graph --oneline'

-- see all config list

git config list

ignore file

  • make .gitignore file and file name like .DS_Store for mac or Thumbs.db for windows
  • for remove tracked file that ignore but added before:
git rm --cached [filename]
  • we can make .gitignore_global in home directory for ignore file in all the place and define it in .gitconfig
git config --global core.excludesfile ~/.gitignore_global

Git workflows

git flow

  • git flow is great for a release-based software workflow.

  • Getting Started instead of git init

git flow init
  • Creating a feature branch
git flow feature start feature_branch
  • Finishing a feature branch
git flow feature finish feature_branch
  • Release Branches
git flow release start 0.1.0
  • Finishing a Release Branches
git flow release finish '0.1.0'
  • Hotfix Branches
git flow hotfix start hotfix_branch
  • Finishing a Hotfix Branches
git flow hotfix finish hotfix_branch

Reference

  • Egghead: practical-git-for-everyday-professional-use