Just because I want to solve a problem while setting my gitee(It needs to input user name and password every time I deploy my Blog), a friend let me study git first so that I can understand what the bug is telling about!!!! (not for solving the problem, I learn it just for understanding the alert ㅍ_ㅍ……) Life is so hard……
I’ll just record some commonly used commands.
Git (/ɡɪt/)[7] is a distributed version-control system for tracking changes in source code during software development.[8] It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed,[9] data integrity,[10] and support for distributed, non-linear workflows.[11]
Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development.[12] Its current maintainer since 2005 is Junio Hamano. As with most other distributed version-control systems, and unlike most client–server systems, every Git directory on every computer is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server.[13] Git is free and open-source software distributed under the terms of the GNU General Public License version 2. ——Wikipedia
Type git
to test whether you have installed git or not.
If not, type
1 | sudo pacman -S git |
1 | mkdir -p Yao/Study/Git/Learn_git |
Initial the directory into Git
1 | git init |
Check hidden directory
1 | ls -ah |
Add a file to to the Git repository
1 | git add filename |
Submit the file to repository
1 | git commit -m "I submitted a new file" |
(-m
parameter tell the git your description)
1 | git config --global user.email "your git email" |
Go to Settings > Emails > Keep my email address private
1 | git config -- global user.email xxxxxxx+youruser@users.noreply.github.com |
Check the current status of the repository
1 | git status |
Check what has been changed and added but not been submitted
1 | git diff |
Check what has been added but not submitted
1 | git diff --cached |
Check the difference between working section and the newest edition in repository
1 | git diff HEAD -- filename |
This high light the change in words
1 | git diff --color-words |
Check the submit history
1 | git log |
Display the brief information of each version
1 | git log --pretty=oneline |
Check the graph of union of branch
1 | git log --graph |
Back to previous version
1 | git reset --hard HEAD^ |
Back to a certain abandoned version(After you reset, you can’t see the version in git log)
1 | git reset --hard commit_id |
(Don’t need to type in the full long commit id, you just need to type the first few word)
See all your commands (Help you to find a abandoned version)
1 | git reflog |
Abandon changes in working section
1 | git checkout -- filename |
(There are two status: 1. the file has not been added yet, revoke it to the same as the repository. 2. the file has been added but changed after added, revoke it to the storage cache.)
Revoke changes in storage cache to working section
1 | git reset HEAD filename |
Delete a file in working section
1 | rm filename |
Delete a file in repository
1 | git rm filename |
Revoke the delete
1 | git checkout -- deleted_filename |
(If changes has not been submitted to repository, it can’t be revoked)
Create SSH Key with GitHub
1 | ssh-keygen -t rsa -C "GitHub_email@xx.xx" |
Link your local repository to GitHub repository
1 | git remote add origin git@github.com:GitHub_name/GitHub_repository_name.git |
Upload local repository to GitHub repository(For an empty repository on GitHub)
1 | git push -u origin master |
Clone a local repository to Github repository
1 | git clone git@github.com:GitHub_name/GitHub_repository_name.git |
Create and switched to the branch
1 | git checkout -b branch_name |
Or using two commands
1 | git branch branch_name |
New way of creating and switching branches
1 | git switch -c branch_name |
Check the current branch
1 | git branch |
Delete the branch
1 | git branch -d branch_name |
Forcibly delete a branch
1 | git branch -D branch_name |
Merge branch a to b
1 | git checkout b |
Check the merged branch that can be safely deleted
1 | git bransh --merged |
Check common ancestor of two commits
1 | git merge-base xxx xxx |
Not using Fast forward
1 | git merge --no-ff -m "description" branch_name |
Hide recent work section
1 | git stash |
Check hide branch repository
1 | git stash list |
Restore the hidden branch repository
1 | git stash apply stash@{xx} |
(first restore and then delete)
Or restoring and deleting at the same time
1 | git stash list |
Copy modified file from another branch
1 | git cherry-pick commit_id |
Add remote
1 | git remote add <name> <url> |
Check remote information
1 | git remote |
For more detail
1 | git remote -v |
Push branches
1 | git push origin branch_name |
Get the newest commit from remote repository . --prune
means delete the local branch if it is removed from remote branch
1 | git pull |
Let local branch connect with remote branch
1 | git branch --set-upstream-to=origin/branch_name branch_name |
Set up the remote branch when pushing
1 | git push --set-upstream origin branch_name |
Make your history commit graph a line
1 | git rebase |
Create a tag for a version of repository
1 | git tag tag_name |
Tag to a specific commit
1 | git tag -a v1.0 <SHA> |
Check for tags information
1 | git show tag_name |
See all tags
1 | git tag --list |
Delete a tag
1 | git tag -d tag_name |
Push the tag to remote repository
1 | git push origin tag_name |
Delete a tag from remote repository
1 | git tag -d tag_name |
Let Git display with color
1 | git config --global color.ui true |
File .gitignore
helps ignore deploying special files
Forcibly add a file
1 | git add -f file_name |
Check ignore rules
1 | git check-ignore -v file_name |
Set a alias for long full name commands
1 | git config --global alias.abbrev full_name |
Some Custom Aliases
1 | // original command |
1 | git config --global alias.ec "config --global -e" |
1 | git config --global alias.ch "checkout" |
1 | git config --global alias.cob "checkout -b" |
1 | git config --global alias.s "status -s" |
clean and update branch alias
1 | git config alias.dlb `!git checkout <DEFAULT-BRANCH> && git pull --prune && git branch --merged | grep -v "\*" | xargs -n 1 git branch -d` |
Git setting that automatically converts line endings when checking files in and out of the repository.
Line endings differ by OS:
Windows uses CRLF
(\r\n
) - true
Unix/Linux/macOS use LF
(\n
) - input
1 | git config --global core.autocrlf <true|false|input> |
--wait
wait for the editor close before continuing.
1 | git config --global core.editor "code --wait" |
1 | git reset --soft HEAD~1 |
Only undo the commit
1 | git reset --mixed HEAD~1 |
Also reset the staging area
1 | git resert --hard HEAD~1 |
Reset the working directory, means you will loose your edition at all,
however, you can look for it using reflog
only if you submitted it before
1 | git reflog |
Create a new commit which the content is the reverse action of this certain commit.
1 | git revert hashcode --no-edit |
revert will automatically pops up edit to let you specify commit message, use --no-edit
to use default message.
--oneline
: Display each commit in a single line
--graph
: Show ASCII graph of the branch and merge structure
--decorate
: Add branch and tag names (refs) as decorations
--all
: Show commits from all branches (not just the current one)
1 | git log --oneline --graph --decorate --all |
See the exact change of each commit
1 | git log --stat |
See the detail difference of each commit
1 | git log --patch |
See the more recent x commits
1 | git log -n x |
1 | git bisect start |
And then git will automatically find the middle commit, and switch to it.
Run using bisect. The test file must return numbers, 0 as good, any other number as bad.
1 | git bisect run test_file_or_command |
Reset bisect to return to a branch
1 | git bisect reset |