Git的基础

配置git

一些 Git 基本配置

git config --global user.name "Nguyen Van A"
git config --global user.email nguyen_van_a@example.com
git config --global core.editor notepad++
git config --global help.autocorrect 1
git config --global core.autocrlf false

# list all global config
git config --global --list

在本地使用git进行工作

初始化 – 状态 – 添加 – 提交 – 日志

    Create a local repo
git init
    See untracked files
git status
    Add (stage) a file to commit it
git add file_name

提交文件

git commit -m "message"
    See commit log
git log
    Add (stage) all modified (updated) files to commit
git add -u
    Then commit it to local repo
git commit -m "message"
    Add (stage) all changed files (including new files)
git add -A
    Then commit them
git commit -m "message"

展示差异

    See changes between 2 commits
git diff 1st_commit_number..2nd_commit_number
    Or
git diff HEAD~1..[HEAD]

(查看HEAD和前一个提交之间的差异)

    Add (stage) a file to commit
git add file_name
    See staged changes
git diff --cached
    See changes of a commit
git show commit_number
    Or
git show HEAD

添加-重置软件 –

    Add (stage) a file to commit
git add file_name
    But want to unstage it after that
git reset --soft

结账 – 分支

    Create a new branch from the current branch
git checkout -b new_branch_name
    See available branches at local
git branch
    Checkout to another local branch
git checkout a_branch
    Rename the current branch
git branch -m new_name
    Delete a local branch
git branch -d branch_to_delete

结帐 – 强制重置

    Checkout to remove uncommitted changes of a file to HEAD
git checkout file_name
    Reset to remove all uncommitted changes (of all files)
git reset --hard
    Move the HEAD back 1 commit
git reset --hard HEAD~1

合并

    Merge a branch to the current branch
git merge the_branch_to_merge_to_current_branch
    After that delete the merged branch
git branch -d the_branch_merged_to_current_branch

使用远程git进行远程工作

    Clone a repo
git clone url_of_the_repo
    See commit history of the project
cd to_the_cloned_project
git log 
    Check what is remoted
git remote -v
    fetch new branches… from remote repo
git fetch
    View remote branches
git branch -r
    Create a local branch from a remote branch
git checkout remote_branch # Will create a local branch that tracks the remote branch
    • Make some changes then commit to the local branch (see Working locally with git)

 

    Push committed content back to the remote branch
git push
    Pull changes of the remote branch to the local branch
git pull
    Clean remote branches that have been deleted from the remote repo
git remote prune [remote_repo_name]

(remote_repo_name的名称是类似于origin)

进一步掌握Git

藏匿

如果

    • You are working on a branch

 

    • You have some uncommited changes

 

    • And the changes are not ready to commit

 

    Then you have to checkout another branch to fix something (that is urgent)

你可以将更改暂存以备后续提交

git stash是将修改内容保存到stash中

查看当前暂存列表

从存储列表中取出一个存储,并应用到你的代码中,但不从存储列表中移除。

取出一个储藏(stash),将其从储藏列表中移除。

删除一个存储

挑选最好的/优质的

如果:

    • You have commitA on a branchA

 

    You want to apply that commit onto another branchB (with the same changes)

你可以从分支A选择性地挑选出提交A,并将其合并到分支B上。

git checkout branchB
git cherry-pick commit_number_of_commitA
广告
将在 10 秒后关闭
bannerAds