The Art of Mastering Git
Tags: react, reactnative, git, githubactions
Published: 2023-01-25 13:14:26

The first thing to learn about Github, is to learn that Git and Github are different. Git is a version control system that helps you maintain a track record of changes made which you cannot normally do in a USB or an SSD while GitHub is a GUI that holds all the code. Git is often ignored by most Software Engineers but they have to learn it the hard way because there is no choice left. I’ve articulated all of the major points you need

## The Four Areas: Basic Workflow

The basic workflow of Git involves four main areas: the working directory, the staging area, the local repository, and the remote repository.The working directory is where you make changes to files.The staging area is where you stage changes before committing them to the local repository.

The local repository is where you commit changes and create branches and tags.The remote repository is where you push and pull changes to share them with others.

To start working with git, you need to initialize a repository in the directory where you want to start tracking files using git init. Then you need to add files to the staging area by using the git add command. Once you're ready to save your changes, you can commit them to the local repository using git commit and you can push the changes to the remote repository using git push.

## The Four Areas: Advanced Tools

In addition to the basic workflow of Git, there are several advanced tools and features that can help you manage and collaborate on projects more effectively. Some of these include:

**Branching:** Git allows you to create multiple branches of a project, which allows you to work on different features or bug fixes simultaneously without affecting the main branch.

**Merging:** When you've finished working on a branch, you can merge it back into the main branch, bringing in all of your changes.

**Rebasing:** This allows you to reapply commits on top of another base tip. It's useful when you need to integrate changes from one branch into another, but you don't want to create a merge commit.

**Tagging:** Git allows you to create tags to mark specific points in the history of a project, such as release versions.

**Stash:** Git stash allows you to save your changes temporarily, and you can use it when you want to switch branches or work on a different task without committing the changes.

**Git Hooks:** Git allows you to run scripts before and after git commands. This can be useful for automating tasks such as testing, building, or deploying code.

**Git aliases:** Git allows you to create custom commands by using git aliases, it's useful for creating custom commands and making your work easier.

These are just a few examples of the advanced tools and features that Git has to offer. With a little practice and experimentation, you'll be able to take full advantage of Git's capabilities to manage and collaborate on projects more efficiently.

## Stashing Data

Stashing data in Git is a way to temporarily save changes that you have made to your working directory, but do not want to commit yet. This allows you to switch branches or work on a different task without committing the changes, and then later come back and re-apply the changes.


When you use the git stash command, Git will save the current state of your working directory and the staging area to a new stash. The changes are not committed to the local repository and are not visible in any branches. Instead, they are saved in a separate stash. You can later apply the changes from a stash by using the git stash apply or git stash pop command.


Stashing data can be useful in situations where you're in the middle of working on something, but need to switch to a different branch to work on something else. It can also be useful for keeping your commits clean and organized, by allowing you to commit only the changes that are ready to be committed.

Additionally, you can create multiple stash and work on different stashes independently, and you can use git stash list to list all stashes and git stash drop to delete a specific stash.


## Solving Conflicts

Solving conflicts in Git occurs when two branches have made changes to the same lines of code in a file and those changes cannot be automatically merged. This can happen when two people are working on the same file at the same time, or when you're trying to merge one branch into another.

When a conflict occurs, Git will mark the conflicted lines in the file with special conflict markers, such as 

```
"<<<<<​1 Malformed citation <<" and ">>>>>>>

```

You will need to manually resolve the conflict by editing the file and removing the conflict markers, along with any unnecessary code. Once you have resolved the conflict, you will need to add the file and commit the changes.

To solve conflicts, you can use the command git merge or git pull and git will automatically identify the conflicts and mark them in the files.

You can also use Git's visual merge and conflict resolution tools, such as GitKraken, SourceTree, or Git Extensions. These tools provide an interactive interface that allows you to easily view and resolve conflicts, and they can be a great help when you're dealing with complex merge scenarios.

It's important to remember that conflicts are an inherent part of working with Git, and that it's normal for them to occur from time to time. With practice and experience, you'll become more proficient at resolving conflicts and minimizing the time and effort required to do so.


## Working with Paths

Working with paths in Git refers to specifying the location of files or directories within a repository. Git allows you to work with paths in several ways:

**Absolute Paths:** An absolute path is the full path from the root of the file system to a file or directory. For example, '/home/user/project/file.txt' is an absolute path.

**Relative Paths:** A relative path is the path to a file or directory relative to the current directory. For example, './file.txt' is a relative path that refers to a file called 'file.txt' in the current directory.

**Wildcard Paths:** Wildcard paths allow you to specify a pattern for matching multiple files or directories. For example, '*.txt' will match all files with the .txt extension in the current directory.

**Regular Expressions:** Git supports using regular expressions to match file paths. For example, .*\.txt$ matches all files that end with .txt in the current directory.

## Committing Parts of a File

Committing parts of a file in Git refers to the process of selectively staging and committing only specific changes within a file, rather than committing the entire file. This can be useful when you want to make multiple commits with different changes, or when you want to commit only a specific change and leave other changes for later.

There are several ways to commit parts of a file in Git:

**git add -p (or git add --patch) :** This command allows you to interactively stage and commit specific changes within a file. It will prompt you to select which changes to stage, and you can choose to stage a specific hunk of changes, or even split it into multiple hunks.

**git add -i (or git add --interactive):** This command also allows you to interactively stage and commit specific changes within a file, but with a different interface. It presents a list of options to choose from such as: stage this hunk, unstage this hunk, etc.

**git gui :** This command opens the Git GUI, a graphical user interface for Git. It allows you to visually select which changes to stage and commit.

**git difftool :** This command opens a diff tool that allows you to visually compare the changes made to the file, and you can select which changes to stage and commit.

Once you've selected the specific changes to stage, you can use the git commit command as usual to commit the changes.

## Becoming a History Surgeon, Tracking Changes in History and Browsing the Log

## How to become a Git Heart Surgeon? 

Becoming a history surgeon in Git refers to the process of manipulating the commit history of a repository to make it more readable, understandable, and maintainable. This can be useful for cleaning up messy commits, squashing multiple commits into one, reordering commits, and more.

There are several Git commands that can help you with this:

**git log:** This command allows you to view the commit history of a repository, including the author, date, and commit message of each commit.

**git blame:** This command allows you to view the last person who modified each line of a file, and the commit in which the modification occurred.

**git diff:** This command allows you to view the differences between different commits, branches, or files.

**git rebase:** This command allows you to reorder, edit, or squash commits in the commit history.

**git revert :** This command allows you to undo a specific commit and create a new commit that undoes the changes made by the previous commit.

**git cherry-pick :** This command allows you to pick a specific commit from one branch and apply it to another branch.

**git reset :** This command allows you to reset your local repository to a specific commit, discarding any commits that were made after it.

Tracking changes in history is a fundamental concept in Git, it allows you to keep track of the changes made to the files and the evolution of the project. By browsing the log, you can see who made changes, when they were made, and why they were made. This information can be useful for troubleshooting issues and understanding the development of the project.

It's important to note that manipulating the commit history can be dangerous, because it can cause conflicts and errors if not done correctly. It's a good practice to make a backup of your repository before attempting to manipulate the history, and to test the changes in a separate branch before applying them to the main branch.