What is Git
Git is a distributed version control system, developed by Linus Torvalds in 2005 for managing the Linux kernel. By keeping track of every change made, it is always possible to restore a previous version of the project, allowing multiple people to work simultaneously on it remotely, automatically managing the most trivial conflicts, and delegating to developers the resolution of the most complex ones, with an intuitive and powerful command line interface.
Command line
Initialize a repository
git init
# E.g.
# git init
Clone a repository
To clone a repository is to create a local copy of the remote repository. A repository can be cloned via HTTPS or SSH. The HTTPS method may be simpler for newcomers, but requires managing the credentials used for authentication to the remote server. The second requires creating an SSH key and configuring it on the remote server.
git clone <url>
# E.g.
# HTTPS
# git clone https://github.com/UNICT-DMI/Telegram-SpottedDMI-Bot.git
# SSH
# git clone git@github.com:UNICT-DMI/Telegram-SpottedDMI-Bot.git
Add files to the repository
To add files to the repository means to insert them into the staging area, ready to be committed.
git add <file>
# E.g.
# git add README.md
Commit changes
Once satisfied with the changes made, they can be committed, creating a new snapshot of the project. It will always be possible to return to this snapshot in the future.
git commit -m "<message>"
# E.g.
# git commit -m "Added README.md"
Push changes
Changes made locally must be shared with the remote repository using the push
command.
The -u
flag allows you to set the local branch as the upstream of the remote branch, so you can use git pull
and git push
without specifying the branch in the future.
git push [-u] [remote] [branch]
# E.g.
# git push -u origin master
The remote branch doesn’t have to have the same name as the local one. In this case one must be explicit when pushing the branch.
git push <remote> <local branch>:<remote branch>
# E.g.
# git push gitlab main:prescan
Fetch changes
To download the changes made by other collaborators to the remote repository, you can use the fetch
command.
git fetch
# E.g.
# git fetch
Pull changes
In addition to downloading changes from the remote repository, you can merge them with local changes immediately using the pull
command.
git pull
# E.g.
# git pull
Delete remote branch
To delete a remote branch, you can use the push
command with the --delete
flag.
git push origin --delete <branch>
# E.g.
# git push origin --delete feature
Tags
Tags are used to mark specific points in the project’s history. They are often used to signify a new releases of the software.
git tag <tag>
# E.g.
# git tag v1.0
# Synchronize tags with the remote repository
git push --tags
# E.g.
# git push --tags
Stop tracking a file
It is possible to stop tracking changes to a file without deleting it from the repository.
git update-index --assume-unchanged <file>
# E.g.
git update-index --assume-unchanged some_file
To revert the operation, use the following command:
git update-index --no-assume-unchanged <file>
# E.g.
git update-index --no-assume-unchanged some_file
Configuration
Show configuration
git config --list
# E.g.
# git config --list
Set a parameter
git config [--global] <parameter> <value>
# E.g.
# git config --global user.name "Mario Rossi"
Remove a parameter
git config [--global] --unset <parameter>
# E.g.
# git config --global --unset user.name
.gitconfig
The .gitconfig
file contains the global configuration of Git.
This is the file I usually use.
# .gitconfig
[core] # Default editor, Visual Studio Code
editor = code --wait
[user]
name = Ernesto Casablanca
email = casablancaernesto@gmail.com
username = TendTo
signingkey = F104E4A645FE7555 # GPG key to sign commits
[alias] # Aliases for the most common commands
aliases = !git config --get-regexp alias | sed -re 's/alias\\.(\\S*)\\s(.*)$/\\1 = \\2/g'
lg = log --graph --date=relative --pretty=tformat:'%C(auto)%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'
graph = log --all --decorate --oneline --graph --date=relative --pretty=tformat:'%C(auto)%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'
oops = commit --amend --no-edit
uncommit = reset --soft HEAD~1
[diff] # Diff tool, Visual Studio Code
tool = vscode
[difftool "vscode"]
cmd = "code --wait --diff $LOCAL $REMOTE"
[commit] # Sign commits by default
gpgSign = true
[init]
defaultBranch = main
[color]
ui = auto
[tag]
sort = version:refname
[mergetool]
keepBackup = false
keepTemporaries = false
writeToTemp = true
[gpg] # Use GPG2 instead of GPG
program = gpg2
.gitignore
The .gitignore
file allows you to specify the files and folders that should not be tracked by Git.
It is possible to use wildcards and relative or absolute paths.
The latter are relative to the root of the repository.
Patterns can be negated using the !
prefix.
Usually, it is advisable to get a preconfigured .gitignore
file, suitable for the programming language used.
# .gitignore
node_modules/ # node_modules folder
/.vscode/ # .vscode folder, only in the root
*.log # All files with .log extension
*kee* # All files with 'kee' in the name
!.gitkeep # Overrides the previous rules, tracking the .gitkeep file
.gitattributes
The .gitattributes
file allows you to specify the rules for tracking files.
It allows you to specify the rules for tracking files, such as line endings, merging, diff, and encoding.
# .gitattributes
# Normalize line endings based on the operating system
* text=auto
# Normalize line endings in CRLF for files with .cmd or .bat extension
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
# Normalize line endings in LF for shell scripts
*.sh text eol=lf
# Ignore the programming language for files with .mps extension
# Useful to make the statistics produced by linguist accurate
*.mps linguist-vendored
.git-blame-ignore-revs
The .git-blame-ignore-revs
file allows you to specify the commits that should not be tracked by git blame
.
Useful to avoid making the project blame unnecessarily illegible with formatting or refactoring commits that involve many lines of code.
# .git-blame-ignore-revs
# Format
db9124f1de0478dcac525009b6f1589b44a7edd8
# Rename internal function
c6378bdd46cb9fbeb4e7a0fbb37d820f8b82232d
# Lint
1795070c67cd08f5c5437b4e0d3ae6e5d3e5fd13