ORIGIN

Git Commands

Git 6 mins961 words

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.

Introduction

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

Install Git

Type git to test whether you have installed git or not.

If not, type

1
→ sudo pacman -S git

Create a repository

1
2
3
4
→ mkdir -p Yao/Study/Git/Learn_git
→ cd Yao/Study/Git/Learn_git
→ pwd
/home/o_oyao/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
2
→ git add filename
→ git add filename1 filename2

Submit the file to repository

1
→ git commit -m "I submitted a new file"

(-m parameter tell the git your description)

Status and Version

Status

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

Version

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
2
→ git reset --hard HEAD^ 
→ git reset --hard HEAD~100

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

Revoke changes

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 file

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)

Remote repository

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

Branches

Create and Merge

Create and switched to the branch

1
→ git checkout -b branch_name

Or using two commands

1
2
→ git branch branch_name
→ git checkout branch_name

New way of creating and switching branches

1
2
→ git switch -c branch_name
→ git switch master

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

Branch management

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
2
→ git stash apply stash@{xx}
→ git stash drop

(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

Remote

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

1
→ git pull

Let local branch connect with remote branch

1
→ git branch --set-upstream-to=origin/branch_name branch_name

Rebase

Make your history commit graph a line

1
→ git rebase

Tags

Create a tag

Create a tag for a version of repository

1
2
3
→ git tag tag_name
→ git tag tag_name
→ git tag tag_name -m "description"

Check for tags information

1
→ git show tag_name

Operate tags

Delete a tag

1
→ git tag -d tag_name

Push the tag to remote repository

1
2
→ git push origin tag_name
→ git push origin --tags

Deleter a tag from remote repository

1
2
→ git tag -d tag_name
→ git push origin :refs/tags/tag_name

Custom Git

Let Git display with color

1
→ git config --global color.ui true

File .gitignore helps ignore deploying special files

Ignore specific file

Forcibly add a file

1
→ git add -f file_name

Check ignore rules

1
→ git check-ignore -v file_name

Alias

Set a alias for long full name commands

1
→ git config --global alias.abbrev full_name
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|