Git common commands in a detailed
Git is a very powerful distributed version control system. It is not only suitable for managing the source code of large open source software, but also has many advantages in managing private documents and source code.
1.Git file operation
$ git help [command] # Show command help$ git show [$id] # Show content of a commit $ git checkout [f Ile] # Discard workspace Modify $ git checkout. # Discard Workspace Modify the git add [file] # to commit the working file modification to the local staging area $ git Add. # Commit all modified work files staging area $ git rm [file] # Delete files from the repository $ git RM--cached [file] # Delete files from the repository, but do not delete the text Piece $ git reset [file] # Recover from staging area to working file $ git reset--. # Recover from Staging area to working file $ git Reset--mixed head~1 # Modify Repository, modify staging area, keep workspace $ git reset--soft head~2 # Modify the repository, keep the staging area, Leave workspace $ git reset--hard head~3 # Modify Repository, modify staging area, modify workspace $ git commit--amend # Modify last commit record $ git reve RT [$id] # restores the state of a commit, the recovery action itself also creates a commit object $ git revert HEAD # restores the last committed state $ git status # View current workspace status $ git config--list # See all Users $ git ls-files # view already File submitted $ gIt commit-a # commits all changes to the current repos $ git commit-v # parameter-V to see the difference in commit $ git commit -M "Message" # Submit to local and add commit information $ git commit-am "init" # Add and submit $ git log [file] # View the file per commit record $ git log-p [file] # view diff$ git log-p-2 # View the most recent two changes in detail diff$ git log--stat # View commit statistics $ git rm--cached [file] # Delete files from Staging area $ git rm-f [file] # forcibly remove version-controlled files $ git rm-r--cached [file] # recursive Delete-R, is a folder when useful $ git diff [file] # Compare the current file and the staging area file difference $ git diff [$id] [$id] # Compare the differences between two commits $ git diff [Branch1]. [BRANCH2] # Compare $ git diff--staged # between two branches compare staging area and repository diff $ git diff--cached # compare staging Zone and Workspace diff $ git diff--stat # Just compare statistics $ git stash # staging work currently in progress $ git stash push # will be staged to push to a temporary space in $ git stash liSt # view all cached code $ git stash Clear # Empty cache contents $ git Stash Drop # Remove a store $ git stash save # specify message$ git stash show # view the diff$ git stash a specified stash pply [Email protected]{1} # takes out the specified cache code $ git Stash Pop # will file pop down from scratch space $ git fetch Origi N Dev:dev # Get Latest version to local does not automatically merge$ Git pull Origin master:master # Get Latest version to local auto merge
GIT branch Operations related commands
$ git Branch # View branch $ git branch [dev] [master] # Create dev branch in master git branch-v # View each branch last commit info $ git branch-r # View remote Branch information $ git branch-a # View all branches Info $ git branch-m [AAA] [BBB] # rename AAA to bbb$ git branch [name] # Create branch from current branch $ git branch-d [BR Anch] # Delete a branch $ git branch-d [branch] # Force Delete a branch $ git branch--set-upstream-to origin/test mast ER # Local Branch and Remote Branch Association $ git Branch--set-upstream-to=origin/master help #$ git branch--merged # View branches that have been merged into the current branch $ git Branch--no-merged # View the branch that has not been merged into the current branch $ git commit [$id]-B [New_branch] # from history commit record Create branch $ git commit [$i D] # from history commit record checkout out but no branching information, switch to other branches will automatically delete $ git checkout [name] # Toggle Branch $ git checkout-- Track Origin/dev # switch to remote Dev branch $ git checkout-b [name] # New from current branch and switch to name$ git checkout-b [new_br] [BR] # Create a new NEW_BR based on branchanch$ git merge Origin/dev # merge branch dev with current branch $ git merge [branch] # Merge branch branch into current branch $ git m Erge Origin/master--no-ff # do not fast-foward merge so you can generate a merge commit $ git rebase master [branch] # will master rebase to Branc H, equivalent to: Git clone [branch] && git rebase master && git clone master && git merge [branch]
GIT Remote branch Management
$ git Pull # Crawl all branches of remote repository update and merge to local $ git pull--no-ff # crawl remote repository All branch updates and merge to local, not To fast-forward merge $ Git fetch origin # Crawl remote repository update $ git merge origin/master # merge remote Master branch into local current branch $ git clon E--track origin/branch # Track a remote branch to create the appropriate local branch $ git clone-b [l_b] origin/[r_b] # Create local branch based on remote branch, function as above $ git push # push all branches $ Git push Origin master # to push a locally specified branch to the remote main branch $ GIT push-u Origin Master # Push the local landlord branch to remote (if no remote main branch is created, used to initialize the remote repository) $ git clone [[[Email protected]] # clone remote repository $ git remote-v # View remote server address and warehouse name $ git remote show [name] # View Remote server warehouse status $ git remote add [name] [url] # Add remote Warehouse address $ git remote RM [repository] # Delete remote repository $ git remote set-url--push [name] [newurl]# Modify remote repository $ git pull [Rname] [lName] # FETCH remote branch $ git push [rname] [lName] # Push remote branch $ GIT push origin [Lname]:[rnam E] # Local branch push to remote $git push origin-d [name] # Remove Remote branch-D can also use--delete$ git remote Set-head Origin master # to set the remote repository head to point to master Branch $ git Branch--set-upstream Master origin/master
Git version fallback operations related commands
HEAD :当前版本HEAD^ :上一个版本$ git log # 查看commit的信息$ git log --pretty=oneline # 单行展示历史记录$ git log --oneline # 单行简单展示$ git reflog # 查看命令历史记录 $ git checkout . # 撤销所有本地改动代码$ git checkout [file] # 撤销所有本地改动代码$ git reset HEAD . # 撤销所有add文件 $ git reset HEAD [file] # 撤销单个add文件$ git reset --soft HEAD # 只回退commit的信息,保留修改代码$ git reset --hard HEAD^ # 彻底回退到上次commit版本,不保留修改代码$ git revert # 以前commit的id$ git reset --hard [branch] # 本地代码回退到与git远程仓库保持一致--hard 参数会抛弃当前工作区的修改--soft 参数的话会回退到之前的版本,但是保留当前工作区的修改,可以重新提交
git tag Operations related commands
$ git tag # 查看标签$ git tag [name] # 创建版本$ git tag -d [name] # 删除版本$ git tag -r # 查看远程版本$ git push origin [name] # 创建远程版本(本地版本push到远程)$ git push origin :refs/tags/[name] # 删除远程版本$ git pull origin --tags # 合并远程仓库的tag到本地$ git push origin --tags # 上传本地tag到远程仓库$ git tag -a [name] -m [message] # 创建带注释的tag
Git submodule (submodule) Related Operations commands
$ git submodule add [url] [path] # 添加子模块$ git submodule init # 初始化子模块,只在首次检出仓库时运行一次就行$ git submodule update # 更新子模块 每次更新或切换分支后都需要运行一下删除子模块:分4步1) $ git rm --cached [path]2) 编辑“.gitmodules”文件,将子模块的相关配置节点删除掉3) 编辑“ .git/config”文件,将子模块的相关配置节点删除掉4) 手动删除子模块残留的目录
git Patch Management
git diff ] ../sync.patch # 生成补丁git apply ../sync.patch # 打补丁git apply --check ../sync.patch #测试补丁能否成功
Git ignores some files, folders do not commit
Create a file with a name of ". Gitignore" under the repository root and write an unwanted folder name or file with one line per element, such as
target*.class
Some of the common commands summarized in the work will continue to be supplemented in the future, if there is a missing welcome supplement.
git command Daquan (full version)