basics and setup#
git --version=git -v- get currently installed versiongit status- see statusgit init- initialize a repository in the current foldergit config- change configuration file, add--globalfor editing the global configgit add <file_name>- move the specified file to the staging areagit commit -m "<message>"- commiting the files currently in the staging area, if-mis not used then text editor will open for message
.git folder#
.git folder is created in every repository. It contains data necessary for the git to operate. Some of the more interesting things:
HEAD- pointer file indicating the ‘state’ currently loaded (branch, commit, etc.)hooks- folder which specifies scripts to be executed at specific moments (before commit, before push, etc.)config- INI file containing LOCAL configuration of the repository
Areas#
In git projects there are typically 4 areas:
- working dir - the actual folder we’re working on, where
git initwas called - staging area - files after calling
git addbut beforegit commitgo there - repository - files go there after
git committhose are saved in log history and can be viewed - remote - optional cloud platform like Github or Gitlab
git log#
Command for displaying the commit history. Some useful flags include:
--oneline- for truncated output--graph- for visual representation of branches--all- display commit history of all branches, not just the ones reachable from the current HEAD
Configuration files#
/etc/gitconfig #system wide rarerly touched
~/.gitconfig # global standard
<repo>/.git/config # local per repositoryconfig file looks like this:
[user]
name = Your Name
email = your@email.com
[core]
editor = vim
[alias]
co = checkout
br = branch
st = status
[push]
default = simpleyou can either change the file manually or use git config command for example:
git config --global user.name "<my_name>"
Aliases are shortcuts for git commands that can be set in the config file.
.gitignore#
Special file for ignoring certain files from being added to the repository.
Example structure:
# ignore normal file
example.txt
# ignore the entire folder
test_ignore/
# ignore another file
.env.gitignore filesYou can have multiple .gitignore files all of which have specified relative path. This makes it easier since you don’t need to go back to the file in the root directory and specify the whole path.
[https://mrkandreev.name/snippets/gitignore-generator/](.gitignore generator) - useful, you specify the technologies used and it generates commonly ignored files, useful as a starting point.
branches#
git branch- list branches and show the currently used onegit branch <new_branch>- creates new branch with a specified namegit checkout <branch_name>- switches to the specified branch if it doesn’t exist it creates onegit checkout <commit id>- go to a commit specifiedgit switch- similar to git checkout but doesn’t create new branch by defaultbranch merge <branch_name>- merges the specified branch INTO the currently used onegit rebase <branch_name>- combines info from a branch by putting it’s commits on top of the branch specified, mostly used for keeping history clean
Merge conflicts#
When merging branches merge conflicts can arise, then a text editor will open and you have to manually fix it.
It looks like this:
<<<< HEAD
line 1
line 2
=========
line 3
line 4
>>>>> "<branch name>"You need to resolve it - keep one verion, both or parts of both and save it with:
git add ...git commit ...
git diff#
Compares the state of a file in different versions. Shouldn’t be relied on too heavily.
--- - indicate file1
+++ - indicate file2
Some examples:
--staged- compares the differences between what is currently staged and not<commit id 1> <commit id 2>- compares the state of files in different commits<branch 1> <branch 2>- compares the state of files in different branches
git stash#
Saves the current state without commiting.
git stash- save the current stategit stash pop- load the last stashed stategit stash list- see stashed statesgit stash apply <stash_id>- apply the specific stash
The stashed files are not branch specific. You can call git stash on one branch and git stash pop on another.
Remote#
git remote- used for connecting local repository to the cloud servicegit remote -v- check the connectiongit remote add <remote_name> <repository_url>- connects the local repo to the cloud, typically<remote_name>isorigingit push <remote_name> <branch>- pushes branch onto the remote with specified namegit push -u <remote_name> <branch>- sets the remote as upstream making it “default” and allowing use of justgit pushgit clone <repo_url>- copies the repository with the specified url and establishes it as upstream
Fetch and pull#
Fetching is useful if data on branch has been changed and we’re not sure if merging is reasonable at the moment.
git fetch- downloads data from the remote repo onto the machine intoorigin/<branch_name>NOT ON<branch_name>. It’s put into the staging area but not into the working directory
After git fetch we often inspect the downloaded files with commands such as:
git log <branch_name>..origin/<branch_name>- shows commits reachable fromorigin/<branch_name>but not from<branch_name>- meaning shows the commits that the branch on cloud repo is ahead bygit diff <branch_name> origin/<branch_name>- compare local changes and the ones downloadedgit checkout origin/<branch_name>- temporarily switch to the downloaded branch without commiting what you have locally