# Git and Github Day-1

### **Linux Commands**

*Useful commands for managing files and navigating the system*

#### **Basic File Operations**

* `ls`  
    List directory contents.
    
* `cd [directory]`  
    Change directory.
    
* `pwd`  
    Print the working directory (current path).
    
* `touch [file]`  
    Create a new, empty file.
    
* `mkdir [directory]`  
    Create a new directory.
    
* `rm [file]`  
    Remove a file.
    
* `rm -r [directory]`  
    Remove a directory and its contents.
    
* `mv [source] [destination]`  
    Move or rename a file or directory.
    
* `cp [source] [destination]`  
    Copy a file or directory.
    

### **Git Cheat Sheet with Linux Commands**

#### **Stage & Snapshot**

*Working with snapshots and the Git staging area*

* `git status`  
    Show modified files in the working directory and staged for your next commit.
    
* `git add [file]`  
    Add a file as it looks now to your next commit (stage it).
    
* `git reset [file]`  
    Unstage a file while retaining the changes in your working directory.
    
* `git diff`  
    Show changes that are not staged.
    
* `git diff --staged`  
    Show changes that are staged but not committed.
    
* `git commit -m "[message]"`  
    Commit your staged content as a new snapshot with a descriptive message.
    

---

#### **Setup**

*Configuring user information used across all local repositories*

* `git config --global` [`user.name`](http://user.name) `"[Name]"`  
    Set your name that will be associated with commits.
    
* `git config --global` [`user.email`](http://user.email) `"[Email]"`  
    Set the email address that will be associated with your commits.
    
* `git config --global color.ui auto`  
    Enable automatic command-line coloring for easier reading.
    

---

#### **Setup & Init**

*Configuring user info, initializing and cloning repositories*

* `git init`  
    Initialize an existing directory as a Git repository.
    
* `git clone [url]`  
    Clone a repository from a remote URL.
    

---

#### **Branch & Merge**

*Isolating work in branches, changing context, and merging changes*

* `git branch`  
    List your branches. A `*` will appear next to the currently active branch.
    
* `git branch [branch-name]`  
    Create a new branch from the current commit.
    
* `git checkout [branch-name]`  
    Switch to another branch and update your working directory.
    
* `git merge [branch]`  
    Merge the specified branch’s history into the current branch.
    
* `git log`  
    Show the commit history of the current branch.
    

---

#### **Share & Update**

*Retrieving updates from a repository and updating local repositories*

* `git remote add [alias] [url]`  
    Add a remote URL with an alias.
    
* `git fetch [alias]`  
    Fetch branches from a remote repository.
    
* `git merge [alias]/[branch]`  
    Merge a remote branch into your current branch.
    
* `git push [alias] [branch]`  
    Push commits from your local branch to the remote repository.
    
* `git pull`  
    Fetch and merge changes from the remote repository to your local branch.
    

---

#### **Tracking Path Changes**

*Versioning file removals and path changes*

* `git rm [file]`  
    Delete a file from the project and stage the removal.
    
* `git mv [old-path] [new-path]`  
    Rename or move a file and stage the change.
    
* `git log --stat -M`  
    Show commit logs with details of moved or renamed files.
    

---

#### **Temporary Commits**

*Temporarily store changes and switch branches*

* `git stash`  
    Save modified and staged changes without committing them.
    
* `git stash list`  
    List all stashed changes.
    
* `git stash pop`  
    Apply the latest stashed changes.
    
* `git stash drop`  
    Discard the most recent stashed changes.
    

---

#### **Rewrite History**

*Rewriting commits, updating branches, and clearing history*

* `git rebase [branch]`  
    Reapply commits of the current branch on top of another branch.
    
* `git reset --hard [commit]`  
    Clear the staging area and rewrite the working directory from the specified commit.
    

---

#### **Inspect & Compare**

*Examining logs, diffs, and object information*

* `git log`  
    Show commit history for the active branch.
    
* `git log [branchB]..[branchA]`  
    Show commits on `branchA` that are not on `branchB`.
    
* `git log --follow [file]`  
    Show the commit history for a file, even across renames.
    
* `git diff [branchB]...[branchA]`  
    Show the difference between `branchA` and `branchB`.
    
* `git show [SHA]`  
    Show any Git object (commit, tag, etc.) in a human-readable format.
    

---

#### **Ignoring Patterns**

*Preventing unintentional staging or committing of files*

* `git config --global core.excludesfile [file]`  
    Set a global ignore pattern for all repositories.
    
* `.gitignore`  
    Use a `.gitignore` file to specify files and patterns to ignore, such as:
    
    ```python
    pgsqlCopyEditlogs/
    *.log
    temp*/
    ```
    

---
