常用git命令

git init 会将当前目录直接转换为 Git 工作目录并在此处创建的 .git(隐藏)目录中创建存储库。

查看哪些文件处于什么状态
Show the working tree status
git status

git status 有助于跟踪已添加的更改、未添加的更改以及更改所在的分支。

查看尚未暂存的文件更新了哪些部分
git diff

直接将远程镜像克隆到本地仓库
git clone git@github.com:sikaozhe1997/Xin-Yue.git

查看远程仓库
git remote -v

从远程获取最新版本到本地
git fetch origin master

比较本地的仓库和远程参考的区别
git log -p master.. origin/master

switch branches: git checkout <branch>

switch branches with new branch created: git checkout -b <branch>

view branches: git branch

add files: git add file1 file2

commit: git commit -m “comments”

放弃修改

没有使用git add到暂存区

git checkout – filename

已经git add到暂存区

git reset HEAD filename

git add & git commit 之后

git reset commit_id

这个id是你想要回到的那个节点,可以通过 git log 查看

Configure your DVCS username for commits

global

git config --global user.name “FIRST_NAME LAST_NAME”

git config --global user.emailMY_NAME@example.com

repository-specific

git config user.name “FIRST_NAME LAST_NAME”

git config user.emailMY_NAME@example.com

git-stash

  • git stash

  • git stash list

  • git stash apply

  • git stash apply stash@{1} or git stash apply stash@"{1}"

  • git stash drop stash@{0} or git stash drop stash@"{0}"

  • git stash save "message"

  • git stash pop stash@{1} or git stash pop stash@"{1}"

pop vs apply

  • pop : Remove a single stashed state from the stash list and apply it on top of the current working tree state
  • apply: Like pop, but do not remove the state from the stash list.

checkout

  • git checkout --track origin/newsletter

Branch newsletter set up to track remote branch newsletter from origin.
Switched to a new branch ‘newsletter’

  • git checkout branch_name -- paths

clone

git clone --depth 1 url

Create a shallow clone with a history truncated to the latest commits.

rename branch

git branch -m main

del branch

delete local branch

  • git branch -d branch_name
  • git branch -D branch_name

The -d option stands for –delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches.

The -D option stands for –delete –force, which deletes the branch regardless of its push and merge status, so be careful using this one!

delete remote branch

git push origin --delete branch_name

diff

  • git diff pom.xml
  • git diff branch1 branch2 – file

Unable to clone Git repository due to self signed certificate

git config --global http.sslVerify false

Git Pull Vs Git Fetch

Fetch

git fetch really only downloads new data from a remote repository - but it doesn’t integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository.
Due to it’s “harmless” nature, you can rest assured: fetch will never manipulate, destroy, or screw up anything.

Pull

git pull, in contrast, is used with a different goal in mind: to update your current HEAD branch with the latest changes from the remote server. This means that pull not only downloads new data; it also directly integrates it into your current working copy files. This has a couple of consequences:

  • Since “git pull” tries to merge remote changes with your local ones, a so-called “merge conflict” can occur. Check out our in-depth tutorial on How to deal with merge conflicts for more information.
  • Like for many other actions, it’s highly recommended to start a “git pull” only with a clean working copy. This means that you should not have any uncommitted local changes before you pull.

keep a git branch in sync with master

1
2
3
4
5
6
7
git checkout master

git pull

git checkout feature_branch

git merge master
1
2
3
4
5
git checkout master

git merge feature_branch

git push origin master

git-reset

  • git reset --hard HEAD@{1} : if you’re feeling the moxy and haven’t done anything else

git reflog

git log

git log path

git-cherry-pick

git cherry-pick master

list conflicted files

git diff --name-only --diff-filter=U

force git pull to overwrite local files

1
2
git fetch --all
git reset --hard origin/branch_name

Force git stash to overwrite added files

git checkout stash -- .

github fork sync

1
2
git remote add upstream https://github.com/kevin-wayne/algs4.git
git pull upstream master

analyze metadata

ordered list of authors by commit count

git shortlog -sn

git shortlog -sn --no-merges

checkout any branch and overwrite local changes

git fetch --all

git reset --hard origin/abranch

git checkout $branch

Changing a commit message

change the most recent commit message

git commit --amend

change GitHub default branch from master to main

git config –global init.defaultBranch main