美文网首页
git使用归纳(待发布)

git使用归纳(待发布)

作者: ShanksWang | 来源:发表于2018-12-18 13:32 被阅读29次

    Git

    初始化git本地仓库:git init
    查看working directory和staging area的状态:git status
    添加文件到staging area:git add <file> [...]  // file means document and folder. "..." means containing lots of parameters.
    检查不同: 
    git diff  // differ working directory and staging area
    git diff HEAD  // differ working directory and HEAD which is the latest commit
    git diff --cached // differ HEAD and staging area
    git diff --staged  // differ HEAD and staging area
    添加提交:git commit -m "commit message"  // should replace "commit message" whatever you want 
    修改提交:
    git add <forgotten_file>  // after commit, then forgeting some files didn't add, or commit information write wrong
    git commit --amend 
    查看log:git log  // show some logs
    查看log:git log --oneline --decorate --graph --all  // show log
    
    
    取消working directory的修改/改变:git checkout HEAD <file> [...]  // discards changes in the working directory
    取消staging area的修改/改变:git reset HEAD <file> [...]  // unstages file changes in the staging area
    
    git reset SHA  // can be used to reset to a previous commit in your commit history
    
    
    /* In Git, the commit you are currently on is known as the 'HEAD' commit. In many cases, the most recently made commit is the 'HEAD' commit. */
    
    git show HEAD // show the HEAD commmit
    git checkout HEAD filename // discards changes in the working dirct
    git reset HEAD filename // unstage file changes in the staging area
    git reset commit_SHA // reset to a previous commit in your history
    
    

    Branch

    查看分支:git branch
    
    查看更多分支:git branch -a [-r]
    
    创建分支:git branch <branch_name>
    
    切换分支:git checkout <branch_name>
    
    创建+切换分支:git checkout -b <branch_name>
    
    重命名分支:git branch -m <new_branch_name> // in current branch
    重命名分支:git branch -m <old_branch_name> <new_branch_name>
    
    合并某分支到当前分支:git merge <branch_name>
    
    修改提交信息:git commit --amend  // might enter text editor mode
    
    // even if "master" branch doesn't have all commits from the branch_name
    强制删除分支:git branch -D <branch_name>  // better check commits
    
    删除分支:git branch -d <branch_name>  // try this first before replace '-d' with '-D'
    
    查看上游:git remote -v
    添加远程地址:git remote add origin <remote_url>  // add the 'origin' remote's URL
    修改远程地址:git remote set-url origin <remote_url>  // set the 'origin' remote's URL,  or you can just edit .git/config and change the URLs there
    移除远程地址: git remote rm <remote_name>  // delete remote repo reference; e.g. delete origin remote: git remote rm origin, more details check [here](https://help.github.com/articles/removing-a-remote/)
    git remote rename <old_name> <new_name>  // rename repo
    删除远程/上游分支:git remote rm origin <branch_name>
    
    拉取并合并:git pull origin <branch_name>
    
    // check which branch have merged to branch of where i am
    git branch --merged
    git branch --no-merged
    
    // delete remote branch
    git push origin --delete branch_name
    
    // merge remote branch to current local branch
    git merge origin/branch
    
    // create new local branch from remote
    git checkout -b new_branch origin/branch_name
    
    

    Git with remote repo

    // Creates a local copy of a remote.
    git clone <url>
    
    // Lists a Git project's remotes,see more detail.
    git remote -v
    
    // but not influence your local code.
    git fetch  // fetch the latest commit from remote repo, but not influence your local code. if you "git branch -r", you
    
    // Merges origin/master into your local branch, there are two or three model
    // 1. Fast forward; 2. Merge made by recursive [3. have conflict]
    git merge origin/master
    or ['git merge origin/branch_name <local_branch>']
    
    // pulls the latest origin remote to local branch.
    // 'git fetch' + 'git merge'
    git pull origin <branch_name>
    
    // push 到同名的branch上
    git push -u origin master 
    
    
    

    Tag

    为什么需要打Tag,每次commit的时候,对应的message描述的应该是,实现什么功能,修改什么bugs等;有时候我们需要通过打Tag,或者叫打标签的方法,记录下,哪个commit是某个模块/这个项目/这个产品的版本号和相关信息。注:一个项目可能包含多个模块,对吧。打tag不会影响到commit。实际上相当于在某个commit上贴了个标签,详情可以看这个项目

    git tag  // show  all tags
    git tag -l 'v1.8.5*'  // show specified mode and look for tag, eg, interested in 1.8.5
    
    // create two type tag, lightwight and annotated
    // recommand you use annotated tag for GNU Privacy Guard
    git tag -a v1.4 -m "my version 1.4"
    git tag v1.4-lw  // no '-m' and '-a' choice
    
    // show v1.4 information
    git show v1.4   //  annotated type tag
    git show v1.4-lw   // lightwight type tag
    git log --pretty=oneline
    git tag -a v1.2 9fcebo23di[this is a commit sha value]  // tag later for some commit
    git tag --delete tag_name  // delete local tag
    git push origin [tagname]  // share tag by pushing tag into remote repo
    git push origin --tags  // push one more tags
    
    # git push remote_name --delete branch_name
    git push origin --delete feature/login  // delete remote brach
    
    // if you wanna check this tag,
    // if you wanna work directory and specified tag of repo
    git checkout -b version2 v2.0.0   // based on this tag 'v2.0.0', jump into new branch
    

    Stash

    git status  // check status
    git stash save  // stash this status
    git status  // check status, again
    git stash list  // show all stash item
    git stash apply  //  back to original state, there are only no commit files, not staged will be auto staged;
    // back to original state, there are not staged, files and no commit files; Better use this command;
    git stash apply --index 
    git stash apply stash@{2} --index   //specified stash item
    git status
    git stash drop stash@{number}  // remove specified stash item
    git stash pop  // remove the last stash item
    git stash branch new_branch  // 
    

    Alias

    git config --global alias.co checkout
                        alias.br branch
                        alias.st status
                        alias.ci commit
                        alias.unstage 'reset HEAD --'  // unstage
                        alias.unchange 'checkout --'  // unmodified work directory
                        alias.last 'log -1 HEAD'   // the last commit
    

    Create a remote repo from github

    echo "# hellotoday" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git remote add origin git@github.com:shanksiscool/hellotoday.git  // ssh mode, your url might be https://www.github.com/shanksiscool/hellotoday.git
    git push -u origin master
    

    After modified .gitignore file

    git rm -r --cached .  // remove everything from the *index*
    git rm --cached filename  // remove one filename from the *index*
    git add .  // add all files to staging area
    git commit -m ".gitignore is now working" // make a commit
    

    相关文章

      网友评论

          本文标题:git使用归纳(待发布)

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