Git常用操作指南

By Long Luo

CONFIGURING GIT

git config –global user.name “[name]” Set the name that will be associated with your commits

git config -global user.email “[email]” Set the email that will be associated with your commits

git config -global alias.[alias] [command] Create a shortcut for a Git command (e.g. alias.glog “log —graph —oneline”)

git config -global core.editor [editor] Set the default text editor to use for commit messages (e.g. vi)

git config -global —edit Open the global config file in a text editor for manual editing

INITIALIZING AND CLONING

git init Initialize an empty Git repository in the current directory

git init [directory] Create an empty Git repo in the specified directory

git clone [url] Clone a remote Git repository from the url into a local directory

git clone [url] [directory] Clone a remote repo into the specified local directory

EXAMINING LOGS

git log Show the commit history for the current branch

git log -p Show the diffs from each commit in the commit history

git log —stat Show stats (files changed, insertions,deletions) for each commit

git log —oneline Show condensed summary of commits in one line each

git log —graph —decorate Draw a text based graph of commits with branch names

git diff Show unstaged file differences compared to current index

git diff -cached Show differences between staged changes and the last commit

git diff [commitl] [commit2] Show changes between two commits

git show [commit] Show changes made in the specified commit

VERSIONING FILES

git add [file] Stage file changes to be committed

git commit -m “[message]” Commit the staged snapshot with commit message

git rm [file] Remove file from staging index and working directory

git mv [file] [newpath] Move or rename file in Git and stage the change

BRANCHING AND MERGING

git branch List all the branches in the current repository

git branch [branch] Create a new branch with name [branch]

git checkout [branch] Switch the current branch to [branch]

git checkout -b [branch] Create a new branch and switch to it

git merge [branch] Merge the history of [branch] into the current branch

git branch -d [branch] Delete the local branch [branch]

RETRIEVING AND UPDATING REPOSITORIES

git fetch [remote] Fetch branches and commits from the remote repository

git pull [remote] Fetch remote changes and directly merge into local repository

git pull —rebase [remote] Fetch remote changes and rebase onto local branch

git push [remote] [branch] Push local branch to remote repository

git push –all [remote] Push all local branches to remote

git push —tags [remote] Push all local tags to remote repository

REWRITING GIT HISTORY

git rebase [branch] Rebase current branch onto [branch]

git rebase -i [commit] Interactively rebase current branch onto [commit]

git reflog Show history of Git commands for current repository

git reset —hard [commit] clear staging area, rewrite working tree from specified commit

REMOTE REPOSITORIES

git remote add [name] [url] Create remote connection with url and alias [name]

git fetch [remote] Fetch all branches from remote repository

git pull [remote] Fetch remote changes and merge into local repository

git push [remote] [branch] Push local branch to remote repository

UNDOING CHANGES

git reset [file] Remove file from staging index but leave unchanged locally

git clean -n Shows which files would be removed from working directory. Use -f option to execute clean.

git revert [commit] Undo changes from specified commit by creating a new commit

$ git add .

$ git add -u .

git reset是指将当前head的内容重置,不会留log信息。

git reset HEAD filename 从暂存区中移除文件

git reset –hard HEAD~3 会将最新的3次提交全部重置,就像没有提交过一样。

git reset –hard commit (38679ed709fd0a3767b79b93d0fba5bb8dd235f8) 回退到 38679ed709fd0a3767b79b93d0fba5bb8dd235f8 版本

根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:

git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 git reset –soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可 git reset –hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容

git 放弃本地修改 强制更新

git fetch –all

git reset –hard origin/master

git fetch 只是下载远程的库的内容,不做任何的合并 git reset 把HEAD指向刚刚下载的最新的版本

git新手。本地做了一些修改,我用git rebase说有冲突。我现在想把本地的请求都干掉,可能有的已经commit过了(没有push过),完全同步成远程版本,应该用什么命令?

使用命令:

git reset –hard ORIGIN/BRANCH

比如master分支:

git reset –hard origin/master

Git

Git dojo

https://www.shortcutfoo.com/

Try Git

https://try.github.io/levels/1/challenges/1

LearnGitBranching

http://learngitbranching.js.org/

查看所有远程分支:

git branch -r

拉取远程分支并创建本地分支

git checkout -b 本地分支名x origin/远程分支名x

使用该方式会在本地新建分支x,并自动切换到该本地分支x。

采用此种方法建立的本地分支会和远程分支建立映射关系。

使用如下命令:

git fetch origin 远程分支名x:本地分支名x

使用该方式会在本地新建分支x,但是不会自动切换到该本地分支x,需要手动 checkout。

采用此种方法建立的本地分支不会和远程分支建立映射关系。

三、本地分支和远程分支建立映射关系的作用

git branch –set-upstream-to origin/远程分支名 本地分支名

切换分支

git checkout 本地分支名

合并分支

git merge 本地分支名称

参考文献

Git