Basic Git

来源:互联网 发布:js.qexvmc.1转跳网址 编辑:程序博客网 时间:2024/04/29 20:23

Git as a common tool of VCS

operations/commands checklist

(1)git config

(a)global proxy
git config --global http.proxy host[:port]
git config --global https.proxy host[:port]
(b)editor
git config --global core.editor "vim_path"

(2)view commit history
git log -2 -p --stat

optional arguments:
   (a)p #shows difference introduced in each commit
   (b)number #number limiting
   (c)stat #as a summarized option
   (d)pretty=format:”fmt_str” #custom log output format
   (e)since,until #time-limiting

(3)undo things(撤销操作)
there are a few use cases:
undo a modifying file, either whole(will discard anything you have modified to the file) or partial/one by one
git checkout -- file_path OR git checkout -p/--patch

undo/unstaging a staged file, before committing
Either
   git add -i #enter interactive mode
   type 3|r|revert in What now prompt #enter revert
   type 1 #like, first item to be reverted
Or
   git reset HEAD file_path

(4)git add
the command git add could
   (a)updates the index using the current content found in the working tree, to prepare the content staged for the next commit
   (b)track files
   (c)mark merge-conflicted files as resolved

(5)git-checkout(创建/切换/重置分支)
git checkout
optional arguments:
   (a)b #create and checkout a new branch
   (b)B #create and checkout a new branch/reset a existing branch

for example,

git checkout -b test_b#equivalent to the two linegit branch test_bgit checkout test_b

(6)git-stash(存档)
Situation:if your working directory or staging area has uncommitted changes that conflict with the branch you are checking out, so that not do a commit of half-done work and can go back this pointer later to continue. its similar screenshot looks like,
这里写图片描述

Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any future time.

git stash [--save] #save unfinished changes in current branch
git stash list #
git stash show [stash@{index_num}] #
git stash apply [--index] [stash@{index_num}] # without –index , will not reapply staged changes but revert previously-staged to unstaged.
git stash pop/drop [stash@{index_num}] #pop outputs unfinished changes,while drop removes stash directly
git stash clear #remove stashes

(7)inspect/add a remote
git remote show origin #
git remote add remote_name remote_url #like upstream as remote_name

(8)git fetch
git fetch [option] [remote_repository] [refspecs]

(9)push to a remote
git push remote_name branch_name

(10)git reset

reset HEAD to specific state
(a)revert back to specific commit
git reset HEAD[~[NUM]]
like git reset HEAD~2, it revert three commits back from lastest commit

(c)optional–undo reset
git reflog #undo to a certain option
git reset HEAD@{index}
git log -number #verify

(b)optional&IMPORTANT–keep local master synced with remote master of your fork
git push origin master [-f|force] #push changes to remote origin/master with possible -f option.

Warning:once done, pull request from remote master branch will be closed.

(11)gitrevisions
specifying revisions and ranges for Git
  (a)<rev1>..<rev2>
   include commits that are reachable from rev1 and exclude ones reachable from rev2

(12)git revert
note:keep working directory clean
it is used to record some new commits to reverse effect of some earlier ones(often only a faulty one)

(13)git reflog
to be continued.

(14)git rebase
note: keep working directory clean
a use case: squash commits where git rebase -i HEAD~[NUM], git rebase --edit-todo and git rebase --continue are core instructions.
By editing commit message, can squash commit with intermediate segement but from lastest.

(14)git status
special short-format characters for -s option
?? for new files that are not tracked
A for new files that have been added to staging area
M for modified files

(15)git mv
rename a file

(16)git rm
remove a file from staging area
git rm [-r] -f or
git rm [-f] --cached

Concepts

(1)branch
   Git branches is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast.
Git encounrages workflows that branch and merge often.

(2)HEAD
Git keeps a special pointer called HEAD, pointing to local branch you are currently on and different than that of other VCS like Subversion.
you can view its sign by git log --oneline --decorate

Words

commit: 提交
stage:暂存

related ones in command:
(1) hunk (代码片/块)
like git checkout -p/--patch where the option interactively select hunks in the difference between the tree-ish (or the index, if unspecified) and the working tree.
hint hunk

(2)index
it holds a snapshot of the content of the working tree, and it is this snapshot that is taken as the contents of the next commit. Thus after making any changes to the working directory, and before running the commit command, you must use the add command to add any new or modified files to the index

(3)snapshot(快照)/commit

to be continued

(4)squash(压缩)
a use case for final pull request with multiple invalid/scattered commits
its behavior like squash multiple commits into a small handful of well-labeled commits.

for example, squash last 3 commits into one

(5)amend(修改/改进)
like git commit --amend #amend current commit/tag log message