Version Control System

Basics

Configuring Git

# Setting configuration globally
git config --global user.name "Rohit Dhill"
git config --global user.email "[email protected]"
git config --global core.editor "nvim"

# On Windows, end of lines are marked as \\r\\n and on Linux/ macOS \\n and this can become problematic when we have multiple collaborators using different different platforms
# On Windows, we want Git to change all end of lines to be converted into CRLF when pulling the code, and convert it back to LF when pushing the code
# On Linux, we don't want it to do anything, but change CRLF to LF if it occurs by mistake
# 
git config --global core.autocrlf true  # Window
git config --global core.autocrlf input # Linux/ macOS

Categories of Version Control Systems

<aside> 👉 Centralized

</aside>

<aside> 👉 Distributed

</aside>

Untitled

Initializing a repository

**learngit@s4dr0t1:~/git/snapshots$** git init
Initialized empty Git repository in /home/learngit/git/snapshots/.git/L

Staging area

What is it?

Add files to the staging area

# Add specific files to the staging area
git add file1 file2 file3

# Add all the files recursively
git add .

Show all files present in the staging area

git ls-files

Check the repository status

Untracked files means the files which are not in the staging area yet, and as a result are not yet being tracked by Git.

**learngit@blackhawk**:~/git/snapshots$   git status [--short / -s]
**XY**  .gitignore
**XY**  bin/app.bin
||
| \\->Represents the working directory
-> Represents the staging area
**learngit@blackhawk**:~/git/snapshots$ git status
On branch master
No commits yet

Untracked files:  
  (use "git add <file>..." to include in what will be committed)
        file1.txt
        file2.txt

nothing added to commit but untracked files present (use "git add" to track)

gitignore

github/gitignore

Remove files from the staging area

# Remove the file from the disk and then update this information to the staging area
rm file && git add file

# Directly remove the files from the disk and update this information to the staging area
git rm file2 [-rf]

# Removing files only from the staging area
git rm --cached file2 [-rf] 

# Remove all the untracked files from the directory
git clean

Restore files from the staging area

# git takes restores the copy from the next environment
# If want to restore the file in the staging area, the copy will be taken from the last commit
# If want to restore the file in the working directory, the copy will be taken from the staging area
git restore <file>            # Restore from the staging area into the working directory
git restore --staging <file>  # Restore from the last snapshot into the staging area

# Restoring a file from a particular commit
git restore --source=HEAD~offset fullPath/file
git restore --source=commitIdentifier fullPath/file

# By the method of checking out
git log --oneline -- file        # Find the commit from which we want to restore from
git checkout <goodCommit> file   # Check out to the said commit
git commit -m "Restore the file" # Self explanatory

Move/ rename the files

# Rename the file and update the staging area for the deleted and new file
mv oldName newName
git add oldName
git add newName

# Doing the aforementioned thing all at once
git mv oldName newName

Diffing

# Check the difference b/w the file in our directory and the stagged file
git diff[tool]

# Show different b/w what's in our staging area and the last snapshot
git diff[tool] --staged

# Showing differences of a file b/w two commits 
git diff commitID commitID

Committing to a repository

Basics

# Bsasic workflow
git commit -m "Meaningful message for our commit"

# Skip the staging area and commit all the changes directly
git commit -am "Fix xyz bug"

# Spawn up $EDITOR to write a larger commit message
git commit

# List all the commit authors and their commits
git shortlog

Directly commit the changes without adding anything to the staging area

# Directly commit all the staged and unstaged changes at once
git commit -a -m "Fix the bug"

Blaming

We can find out who has written a bad piece of code and basically blame them for the same.

git blame file [additionalFlags]

Tagging commits

We can create tags for certain commits and we will be able to reference those commits using the tag

git tag <tag> <commitID>

# Apart from tagging commits, we can also associate a particular message with its corresponding tag (aka Annotation Tag)
git tag -a <tag> -m "message"

# Check all the tags 
git tag

# Check all the tags and their associated message/ object
git tag -n

Good committing practices

Browsing history