learn git
this post base refer Ry’s Git Tutorial
Git is a VCS(Version Control System), that is “Distributed”
Git repository 3 main components:
- the working directory
- the staged snapshot
- committed snapshots
Common
git init
:create a git repo in current folder, to show out,ls -al
git status
:view the status of each file in a repository.git add <file>
:add file to stage(wait to commit)git commit
:commit to repo, using default editor to edit messagegit commit -m "<message>"
:directly cmd line to endter commit messagegit commit -a -m "<message>"
:Stage all tracked files and commit the snapshot using the specified message.git commit --amend
:Add staged changes to the most recent commit instead of creating a new one
git log
:show the commit historygit log --oneline
:show all history, but each commit only one linegit log --oneline <file>
:only show the specified file, git history in one line style
git revert <commit-id>
:Undo the specified commit by applying a new commit.git tag -a <tag-name> -m "<description>"
create an annotated tag pointing to the most recent commit.git checkout <commit-id>
:checkout to the repo versiongit checkout <tag_name>
:checkout the tag commitgit checkout <branch_name>
:checkout to the branch
git reset --hard
:Reset tracked files to match the most recent commit.git reset
:clean the stagegit reset <file>
:clean the file in the stage
git clean -f
:Remove untracked files.git reset --hard / git clean -f
:Permanently undo uncommitted changes.git branch
:View Existing Branchesgit branch <branch-name>
:Create a new branch using the current working directory as its base.git checkout <branch-name>
:Make the working directory and the HEAD match the specified branch.git branch -d <branch-name>
:Delete a branch, that already mergedgit branch -D <branch-name>
:Force the removal of an unmerged branch (be careful: it will be lost forever).
git merge <branch-name>
:Merge a branch into the checked-out branch.git merge --no-ff <branch-name>
Force a merge commit even if Git could do a fast-forward merge, will has a merge commit
git rm <file>
:Remove a file from the working directory (if applicable) and stop tracking the file.git mv <file>
:Rename or move the filegit rebase <new-base>
:Move the current branch’s commits to the tip of<new-base>
, which can be either abranch name
or acommit ID
.git rebase -i <new-base>
:Perform an interactive rebase and select actions for each commit.git rebase --continue
:Continue a rebase after amending a commit.git rebase --abort
Abandon the current interactive rebase and return the repository to its former state.
git reflog
:Display the local, chronological history of a repository.git reset --mixed HEAD~<n>
Move the HEAD backward commits, but don’t change the working directory.git reset --hard HEAD~<n>
:Move the HEAD backward commits, and change the working directory to match.git log <since>..<until>
:Display the commits reachable from but not from . These parameters can be either commit ID’s or branch names.git log --stat
:Include extra information about altered files in the log output.
Note
- nerver commit on a middle of HEAD to nowhere branch
you must checkout to master, or just create a new branch then checkout to it
unless you are using rebase
, it’s call dangling commit
that cannot be reached from any branch and are thus in danger of being lost forever
but the rebase will know will to go
- branch
-
topic branches
:exist to develop a certain topic, then they are deletedfeature branch
:longer-running type of topic branch
it was created with the intention of developing a specific feature,
-
hotfix branch
:create and test the news updates In contrast to our relatively long-running feature branch -
rule-of-thumb
- Create a new branch for each major addition to your project.
- Don’t create a branch if you can’t give it a specific name.
- merge
fast-forward merge
, has common history, and no conflict3-way merge
may occurmerge conflict
conflict will like below
<<<<<<< HEAD
<li><a href="news-1.html">Blue Is The New Hue</a></li>
=======
<li><a href="rainbow.html">Our New Rainbow</a></li>
>>>>>>> crazy
<<<<<<< HEAD
shows us the version in the current branchafter the =======
shows the version in thecrazy branch
.
list merge conflict file
git diff --name-only --diff-filter=U
- splite or rearrange commit
git rebase -i <new-base>