Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

常用Git命令 #14

Open
zjs1224522500 opened this issue May 23, 2019 · 6 comments
Open

常用Git命令 #14

zjs1224522500 opened this issue May 23, 2019 · 6 comments
Labels
Command Command line

Comments

@zjs1224522500
Copy link
Owner

zjs1224522500 commented May 23, 2019

1. Common

>> git status // Show status of repo
>> git pull // Pull commit from binding remote branch
>> git add xxx.file // Add specific file to cache
>> git add ./ // Add all file to cache
>> git commit -m 'msg' // New commit with msg
>> git push origin : // Push new commit from local branch to remote branch
>> git stash // Store the data temporaryly
>> git stash pop // Pop out the data in stash

Change the latest commit

>> Modify the files
>> git add file-name
>> git commit --amend
>> Edit the commit message
>> git push origin local-branch:remote-branch -f

2. Log

>> git log // query current branch log tree
>> git log // query specific branch log tree
>> git log --author="Elvis Zhang" // query the commit info committed by Elvis Zhang
>> git log --pretty=oneline // Show the history briefly

3. Rebase

>> git pull rebase // Pull commit and apply in right order
>> git rebase -i HEAD~3 // Rebase last 3 commits Pick/Squash (Merge Commits)

4. Branch

>> git checkout // Switch branch
>> git branch // List branches
>> git branch // New branch with branch name
>> git branch -d // Delete the given branch, cannot be current branch
>> git checkout -b // New branch and switch to the branch
>> git merge // Merge the given branch to current branch
>> git cherry-pick // Merge given commit to current branch

5. Clean

>> git clean -f // Delete the untracked files

6. Reset

>> git reset --hard origin/ // Reset the cache and the commit
>> git reset --soft // Reset the commit and keep the modification.

7. HEAD

  • HEAD is used as a pointer for latest commit.
  • HEAD~N is used as a pointer for (latest commint pointer - N)

8. RM

>> git rm -r --cached . // Refresh cache. (In order to enforce the change of .ignore file)

9. Remote

>> git remote show // List all remote repo name [Always be used to sync official repo]
>> git remote add [name] [url] // Add a new remote repo
>> git remove [name] // Remove the current remote repo
>> git remote -v // List the remote repo name and url.

10.Reflog

>> git reflog // Show the git operation history

11. Checkout

Branch Management

>> git checkout -b new_branch_name // Create new branch and change to it.
>> git checkout new_branch_name // Change to the exsited branch

12. Diff

>> git diff // Compare the cache and working area.
>> git diff --cached […] // Compare the cache area and latest commit. [..] file name
>> git diff HEAD […] // Compare the working area and latest commit.
>> git diff commit-id […] // Compare the workinga area and given commit.
>> git diff --cached [] […] // Compare the cache and given commit.
>> git diff [] [] // Compare two commits.
>> git diff --HEAD > patch-name // Make the differences between the working area and latest commit as a patch. And can use command git apply patch-name to apply patch.

13. Branch

>> git branch // Show existed branched
>> git branch -vv // Show binding relation between local branch and remote branch
>> git branch new_branch_name // Create new branch with branch_name and not switch.
>> git branch -d to_be_deleted_branch_name // Delete the branch with name (Not working branch)
>> git branch -D to_be_deleted_branch_name // Force delete the branch with name
>> git branch --set-upstream-to=origin/remote_branch_name // Bind remote branch and local branch.

@zjs1224522500 zjs1224522500 added the Command Command line label May 23, 2019
@zjs1224522500
Copy link
Owner Author

zjs1224522500 commented Mar 24, 2020

新建本底仓库和远程同步

  • git init // 初始化仓库
  • git config --global user.email "[email protected]" // 配置 user email
  • git remote add origin [email protected]:zjs1224522500/C-Example.git // Add remote repo

仅修改当前 repo

@zjs1224522500
Copy link
Owner Author

zjs1224522500 commented Aug 16, 2020

修改ignore文件之后的同步

  • modify .gitignore
  • git status --ignored // 查看 ignore 状态
  • git rm -r --cached . // 删除缓存
  • git status --ignored // 查看 ignore 状态
  • git add . // 重新添加
  • git commit ...

@zjs1224522500
Copy link
Owner Author

zjs1224522500 commented Dec 1, 2020

Git 配置

  • SSH 代理配置 .ssh/config,使用 nmap
Host github.com
  ProxyCommand D:\\nmap-7.70-win32\\nmap-7.70\\ncat.exe --proxy-type socks5 --proxy 127.0.0.1:10808 %h %p
  • .gitconfig
[user]
	email = [email protected]
	name = Elvis Zhang
[http]
    proxy = http://127.0.0.1:10809
[https]
    proxy = http://127.0.0.1:10809

@zjs1224522500
Copy link
Owner Author

zjs1224522500 commented Apr 13, 2021

Git tag

  • Add new tag: git tag -a $version -m "$msg" eg. git tag -a v1.4 -m "my version 1.4"
  • List tags: git tag -l
  • Push tag to origin: git push origin <tagname>
  • Delete tags: git tag -d <tagname>
  • Delete origin tag git push origin --delete <tagname>
  • Switch to specific version git checkout <tagname>

@zjs1224522500
Copy link
Owner Author

Git 删除历史记录

  • git checkout --orphan latest_branch // 创建一个 孤儿分支
  • git add -A // 添加所有文件并提交
  • `git commit -am 'initial commit'
  • git branch -D master // 删除原有的主分支
  • git branch -m master // 修改当前分支为主分支
  • git push -f origin master // 强制 push 当前分支到主分支
  • git branch --set-upstream-to=origin/master // 由于是后来改名的,和远程主分支关联一下

@zjs1224522500
Copy link
Owner Author

zjs1224522500 commented Jun 10, 2021

Submodules

https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%AD%90%E6%A8%A1%E5%9D%97

添加子模块

获取子模块

  • git submodule update --init --recursive // clone 后拉取子模块
  • git clone --recurse-submodules https://github.com/zjs1224522500/leveldb-learn.git // clone 时拉取子模块
  • git submodule update --init xxx // 拉取单个 xxx 子模块

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Command Command line
Projects
None yet
Development

No branches or pull requests

1 participant