Git and GitHub 学习笔记1
“Git Tutorial for beginner” 学习笔记(link)
Git Installation
sudo apt-get install git
Git configuration
git config --global user.name "your name"
git config --global user.email your_email
git config --global core.editor "your_evironment"
git config --global core.autocrlf input #true for windows, input for Linux/Mac
For more git config information (link) or
git config --help
git config -h
Initializing a Repository
git init
Staging files
git status #
git add file1 file2/ or .
Committing changes
git commit -m "Initial commit." #-m message
git commit # type the message in the file.
Each commit should represent a logically step change.
If there is a bug fix and a typo, you need to commit them separately.
Removing Files
rm file_name
git status # Cannot delete file_name in Git
git ls-files # show current files in Git
git add file_name
git status # The file_name has been deleted from Git
git commit -m "Remove unused code."
or you can delete file_name directly by using
git rm file_name
Rename or Moving Files
mv file_name1 file_name2
git status
git add file_name1
git add file_name2
git status # rename a name
or you can rename file_name1 directly by using
git mv file_name_1 file_name_2
git status
git commit -m "Refactor code"
Ignoring Files
Do not want update your files onto git
echo dir/ > .gitignore
vim .gitignore
# In the file, you can add every files which you don't want to update on git
git status
git add .gitignore
git commit -m "Add gitignore"
Gitignore on GitHub will show which files don’t need to push on Github.
Viewing the Staged and Unstaged Changes
git diff --staged # The changes in this stage
git diff # The changes in last operation
Diff Tools
# Setting VScode as default
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "vim --wait --diff $LOCAL $REMOTE"
git config --global -e
# Setting VIM as default
git config --global diff.tool vimdiff
git config --global difftool.prompt false
git config --global alias.d difftool
Open .gitconfig file link.
- 对所有用户,位于/etc/gitconfig
- 对于当前用户,位于~/.gitconfig
- 对于该git仓,位于.git/config
Viewing the History
git log
git log --oneline
git log --oneline --reverse
Viewing a Commit
git show
git show HEAD~1: file_name
git ls-tree HEAD~1
Unstaging Files
git restore --staged file_name
Discard local changes
git restore . #undo all the commit
git clean # git clean -h
Restore a file to early version
git rm file_name
git status -s
git commit -m "delete a file"
git log --oneline
git restore --source=HEAD~1 file_name # git resotre -h