📜 ⬆️ ⬇️

How do i use git

Intro


I had to learn the basics of git at my first job (about three years ago).
Since then, I believed that to complete the work you need to remember only a few commands:



:



, , - .


, . , , .




, . "git_completion", .


bash


.bashrc. .


function git-current-branch {
    git branch --no-color 2> /dev/null | grep \* | colrm 1 2
}

function set_prompt_line {
    local        BLUE="\[\033[0;34m\]"

    # OPTIONAL - if you want to use any of these other colors:
    local         RED="\[\033[0;31m\]"
    local   LIGHT_RED="\[\033[1;31m\]"
    local       GREEN="\[\033[0;32m\]"
    local LIGHT_GREEN="\[\033[1;32m\]"
    local       WHITE="\[\033[1;37m\]"
    local  LIGHT_GRAY="\[\033[0;37m\]"
    # END OPTIONAL
    local     DEFAULT="\[\033[0m\]"
    export PS1="$BLUE\w $LIGHT_RED[\$(git-current-branch)]$DEFAULT \$ "
}

set_prompt_line

: PS1. Have fun.



-, , , , .. git. :


#
# Git
#
alias current-branch='git-current-branch'
alias git-uncommit='git reset --soft $(git log --format=%H -2 | tail -1)'
alias gst='git status'
alias glog='git log'
alias gcheck='git checkout'
alias gamend='git commit --amend'
__git_complete gcheck _git_checkout
alias gcom='git commit'
__git_complete gcom _git_commit
alias gdiff='git diff'
__git_complete gdiff _git_diff
alias gadd='git add'
__git_complete gadd _git_add

__git_complete <something> <another>. .



, :


- , vim!

, . git commit -m.


Git, (crontab, ) EDITOR.


(~/.bashrc) :


export EDITOR=<,    >

emacsclient, subl (Sublime Text). , , , , .


,


, . :


1)
2) git stash, , ..., , git stash pop


, ().


,


git diff

+ stage ( ). :


, stage


git diff --cached

: .



.. ,


git clean -df



alias :


alias git-uncommit='git reset --soft $(git log --format=%H -2 | tail -1)'

git reset --soft <commit/branch/tag> , . stage.


$(<whatever>) — . , cat $(ls | tail -1) ls.


git log --format=%H -2 .


, , , , , stage


upd. , : git reset --soft HEAD~ — "" "" . , , HEAD HEAD~



, , ( , ),


:


rebase!


git rebase -i master

, .
:



, , , squash.


, , , . .


-


git add <forgotten changes>
git commit --amend

:


git commit --amend --no-edit #   
git commit --amend -m 'my commit message' #  ,    

( --amend )


:


3 , , - .


:


rebase!


git rebase -i HEAD~3

( ) : , [to_squash], , (git rebase -i master) , , s (squash).



( ""). , hello_world.rb, , "create super-booper feature", hello_world.rb , . "rename variable x to y in hello_world.rb".


, , :


def kvadrat(x)
  x * x
end

puts kvadrat(n)

: n. ! , .


:


def square(x)
  x * x
end

def double(x)
  x + x
end

puts square(n)
puts double(n)

? , , , , .. . , " + ", , ?


:


git add -p

. (hunk) , : , , . , (kvadrat(x) + kub(x) => square(x) + cube(x) 2 ).


, , . , ( ? )




Outro


- "" git, .


( , ) , () : Know your tools!.


( ) , . , , : , .


')

Source: https://habr.com/ru/post/336708/


All Articles