Git

作者: yaohwang | 来源:发表于2016-09-04 01:01 被阅读14次

    Git

    Local

    Install Git

    # install
    sudo apt-get install git
    
    # important config
    # --global means it's working for current user (~/.gitconfig)
    # without --global means for current repository (.git/config)
    git config --global user.name "myname"
    git config --global user.email "myemail"
    # other config
    git conifg --global color.ui true
    git config --global alias alias_name raw_name
    

    Basic Skills

    # make directory
    mkdir dir
    cd dir
    
    # initialize git repository
    git init
    
    # add files which ready to commit
    git add file1
    git add file2
    # even the file3 is in .gitignore
    git add -f file3
    ...
    
    # delete files
    rm file1
    git rm file1
    # introduce later, recovery file1
    # git checkout -- file1
    
    # commit changes
    git commit -m "msg"
    
    # branches
    # create branch
    git branch bra1
    # switch to new branch ${bra1}
    git checkout bra1
    # create and switch
    git checkout -b bra1
    # merge branches
    # merge specified branch with current branch
    git merge bra1
    git merge --no-ff -m "msg" bra1
    # delete branch
    git branch -d bra1
    git branch -D feature-name
    
    # stash
    git stash
    # recovery
    git stash apply
    git stash apply stash@{num}
    # drop
    git stash drop
    # recovery and drop
    git stash pop
    
    # generate .gitignore and put the files or directories which you don't want track to it.
    

    Check

    # difference
    git diff file1
    git diff HEAD -- readme.txt
    
    # add and commit status
    git status
    
    # log
    # first commit to current commit
    git log
    git log --pretty=oneline
    git log --graph --pretty=oneline --abbrev-commit
    git log -1
    
    # all commit
    git reflog
    
    # check status of current branches
    git branch
    git branch -a
    git branch -vv
    
    # check stash
    git stash list
    
    # check remote repository
    git remote
    git remote -v
    
    # check .gitignore
    # find the rule why file3 has been ignored
    git check-ignore -v file3
    

    Time Travel

    # commitid fineness
    git reset --hard HEAD^
    git reset --hard HEAD^^
    git reset --hard HEAD~100
    git reset --hard commitid
    
    # variant fineness
    # file will change: discard changes after git add
    # file will change: discard changes after git commit
    git checkout -- file1
    
    # variant fineness
    # file don't change: discard git add
    git reset HEAD file1
    

    Remote

    Link

    # use ssh to communicate
    # generate id_rsa and id_rsa.pub in ~/.ssh/
    ssh-keygen -t rsa -C "myeami"
    
    # add id_rsa.pub to SSH KEYS of remote servers
    # remote server: GitHub, gitoschina etc.
    

    Basic Skills

    # fetch remote repository information
    git fetch
    # merge local branch with remote branch
    git merge
    git merge origin/remote_branch_name
    git merge @{upstream}
    git merge @{u}
    
    # fetch and merge
    git pull
    
    # create remote branch the first time
    # push local branch to the remote branch
    git push origin remote_branch_name
    # push specified local tag to the remote server
    git push origin local_tag_name
    # push all local tag (which havn't been pushed) to the remote server
    git push origin --tags
    
    # make tag (the name of commitid)
    git tag tag_name
    git tag tag_name commitid
    git tag -a tag_name -m "msg" commitid
    # use -s (instead of -a) to generate PGP signature, gpg has been needed.
    git tag -s tag_name -m "msg" commitid
    # delete local tag
    git tag -d v0.1
    # delete remote tag
    # delete local tag first
    git push origin :refs/tags/tag_name
    

    Check

    # show all tag names
    git tag
    
    # show one specific tag information
    git show tag_name
    

    Sync

    # both local and remote repository already exists
    
    # create new repository on remote server
    
    # associate local repository with remote repository
    # Git remote repository name: origin (default name)
    git remote add origin remote.git.address
    
    # push current branch (default master) to remote branch
    # -u : used at the first time to associate local master branch with remote master branch.
    git push -u origin master
    git push origin master
    git push origin remote_branch_name
    

    Clone

    # local repository doesn't exists
    
    # clone repository from remote server
    # create local directory itself
    # can only see master branch
    git clone remote.git.address
    
    # get other branch
    # create new remote branch
    git push origin remote_branch_name
    # create local branch and link it to remote branch
    git branch local_branch_name
    git branch -u origin/remote_branch_name
    git branch --set-upstream local_branch_name origin/remote_branch_name
    # remote branch already exists
    git checkout -b other_branch origin/other_branch
    

    Usage

    • master: only do release
    • dev: everyone commit to dev, dev commit to master.
    • feature-name: new feature, experiment.
    • bug:
    • everyone:

    GitHub

    • Fork other people's project
    • clone from self-repository on GitHub
    • pull request on self-repository of GitHub, waiting for raw project manager to accept.

    Git server

    • install: sudo apt-get install git

    • add user: sudo adduser git

    • certificate: collect id_rsa.pub and put them into file /home/git/.ssh/authorized_keys

    • init git repository: do sudo git init --bare repository_name.git under /dirname/. (No working directory in it, because it's just for share, no one should login this server and change it.)

    • change owner: sudo chown -R git:git repository_name.git

    • forbiden shell: change /etc/passwd, change git:x:1001:1001:,,,:/home/git:bin/bash into git:x:1001:1001:,,,:/home/git:/user/bin/git-shell.
      (then we can use git by ssh, but we can't use shell.)

    • clone: git clone git@server:/dirname/repository_name.git

    • push etc.

    • Gitosis to manage id_rsa.pub

    • Gitolite to manage privilege

    相关文章

      网友评论

          本文标题:Git

          本文链接:https://www.haomeiwen.com/subject/mcrlettx.html