---
# System prepended metadata

title: Git Practice
tags: [' git', CoderSchool, ' github']

---

---
title: Git Practice
tags: CoderSchool, git, github
---
![](https://i.imgur.com/0AUxkXt.png)

# Git Practice

<p style='color:#95a5a6;font-weight:bold'>Easy - Common</p>
<p style='color:#2980b9;font-weight:bold'>Beginner - Rare </p>
<p style='color:#8e44ad;font-weight:bold'>Intermediate - Epic</p>
<p style='color:#e74c3c;font-weight:bold'>Advanced - Legendary</p>


Scenarios or problem statements:
1. <p style='color:#95a5a6;font-weight:bold'>How to create an Github repository?</p>
* Create a new repository on the command line:
```bash=
echo "# project_name" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/link_to_github_repo
git push -u origin master
```
* Push an existing repository from the command line:
```bash=
git remote add origin https://github.com/link_to_github_repo
git push -u origin master
```
2. <p style='color:#95a5a6;font-weight:bold'>How to push your code to Github repo?</p>
```bash=
git push -u origin <branchName>
```

3. <p style='color:#95a5a6;font-weight:bold'> What is staging, commit?</p>

STAGING sets selected file in directory to tracked status and ready to commit. 
COMMITTING creates a snapshot of your directory at a particular moment 

4.  <p style='color:#95a5a6;font-weight:bold'> How to get code from repo?</p>
```bash=
git pull origin branch_name
# or
git fetch origin branch_name
git merge branch_name
```
5.  <p style='color:#95a5a6;font-weight:bold'> How to create a branch?</p>
```bash=
git branch <newBranch>
# or create and switch to the new branch
git checkout -b <newBranch>
```
6.  <p style='color:#95a5a6;font-weight:bold'> How to switch to another branch?</p>
```bash=
git checkout <nameBranch>
```
7.  <p style='color:#95a5a6;font-weight:bold'> How to merge?</p>
```bash=
git checkout <nameBranch>
git merge <anotherBranchname>
```
8.  <p style='color:#95a5a6;font-weight:bold'> How to rebase?</p>
```bash=
# 1 checkout to the branch that you want to rebase
git checkout <nameBranch>
# 2 select the location you want to rebase.
git rebase <nameBranch or commit hash> 
```
9.  <p style='color:#8e44ad;font-weight:bold'>What is difference between merge and rebase?</p>

`git merge` apply all unique commits from branch A into branch B in one commit with final result

`git merge` doesn’t rewrite commit history, just adds one new commit

`git rebase` gets all unique commits from both branches and applies them one by one

`git rebase` rewrites commit history but doesn’t create extra commit for merging

10.  <p style='color:#95a5a6;font-weight:bold'> How to move code to staging?</p>
```
git add . # add everything in your dir to staging area(except files in .gitignore)

git add file_name # add only 
```
11. <p style='color:#95a5a6;font-weight:bold'> How to unstage code?</p> 
```
git reset
``` 
12.  <p style='color:#2980b9;font-weight:bold'>How to get rid of changes that we don't want to commit?</p>
```
git stash
```
13.  <p style='color:#2980b9;font-weight:bold'>How to reapply stashed change?</p>
```
git stash apply
or
git stash pop
```
14.  <p style='color:#95a5a6;font-weight:bold'> How to commit code?</p>
```
git commit -m "Your message"
```
15.  <p style='color:#2980b9;font-weight:bold'>How to undo commit?</p>
```
git reset <commit>
--soft: changed files are unstaged
--hard: changed files are discarded
```
16. <p style='color:#2980b9;font-weight:bold'>How to customize commits history? </p>
```
git rebase -i 
```
17.  <p style='color:#95a5a6;font-weight:bold'>How to revert a specific commit? </p>
```
git revert <commit>
```
18.  <p style='color:#8e44ad;font-weight:bold'>What is the difference between reset and revert?</p>

RESET moves the HEAD to a specific commit while REVERT records a new commit to reverse the effect of an earlier commit.


19.  <p style='color:#95a5a6;font-weight:bold'>How to view commit log? </p>
```
git log
or
git reflog
```

20. <p style='color:#2980b9;font-weight:bold'>How do we un-merged a master branch that has already been merged with another branch? </p>
```
git revert HEAD^ #can use head or <commit hash>
or 
git reset HEAD^ #can use head or <commit hash>
```
21. <p style='color:#8e44ad;font-weight:bold'>How do we undo rebase command?</p>

Since rebase paste all the commits of a branch to the tip of another branch, we need refer to the reflog to find the change before the rebase. This is also the reason why we should not use revert, since revert only add nodes to the trees and not seperate the two branches to their original state.
```
git reflog # find the <commit hash>
```
then we reset it to the specified commit 
```
git reset <commit hash>
```


21. <p style='color:#2980b9;font-weight:bold'>What is the golden rule of rebasing? Explain it!</p>
“Never rebase while you’re on a public branch". This change the tree history, which is catastrophic when multiple Dev working on it. [See more ](https://www.freecodecamp.org/news/git-rebase-and-the-golden-rule-explained-70715eccc372/)

22. <p style='color:#bdc3c7;font-weight:bold'> List the workflow of git. </p>
- Directory 
- Staging
- Commit
![](https://i.imgur.com/9hZ7kTO.png)


23. <p style='color:#e74c3c;font-weight:bold'>What is the different between reset and revert. When should we use revert or reset?</p>
```
git reset --soft <commit hash> 
# unstaged the changes you make. 
#Branch move to the specified commit (head must not be untached)

git reset --hard <commit hash>
# remove all the changes (like you never staged these changes)
#Branch move to the specified commit (head must not be untached)

git revert <commit hash>
# clone the specified commit and add it to the tree
```
24. <p style='color:#e74c3c;font-weight:bold'>If I rebase bugFix branch on top of my master branch, how can I move my master branch to the latest bugFix commit? Name at least two way to do it. </p>

```
# First method
# if not on master branch
git checkout master 
git rebase bugFix

# Second method 
git checkout master 
git merge bugFix

# Third method
git checkout master 
git branch -f master bugFix
```
25. <p style='color:#8e44ad;font-weight:bold'>How do we pull and push from a directory without enter username and password again? (only list the step) </p>

### Steps: 
     1. Check existing key 
     2. If not exist, generate your key 
     3. Copy the content of your key
     4. Go to you github account add your key in 
     5. Testing for connections and allow to connect
     6. Change your origin to use SSH connetion instead of HTTPS to clone repo.

### Details step-by-step:
- Generate your own SSH key.
    ```
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    ```

-  When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
    ```
    Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
    ```
- It will ask you to select a passphrase to protect your SSH key.
    ```
    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    ```
- Copy the content of your PUBLIC ssh key (Do not touch your private key)
    ```
    clip < ~/.ssh/id_rsa.pub
    # if not working, use cat and copy everything from the terminal
    cat < ~/.ssh/id_rsa.pub
    ```
- Go to settings on your github account, choose SSH and GCP key option. Click on new SSH key and paste the key in. Remember to put a name for the device that you generate the key from. 
- Testing your SSH connection: 
    ```
    $ ssh -T git@github.com
    # Attempts to ssh to GitHub
    ```
- You may see a warning like this. Please enter yes to continue with the setup.
    ```
    > The authenticity of host 'github.com (IP ADDRESS)' can't be established.
      > RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
      > Are you sure you want to continue connecting (yes/no)?
    ```
- If this message pop up, you have succeeded 
    ```
    > Hi username! You have successfully authenticated, but GitHub does no provide shell access.
    ```
- Now set your connection to SSH to pull from you repo. The default connection is HTTPS, hence it requires you to enter username and password everytime. The SSH connection can be found on the clone button on your repo and it looks something like this: 
    ```
    git remote set-url origin git@github.com:dhminh1024/ml_question_bank.git
    ```
## Customizing Gits

* Open configuration file
```bash=
vim ~/.gitconfig
```

* Configurations:

```

```