A commit is a snapshot of contents in working directory (prepared in staging area).
A commit is uniguely identified by an ID
SHA1
hash.Any commit can be referred to by
SHA-1
=> 40-hex digitSHA-1
=> first few hex digits that can uniuely
identify the commitAn example of a commit from running
git log
:
commit 84d06cdc06389ae7c462434cb7b1db0980f63860 (HEAD -> master, origin/master, origin/HEAD)
Merge: 26c4f98f 48bf2fa8
Author: Junio C Hamano <gitster@pobox.com>
Date: Fri Mar 26 14:59:47 2021 -0700
Download http://git-scm.com/downloads
Various GUI clients: http://git-scm.com/downloads/guis
Visual Studio Code
extensions, e.g.
If git is already installed:
git clone https://github.com/git/git
There are 4 levels of configurations
Use option to specify level of a configuration
git config --system # require admin permission
git config --global
git config --local
git config --worktree
git config # default to --local
Get help on command-line option summary:
git command -h
Get manual page help on:
git command --help
It's is conventional to supply your name and email.
git config --global user.name "Name Surname"
git config --global user.email "user@ssome_domain"
Configuration file format is similar to .ini
format.
git config --system -e # require admin permission
git config --global -e
git config --local -e
git config -e # default to --local
Configure editor to use when writing commit message.
Examples of some editors
git config --global core.editor "code --wait"
git config --global core.editor "nano -w"
git config --global core.editor "vim"
Default depends on systems and installation.
End of line:
CR
LF
) - Carriage Return, Line
FeedLF
) - Line FeedShould store end of line as LF
in repository.
For Linux/MacOS:
git config --global core.autocrlf input
For Windows:
git config --global core.autocrlf true
Add file .gitattributes
to your repository with
content.
# Contents in .gitattributes
* text=auto
Create a repository inside a directory:
mkdir myproject
cd myproject
git init
Or create a new directory with a repository:
git init myproject
Clone a local directory:
git clone /path/to/local/directory [optional new directory name]
git clone /d/projectA [optional directory name]
Clone a remote repository
git clone url
Linear history workflow.
git init myproject
Add some files to working directory.
Then add these changes to staging area
git add f1.txt # add single file
git add f1.txt f2.txt # add multiple files
git add * # add all files in current directory (shell expands the asterisk)
git add . # add all files recursively from this directory
Check changes between
git status # long description
git status -s # short status using characters
Make a commit with commit message.
git commit -m "Initial commit"
If run without -m
option, git
will open
default editor.
git commit
Updates files in the working tree to match given branch or current branch.
git checkout main # update working tree to branch main
git checkout master # update working tree to branch master
git checkout feature_x # update working tree to branch feature_x
# since git version 2.23
git switch main # update working tree to branch main
git switch master # update working tree to branch master
git switch feature_x # update working tree to branch feature_x
Make contents in working directory the same as in staging area.
git restore f1.txt # restore single file
git restore f1.txt f2.txt # restore multiple files
git restore * # restore all files in current directory (shell expands the asterisk)
git restore . # restore all files recursively from this directory
Making contents in staging area the same as in HEAD. Also called "unstage changes".
git restore --staged f1.txt # restore single file
git restore --staged f1.txt f2.txt # restore multiple files
git restore --staged * # restore all files in current directory (shell expands the asterisk)
git restore --staged . # restore all files recursively from this directory
git restore -s commit_ID f1.txt # restore f1.txt to version in commit_ID
git restore --source commit_ID f1.txt # the same with long option
git log # show log from the latest commit
git log --reverse # show log from the first commit
git log --oneline # show log of one line for each commit
git log --pretty=oneline # show pretty format in one line
See changes in working directory from staging area
git diff
See changes in staging area from HEAD
git diff --cached
git diff --staged # synonymous to --cached
Configure diff.tool
with a name "vscode":
git config --global diff.tool vscode
Configure difftool
with the actual command:
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
See changes with diff tool.
git difftool
git difftool --staged
git show commit_ID
Remove files that are already tracked by Git.
git rm f1.txt
git rm f1.txt f2.txt
git rm *
git mv f1.txt f3.txt
Add file .gitignore
to your repository
Example of contents in .gitignore
# Object files "#" starts line comment
*.o # ignore all files that end with ".o"
*.obj # ignore all files that end with ".obj"
Create a tag that will be associated with a commit.
git tag v1 # tag current commit with name "v1"
git tag v2 daf723e # tag commit id "daf723e" with name "v2"
alias
Create some common operations with shorter keystrokes.
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.la "log --all --oneline --decorate"
git config --global alias.hist "log --pretty=format:'%C(auto)%h %Cgreen(%cd)%C(auto)%d %C(auto)%s %C(cyan)[%an]' --graph --date=short"