配置 config user 1 2 git config --global user.email "you@example.com" git config --global user.name "Your Name" Ignore Git permission changes 1 git config core.fileMode false Fix .gitignore 1 git rm -r --cached . git仓库 初始化一个版本仓库 1 git init clone远程版本库 1 git clone git@github.com:myself659/FFmpeg.git 添加远程版本库origin 1 git remote add origin git@github.com:myself659/rtmpserver_demo.git 查看远程仓库信息 1 git remote -v 删除远程仓库 1 git remote rm <repository> git修改 添加当前修改的文件到暂存区 1 git add . 提交修改到本地 1 git commit -m "fix bug 0001" 提交修改到远程 1 git push -u origin master 查看修改状态 1 git status 重命名文件 1 git mv README readme 从版本库中删除文件 1 git rm readme 取消对文件修改 1 git checkout -- readme 修改最新一次修改注释 1 git commit amend 显示所有提交 1 git show 1 git show <commit> --stat 1 git show <commit> -- <filepath> 恢复最后一次提交的状态 1 git revert HEAD 恢复某次提交的状态 1 git revert <$id> Remove untracked files & directories 1 2 3 4 # To remove untracked files git clean -f # TO remove untracked directories git clean -fd Discard local changes 1 git reset --hard Delete untracked files 1 git clean -fd git commit Edit last commit in both local and remote repository 1 2 3 4 5 6 7 8 9 # Step 1: Check recent commit history git show HEAD # Step 2: Update commit locally git add <file-path(s)> git commit --amend -m "<New-commit-message>" # Step 3: Check recent commit history to verify the correct edit git show HEAD # Step 4: Push the edit upstream git push -f # Then, verify it in the web interface of the hosting service Delete last commit in both local and remote repository 1 2 3 4 5 6 7 # Step 1: Check recent commit history to check git log --oneline -5 # Step 2: Delete commit locally and push the change to remote git reset --soft HEAD~1 # Delete commit locally git push -f # Delete commit upstream # Step 3: Check recent commit history to check git log --oneline -5 git repository rename repository 1 2 3 4 5 6 7 8 9 10 # Step 1: Rename local repository cd .. # Go to the directory that holds the repository mv <repo-old-name> <repo-new-name> cd <repo-new-name> # Step 2: Check remote url git remote -v # Step 3: Update remote url git remote set-url origin <new-url> # Step 4: Check remote url for verification git remote -v git log 查看修改log 1 git log 查看指定文件每次提交记录 1 git log <filename> 查看最近两次详细修改内容的diff 1 git log -p -2 查看提交统计信息 1 git log --stat Move a commit from one branch to another 1 git cherry-pick COMMIT-HASH 查看项目各个成员提交代码统计 1 git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done git log graph 1 git log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%an%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)- %s%C(reset)' --all git分支 查看远程分支 1 git branch -r 创建新分支 1 git branch <new_branch> 1 git checkout -b NEW-BRANCH-NAME 查看各分支创建最后提交信息 1 git branch -v 删除指定分支 1 2 git branch -d <branch_name> git push origin -d <branch-name> 强制删除指定分支 1 git branch -D <branch_name> 查看已经被合并到当前分支的分支 1 git branch --merged 查看尚未被合并到当前分支的分支 1 git branch --no-merged 合并分支 1 git merge <merge_branch> 切换分支 1 git checkout <branch_name> -f 抓取远程仓库所有分支更新并合并到本地 1 git pull 抓取远程仓库所有分支更新并合并到本地,不要快进合并 1 git pull --no-ff 抓取远程仓库更新 1 git fetch origin push所有分支 1 git push 将本地主分支推到远程主分支 1 git push origin master 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库) 1 git push -u origin master 创建远程分支 1 git push origin <local_branch>:<remote_branch> 查看所有分支名称 1 git branch --all check branch status 1 git status rename local branch 1 git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME rename remote branch 1 git push origin :OLD-BRANCH-NAME NEW-BRANCH-NAME Delete a branch on a remote repository 1 git push REMOTE-NAME --delete BRANCH-NAME clean git branches exclude main and master 1 git branch | grep -v "master" | grep -v "main" | grep -v ^\* | xargs git branch -D; Create a local copy of the existing remote branch 1 2 git fetch origin git switch --track origin/<branch-name> git diff 比较当前文件和暂存区文件差异 1 git diff <file> 比较两次提交之间的差异 1 git diff <$id1> <$id2> 比较两个分支 1 git diff <branch1> <branch2> 比较统计信息 1 git diff --stat 提交git diff How to Include Diff into Git Commit Message
...