Set up git
- User name: git config --global user.name "my name"
- Email: git config --global user.email "email addr"
- Init a repo: git init
Branching
- List branches: git branch -a
- Work on a branch: git checkout <branch name>
- Checkout a remote branch: git checkout -b <new branch> <start point>
e.g. git checkout -b stable-1.3 remotes/origin/stable-1.3
- Create a new local branch: git branch [branch_name]
- Delete a local branch: git branch -d [branch_name]
- Delete a remote branch: git push [remote] [:branch_name]
e.g.git push origin :newfeatureThat will delete thenewfeaturebranch on theoriginremote, but you’ll still need to delete the branch locally withgit branch -d newfeature.
- Publish a local branch to remote:
e.g. git push [remote] [local_branch_name]:[remote_branch_name]
Remote repositories
- list remote repos: git remote
- Add a remote repo: git remote add <name> <url>
e.g. git remote add github https://github.com/luohao-brian/qemu-kvm.git
- Update remote repos, which will fetch all updates from the remotes
e.g. git remote update
Synchronization
- Pull a repo: git pull <repo>
- Push to a repo: git push <repo> <refspec>
e.g. git push github stable-1.3
Undo
- If you've messed up the working tree, but haven't yet committedyour mistake, you can return the entire working tree to the lastcommitted state with:
$ git reset --hard HEAD
$ git reset --hard HEAD~1
Cherry-picking
If you would like to cherry-pick a commit from a branch, use command as follows, in which -x suggests git to append a line that says "(cherry picked from commit ...)"...
$ git cherry-pick -x <commit_id>- If a cherry-picking fails, try abort all temporary actions by:
$ git cherry-pick --abort - If you would like to cherry-pick a range of commits, try:
$ git cherry-pick [old_commit]..[new_commit] - or, go though each of them by command as follows and then cherry-pick it one by one:
$git rev-list --reverse b..f | xargs -n 1 git cherry-pick
Git基础教程:设置与常用命令详解
本文详细介绍了如何使用Git进行版本控制,包括如何设置用户名和邮箱、初始化仓库、创建与切换分支、远程仓库操作及同步、撤销更改、 cherry-pick 操作等核心功能。
13万+

被折叠的 条评论
为什么被折叠?



