# Git and GitHub

### **Source code management**

Source code management (SCM) is used to track modifications to a source code repository. SCM tracks a running history of changes to a code base and helps resolve conflicts when merging updates from multiple contributors. SCM is also synonymous with Version control. 

Source code management has two types:

1. CVCS: Centralized Version Control System
    
2. DVCS: Distributed Version Control System
    

### Centralized Version Control System

* It is not locally available, meaning we have always needed to connected to the network to perform any action.
    
* Since everything is centralized, if central server get failed you will loose entire data
    
* Example: Concurrent Versions System (CVS), Perforce, and Subversion (SVN)
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700581990704/f2661198-362f-4384-a7f4-0b7ee9d9a0f6.png align="center")
    

### Distributed Version Control System

* In DVCS, every contributor has a local copy or clone of main repository i.e everyone maintains a local repository of their own, which contains all the file and metadata present in the main repository.
    
* Example: Git
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700582635070/01fc5035-e8eb-41c8-aff2-331e47edf08c.png align="center")
    

### What is git?

Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development.

### What is gitHub?

GitHub is a platform and cloud-based service for software development and version control, allowing developers to store and manage their code.

### Git three-stage architecture

![3 Git – Three stage architecture - CodeTej](https://codetej.in/wp-content/uploads/2021/05/3-stage-architecture-of-GIt-1024x576.png align="left")

### Some important Git command

* To show all branches **git branch**
    
* Create a new branch **git branch &lt;branch\_name&gt;**
    
* Command for branch merge **git merge &lt;branch\_name&gt;**
    
* **git log --merge** command helps to produce the list of commits that are causing the conflict
    
* **git diff** command helps to identify the differences between the state's repositories or file
    
* **git checkout** is used to undo the changes made to the file, or for changing branches
    
* **git reset --mixed** is used to undo changes to the working directory and staging area
    
* **git merge --abort** helps in exciting the merge process and returning back to the state before the merging began
    
* **git reset** command is used at the time of merge conflict to reset the conflicted files to their original state
    
* Set global username and email for Git (Locally).
    
    ```plaintext
    git config --global user.name "<your username>"
    git config --global user.email "<your email>"
    ```
    
* Initialise an empty Git Repository
    
    ```plaintext
    git init
    ```
    
* Clone an existing Git Repository
    
    ```plaintext
    git clone <repository URL>
    ```
    
* Add file/stage to git
    
* ```plaintext
    git add <filename>
    ```
    
* Add all the files to git
    
* ```plaintext
    git add .
    ```
    
* Commit all the staged files to git
    
* ```plaintext
    git commit -m "<your commit message>"
    ```
    
* Restore the file from being modified to Tracked
    
* ```plaintext
    git restore <filename>
    git checkout <filename>
    ```
    
* Show the status of your Git respository
    
* ```plaintext
    git status
    ```
    
* Show the branches of your git repository
    
* ```plaintext
    git branch
    ```
    
* Checkout to a new branch
    
* ```plaintext
    git checkout -b <branch name>
    ```
    
* Checkout to an existing branch
    
* ```plaintext
    git checkout <branch name>
    ```
    
* Remove a branch from Git
    
* ```plaintext
    git branch -d <branch name>
    ```
    
* Show remote origin URL
    
* ```plaintext
    git remote -v
    ```
    
* Add remote origin URL
    
* ```plaintext
    git remote add origin <your remote git URL>
    ```
    
* Remove remote origin URL
    
* ```plaintext
    git remote remove origin 
    ```
    
* Fetch all the remote branches
    
* ```plaintext
    git fetch
    ```
    
* Push your local changes to remote branch
    
* ```plaintext
    git push origin <branch name>
    ```
    
* Pull your remote changes to local branch
    
* ```plaintext
    git pull origin <branch name>
    ```
    
* Check you git commits and logs
    
* ```plaintext
    git log
    ```
    

### What is cherry picking in git?

Cherry picking is **the act of picking a commit from a branch and applying it to another**. git cherry-pick can be useful for undoing changes. For example, say a commit is accidently made to the wrong branch. You can switch to the correct branch and cherry-pick the commit to where it should belong.

### Git stashing

`git stash` temporarily shelves (or *stashes*) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.

### Commands related to stashing

* To stash an item **git stash**
    
* To see stashed item list **git stash list**
    
* To apply stashed item **git stash apply stash@{&lt;list number&gt;}**
    
* To clear the stash items **git stash clear**
    

Happy Learning
