Git is a distributed version-control system for files.
Below is a concise cheat sheet for the core commands: initializing a repository, configuring it, working with the index and commits, and rolling back changes.
git init
The git init command initializes a new git repository.
git config
The git config command lets you change various repository settings, both local and global.
The available settings are: user.name — the name of the user the changes will be attributed to; user.email — the email of the user the changes will be attributed to.
Let's add these settings to the repository:
git config user.name "User Name"
git config user.email "user@mail.com"You can see the changes you've made in the repository's config file:
cat .git/configThe result will look like this:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = User Name
email = user@mail.comSettings are split into several levels: system — the machine-wide level with default settings (/etc/gitconfig); global — the user level (~/.gitconfig); local — the repository level (<project>/.git/config).
To change the settings at each level you use the corresponding keys: --system, --global and --local (used by default).
In the hierarchy, settings are first looked up at the local level. If they are not there, the user level is checked, and if they are missing there too, the system level is used — with the default settings made when git was installed.
The --list key lets you check the current parameter values. Let's run the command:
git config --list --localOutput:
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=User Name
user.email=user@mail.comAnd the same command for the global level:
git config --list --globalOutput:
user.name=another_user
user.email=another@gmail.com
core.autocrlf=inputgit status and git add
git status checks the state of the repository. The command shows the current branch, the list of new files not added to the index, and the list of changed and staged files.
git add adds a file to the git index. The -p key lets you make a partial commit: if a single file has several changes, git will ask for each one whether to apply it.
git commit, git show and git reset
git commit records the changes from the index into the repository. The -m key lets you add a description of the commit in quotes.
git show displays information about a commit: which files were changed, the changes themselves, who made the commit and when, and other information. Without a parameter the command shows information about the last commit; if you add a commit id, it shows information about that commit.
git reset lets you roll back changes.