Git Notes

Version Control : To keep track of changes made to our files.

Working with Git

Initializing Git

git config --global user.email "myemail@main.com"
git config --global user.name "my name"

Git Tracking System

Pasted image 20240603173742.png

When working with Git, you take snapshots of your project at different points in time. These snapshots represent the history of your project and are stored in the Git repository (directory).

  • Each time of commit, you take a snapshot of your project.

  • These snapshots represents the history of your project and includes in git directory.

  • Within a git directory, there are two types of files:

    1. Tracked File: These are the files that Git actively monitors for changes. When you modify a tracked file (e.g., by adding, modifying, or deleting content), Git recognizes these changes.
    2. Untracked File: Untracked files are not yet part of Git’s version control system. They don’t get included in snapshots until you explicitly add them to the staging area.
  • We can initialize a git repo by using 'git init' command.

Three Main Stages of Tracked Files

  1. Modified : A file is considered modified when you make changes to it. These changes are not yet staged for commit.
  2. Stage: After modifying a file, you can stage it for the next commit.- Staging means that the changes are ready to be included in the next snapshot.
  3. Committed: When you commit your changes, Git safely stores them in its database. The committed changes become part of the project’s history.

A Good Commit Message :

A Short Description
--Empty Line--
Approximately 50 charter summary. Can be add links. 
Below, we discuss two main topics.
  1. Work Locally with Git
  2. Deal with Working Tree (Or with Commits and Branches)

Work Locally with Git

Usual Git Operations:

git stages.png

Commands for check current status and log

git status   : 
git log      : show the previous changes
git log -p   : show changes with differences
git log --stat : statics about changes
git show 
git diff     : show the changes (unstage)
git diff --stage: show the changes (stage)



Commands for File Handling

git rm <file name> : Delete a file
git mv <Initial name> <Final name> : Rename a file


Dealing with Working Tree

Branch

A Pointer to a particular commit. The default branch name initially created os 'master'.

Simply put, a branch in Git is like a new folder. However, unlike traditional folders, Git branches offer several benefits. Most importantly, we can automatically merge changes from a working branch into the main branch. This allows us to experiment with the project without affecting the main project files. If the experiment provides positive results, we can automatically incorporate those changes or new features into the main project.

git branch : See Available Branches
git branch <new-branch-name> : Create a new branch
git checkout <branch-name> : Select a branch
git checkout -b <new-branch-name> : Create and Select a branch
git merge <branch-name> : Merge a branch with currently selected branch

Pasted image 20240603173935.png

Other Useful commands for work with branches
git branch -d <branch-name> : delete a branch
git log --graph --oneline
Merging

Git uses two different algorithms to perform a merge.

  1. Fast-Forward
  2. Three-way merge
  • Merge Conflict: Occur when change same files in both merging branches.