---
# System prepended metadata

title: git cheat sheet

---

  # git cheat sheet

  1. [repos](#repos)

  2. [what's up](#whats-up)

  3. [commits](#commits)

  4. [looking around](#looking-around)

  5. [⭐ feature branches (git flow collaboration)](#-feature-branches-git-flow-collaboration)

  6. [rebase issues](#rebase-issues)

  7. [remotes](#remotes)

  8. [other cool tricks](#other-cool-tricks)

  ### repos

  #### - _to start a repository (am i in the right place on my computer? make a folder):_

  In a terminal, i'll jump to my user folder to start.
  then i should go into the right folder where i keep my projects (eg./Users/ME/projects).
  If it's an existing project i want to add git to, ill go into that sub-folder.
  if its not an existing project, i should make a folder for my new project in the right folder, in my projects folder.
  then i initialise git:

  ```
  cd
  cd projects
  mkdir MY_NEW_PROJECT
  cd MY_NEW_PROJECT
  git init
  ```

  #### - _to check if i've accidentally made a git repository in the wrong folder:_

  use `pwd` to check your folder, if you're in the right one. or always go back to user home folder with `cd` and actively navigate to the right place.

  ```
  pwd
  ( or )
  cd
  ( then go to correct folder )
  ```

  #### - _to delete a git repository i've accidentally made in the wrong folder:_

  to delete _ANY_ folder .. where FOLDER_NAME is the folder you want to delete

  ```
  rm -rf FOLDER_NAME
  eg.
  rm -rf .git
  ```

  #### - _to download an existing repository (am i in the right place on my computer? don't make a folder):_

  make sure you're in the right folder REMINDER: you don't need to make a project folder, git will make it

  ```
  cd
  cd projects
  git clone MY_GITHUB_REPO_URL
  ```

  ### what's up

  #### - _to check whats happening in git right now:_

  ```
  git status
  ```

  #### - _to check which branch i'm in:_

  ```
  git branch
  ```

  ### commits

  zan

  #### - _to add something to my shopping basket (add to staging area):_

  ```
  git add example.html
  ```

  fi

  #### - _to save my current work (make a commit):_ git commit

  ```
  git commit -m "add font and other styling"
  ```

  samin

  #### - _why I should't type `git add .`:_

  this will add EVERYTHING that changed, without me thinking carefully about it.
  We are supposed to _carefully_ make and watch changes in git, that's what it's for.

  jens

  #### - _what a commit message should contain:_

  the message in between "" which explains the changes or the feature you want to implement

  - in present tense "add" not "added"

  zan

  #### - _how often i should commit my work:_

  As often as humanly possible

  fi

  #### - _reasons to commit my work:_

  ```
  So you will not lose work and you travel back to other points!
  ```

  samin

  #### - \_to check i saved my work before going a commit:

  (unCOMMITed changes) - this will show you uncommited changes

  ```
  git checkout
  ```

cd
touch .bashrc





  (unSAVED text files)
  look for the **black/white circle** in the tab of any open files Visual Studio Code! That means they're unsaved.

  ### looking around

  jens

  #### - _to see all the commits:_

  ```
  git log             //shows commits in historical order
  (or)
  git log --oneline   //same as before just shorter
  (or)
  git reflog         //shows history of travelling and current commit you're in
  ```

  fi

  #### - _to see where I have been in terms of travelling between commits:_

  ```
  git reflog
  ```

  zan

  #### - _to compare the current commit to another commit:_

  samin

  #### - _to travel to another commit:_
```
  git log --oneline
  git checkout (id) OR branchname
```
    
    arthi
  #### - _reason that i can't sometimes switch to a different commit or do a pull:_
  
uncommitted changes in the staging area.
changes are not yet committed.


```
git commit -m filename 
git pull
```

  ### ⭐ feature branches (git flow collaboration)

katrin
  #### - _to start making a feature (new branch):_

```
git checkout -b my_feature_branch
```
samin
  #### - _to continue making a feature after lunch break (rebase):_

```
git checkout main
git pull     
git checkout my_feature_branch
git rebase main

```
git pull
fabian
  #### - _to finish making a feature (rebase, push, pr):_
  
```
git checkout main
git pull     
git checkout my_feature_branch
git rebase main
git push
```



  #### - _how often to PR (merge a feature branch into the main branch):_
  
  ```
rather do a few PRs with small changes, then big ones with lots of code / changes
```

  ### rebase issues
  
Fabian
  #### - _why i use `rebase` instead of `merge`:_
  the key difference between merge and rebase is that while merge preserves history 
  as it happened, rebase rewrites it.Merging direktly inserts the new main. Rebase 
  puts the commits made by others in to the commit history. 
  Rebase will have *inbetween* steps (commits) made by others in the log, which would not be there if we merge.
   
  #### - _some reasons and commands that cause a `conflict`to happen:
  conflicts with local changes
  A failure DURING a merge indicates a conflict between the current local branch and the branch being merged. This indicates a conflict with another developers code. Git will do its best to merge the files but will leave things for you to resolve manually in the conflicted files. 
  
  the question is not "is there a conflict on a specific line"
  the question is "is there a conflict on a specific line at the same point
  in time" (same amount of commmits)
marina
  #### - _what is happening during a rebase conflict in simple words:_
  two of your commits modified the same line in the same file, and Git doesn't know which change to apply.

marina 
  #### - _the steps to do during a rebase:_
  Make sure to be in the feature branch that you want to rebase
  Pull feature branch latest commits
  Checkout branch you are planning to rebasing onto
  Pull latest commits of branch you are planning to rebase onto git pull
  Checkout feature branch
  Start rebase git rebase
  Fix conflicts during rebase or skip to continue rebase

  ### remotes
arthi
  #### - _to check where git is pushing code to (list all remotes):_
  git commit -
  git main branch
  

  ### other cool tricks
katrin
  #### - _to hide a file from git and ignore it forever ( so it never goes on github ):_
make a .gitignore file, add whatever you need
```
**/.DS_Store (hides all .DS_Store files)
templates.js (hides the file "templates.js")
/js_templates (hides a folder)

```
  #### - _instead of diffing every changed line of code, show only filenames of files that changed:_
 `git log --name-only`
 
 
  #### - _to visualise a tree of the git repository 🌳:_
  
  `git log --graph`
  
   Compact version:
  ```
  git log --oneline --abbrev-commit --all --graph --decorate --color
  ```
