Git常用操作指南
By Long Luo
Windows 下
.gitconfig1
2
3
4
5
6[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
CONFIGURING GIT
Set the name that will be associated with your commits1
git config --global user.name "[name]"
Set the email that will be associated with your commits1
git config -global user.email "[email]"
Create a shortcut for a Git command (e.g. alias.glog “log —graph —oneline”)1
git config -global alias.[alias] [command]
Set the default text editor to use for commit messages (e.g. vi)1
git config -global core.editor [editor]
Open the global config file in a text editor for manual editing1
git config -global —edit
INITIALIZING AND CLONING
Initialize an empty Git repository in the current directory1
git init
Create an empty Git repo in the specified directory1
git init [directory]
Clone a remote Git repository from the url into a local directory1
git clone [url]
Clone a remote repo into the specified local directory1
git clone [url] [directory]
EXAMINING LOGS
Show the commit history for the current branch1
git log
Show the diffs from each commit in the commit history1
git log -p
Show stats (files changed, insertions,deletions) for each commit1
git log —stat
Show condensed summary of commits in one line each1
git log —oneline
Draw a text based graph of commits with branch names1
git log —graph —decorate
Show unstaged file differences compared to current index1
git diff
Show differences between staged changes and the last commit1
git diff -cached
Show changes between two commits1
git diff [commitl] [commit2]
Show changes made in the specified commit1
git show [commit]
VERSIONING FILES
Stage file changes to be committed1
git add [file]
Commit the staged snapshot with commit message1
git commit -m "[message]"
Remove file from staging index and working directory1
git rm [file]
Move or rename file in Git and stage the change1
git mv [file] [newpath]
BRANCHING AND MERGING
List all the branches in the current repository1
git branch
Create a new branch with name [branch]1
git branch [branch]
Switch the current branch to [branch]1
git checkout [branch]
Create a new branch and switch to it1
git checkout -b [branch]
Merge the history of [branch] into the current branch1
git merge [branch]
Delete the local branch [branch]1
git branch -d [branch]
RETRIEVING AND UPDATING REPOSITORIES
Fetch branches and commits from the remote repository1
git fetch [remote]
Fetch remote changes and directly merge into local repository1
git pull [remote]
Fetch remote changes and rebase onto local branch1
git pull —rebase [remote]
Push local branch to remote repository1
git push [remote] [branch]
Push all local branches to remote1
git push --all [remote]
Push all local tags to remote repository1
git push —tags [remote]
REWRITING GIT HISTORY
Rebase current branch onto [branch]1
git rebase [branch]
Interactively rebase current branch onto [commit]1
git rebase -i [commit]
Show history of Git commands for current repository1
git reflog
clear staging area, rewrite working tree from specified commit1
git reset —hard [commit]
REMOTE REPOSITORIES
Create remote connection with url and alias [name]1
git remote add [name] [url]
Fetch all branches from remote repository1
git fetch [remote]
Fetch remote changes and merge into local repository1
git pull [remote]
Push local branch to remote repository1
git push [remote] [branch]
UNDOING CHANGES
Remove file from staging index but leave unchanged locally1
git reset [file]
Shows which files would be removed from working directory. Use -f option to execute clean.1
git clean -n
Undo changes from specified commit by creating a new commit1
git revert [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
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 本地分支名称