# Git - Basic

Git is a software tool based on distributed version control system, that is installed in an individual machine to maintain the local repositories as well as push/pull changes from/to the central repositories.

It is a distributed, open-source version control system.

It enables developers and data scientists to track code, merge changes and revert to older versions. It allows you to sync changes with a remote server. Due to its flexibility and popularity, Git has become an industry standard as it supports almost all development environments, command-line tools, and operating systems.

<mark>Git Configuration Levels:</mark>

`1. --system`  
`git config --system --list`  
`Config File:- cat /etc/gitconfig`

`2. --global`  
`git config --global --list`  
`Config File:- cat ~/.gitconfig`

`3. --local`  
`git config --local --list`  
`Config File:- cat .git/config`

**<mark>Note:</mark>** Local overrides Global and Global overrides System Level.

**<mark>Three stages of Git-:</mark>**

i) **Working directory/Workspace:** The folder where you work/save all the work files. Inside this folder, you'll have to initialize the local repo:  
`git init`  
After initializing, add files to the staging area by:  
`git add <Filename>`

ii) **Staging area:** It acts like a caching zone or temporary storage of files before pushing them to a local repo. To send changes to the local repo:  
`git commit -m "your message here"`

iii) **Local Repository:** It maintains the versions. `git push` and `git pull` commands can then be used to push/pull code from a central repository like GitHub, GitLab, or Bitbucket...

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682276933510/8720022a-265c-4bcb-bc90-f827ca4812a2.png align="center")

**<mark>Helpful URLs:</mark>**  
Install on Windows: [https://git-scm.com/download/win](https://git-scm.com/download/win)  
`Git CheatSheet:-` [https://gist.github.com/LondheShubham153/0b367734a02e8cf77e6ea1dc90ee0f38](https://gist.github.com/LondheShubham153/0b367734a02e8cf77e6ea1dc90ee0f38)  
[https://education.github.com/git-cheat-sheet-education.pdf](https://education.github.com/git-cheat-sheet-education.pdf) [https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet](https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet)

### <mark>Working with Git:-</mark>

1. Connect to EC2 Instance \[Verify if Git is installed, if not install it\]
    
    `sudo -s pwd yum list git //List Available Packages`  
    `git --version // Check the Git Version`  
    `yum install git -y // If the above cmd, has not shown the version, install it...`
    
    You can also install Git Bash on your Operating system, you can download the App from here: [https://git-scm.com/downloads](https://git-scm.com/downloads)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682179050895/245ac946-4ee3-476a-bbea-aad3e75366b4.png align="center")
    
2. Create a Directory and file where you want to work/save all the work files.  
    `mkdir devops-git-first && cd devops-git-first && echo "First GitHub File..." > github.txt && pwd && ls -lha`
    
3. Initialize the local repo \[Inside this folder, we create eariler\]  
    This will create a hidden directory that stores all metadata required for it to function.  
    `git init`
    
4. Configure the username and user email to identify who made the changes:  
    `git config --global` [`user.name`](http://user.name) `"<Username>"`  
    `git config --global` [`user.email`](http://user.email) `"<your_email_here>"`
    
    <mark>Note:</mark> [`user.name`](http://user.name) `and` [`user.email`](http://user.email) `settings need to be configured for all Commits. So it means, you can't commit, until you have configured your Username and Email.`
    
    `User settings in GIT are mainly defined at Global Level because they correspond to user-level settings for all repositories.`
    
5. To list created users:  
    `git config --list`
    
6. Check git status:  
    `git status`
    
7. Take your file from untrack to the staging area
    
    *(For single file)*  
    `git add <File-name>`
    
    *(All files)*  
    `git add .`
    
    Check git status to verify the changes:  
    `git status`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682182321518/aabec43f-5c3e-4620-b14d-29d0ae02ceb7.png align="center")
    
8. Commit file to the local repo:
    
    `git commit -m "Your message here"`
    
9. Check the git log:  
    *Detail log*  
    `git log`
    
    *Summary logs*  
    `git log --oneline`
    
10. To show what's in the specific commit:  
    `git show <commit_id>`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682182397542/dbb3aa9a-572e-4b7a-b2c1-d17941802c7c.png align="center")
    
11. Restore the file, once it gets deleted from the File System:  
    `ls -lha; rm -fv <Filename>`  
    `git status`  
    `git restore <Filename> // Restore the file from Repo`  
    `git status`  
    `git log`
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682180223073/2cbb22ca-37cd-4ada-9570-8420cb2ec68e.png align="center")

**<mark>Example:</mark>**  
Let's create Repo, Add a file to it, and Commit:  
`mkdir gitdirectory`  
`git init gitdirectory ; cd gitdirectory; touch file1.txt ;`  
`git status ; git add file1.txt ; git commit -m "First Blank File"`  
`git status; git log --oneline`

**<mark>Git Branches:</mark>** Branches are used for **parallel development**. There is no restriction on the number of branches that can be created. It is recommended that each task is done in a separate branch and then merged after the successful completion of the task. Any new file is visible to all the branches unless committed to a specific branch.

Branching is a **separate workspace**, usually created when you want to experiment or test something without impacting the main code base, it is also used for  
\- Manage Releases  
\- Create Hotfixes for production branches.

In short, A branch is a method of requesting a new working directory and staging area.

**Note:** A Git branch is just a pointer to the commits, it does not change the contents of the repository.

* To see all the branches in the system:  
    `git branch`
    
* To create a new branch:  
    `git branch <branch name>`
    
* To switch to another branch:  
    `git checkout <branch name>`
    
* Merges Branches  
    `git merge <branch name>`
    
* Create and checkout/switch to a new branch named.  
    Drop the -b flag to checkout an existing branch  
    `git checkout -b <branch name>`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682182760944/45c1aafe-c300-486a-99ec-09c4005408b3.png align="center")
    
* See example, when we switch to another branch, the files created on the previous branch will not be available.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682185268062/d861664d-8320-4356-9507-f10ad42d2042.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682185407949/a2e3fdaa-a817-4dc8-bceb-db9b46b687f1.png align="center")

**<mark>Notes:</mark>**  
**\-** Master/main is the default branch, which is created by default when a repository is initiated using `git init` cmd.  
\- `*` :- Represend as Active Branch  
\- When a new branch is created, the complete commit history of the master branch is replicated.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682277982821/b469f030-8555-4f19-9025-fd75f5b4ed88.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682666957630/86f466a2-915a-4f0d-8892-8d2b91a6bae7.png align="center")

`git diff --staged HEAD // This indicates the changes in the last commit`  
`git diff --staged HEAD~ // To refer to the second last commit`  
`git diff <Commit-ID>`

**\- Revert to Earlier Commits in GIT**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682668697223/4af7dbb9-1f78-4044-89a8-f2b33bbd59c9.png align="center")

<mark>Demo 1:</mark>  
`git log --oneline`  
`$ cat file1.txt`  
`Line 1`  
`Line 2`  
`Line 3`  
`Line 4`

**Use the below cmd to Revert to the Earlier Stage**  
`$ git checkout HEAD~1 file1.txt`  
`Or`  
`git checkout <Commit ID> <File-Name>`  
`HEAD~1 :- indiadates the recent Commit 2`

Check the file which now shows you the previous version's contents  
`cat file1.txt`

The status will show the file needs to be committed  
`git status`  
`git commit -m "Reverting the Changes, by removing the Line 4 by reverting"`  
`git status`  
`git log --oneline`

* **Deleting Files from Staging Area and Working Directory**  
    `git rm <File-name>`
    
* Deleting only from the Staging Area  
    `git rm --cached <File-Name>`
    
* **Check how many files are staged**  
    `git ls-files --staged`
    
    **<mark>Note: -</mark>** Even if the file is deleted, its commit history is still present in the repository. If you want you can revert the deletion it can be done using:  
    `git checkout <Commit ID/Version> <File-Name>`
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682670263045/bb39642d-a850-4fe5-9f66-ec38e4cf8928.png align="center")
    
    * **Ignore Files in Git using**`.gitignore` file, It:-  
        `// Stores File Name`  
        `// Stores File Pattern`
        
        <mark>-f flag can be used to ignore the.</mark>`gitignore` <mark>file</mark>  
        i.e:- `git add -f <file name>`
        
        `cat .gitignore`  
        `*.txt`  
        `touch {1..10}.txt`  
        `ls`  
        `git status`  
        `git add .`  
        `git commit -m "Trying to commit"`
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682670684702/184b833a-1887-43e9-8e73-b16396aca1f0.png align="center")
        
        * **Rename file in Git**  
            `git mv <File name> <New file name>`  
            `git add <File name>`  
            `git commit -m "Message"`  
            `git status`  
            `git log --oneline`
