Git basics
Tags: github, git, terminal, linux
Published: 2020-06-18 12:06:43

A simple guide on editing a repository offline using `terminal commands`. `These` are the commands you need.
This assumes you have git, github, a repository (or repo) which you can access in github, and a linux or mac terminal.

## 1. moving to the right folder

a) Type `pwd` in the terminal and press enter to see where you are.

b) `ls` or `dir` to show the things in the current directory

`ls -l` to list information on the files and folders

c) Use `cd` to change directory.

`cd` to go to the home directory

`cd docs` to go to  a specific folder named docs

(repeat until you are in the folder you need to be)

you can also use `cd ..` to move one directory up and `cd /..` to move to the top directory

or you could simply navigate to the right folder with your file browser, right click it, and select 'Open in Terminal' or the equivalent option


## 2. cloning a repository (download it to your current directory to work in it)

a) go to the github page for the repository

b) click the green clone or download button and copy the LINK (it ends with .git)

c) We now start work in git. All git commands start with `git`.

Use `git clone LINK` to clone the online repository (this is called a remote repo) to the folder you're in (now it's a local repo)

d) you are not working in the repo yet. You still need to go there using `cd NAME` where NAME is the name of the repo

e) you are in the repo now!

## 3. opening a branch and editing it

 a) use `git branch -a` or `git branch -av` to show all branches. The branch you are now in will be green or white.

 b) if you need to create a new branch just use `git branch NEWBRANCHNAME`
 
 c) `git checkout BRANCH` to start working in the branch named BRANCH
 
 d) `ls` or `dir` to show the files in the repo to know which files you can open
 
 e) `open FILENAME` to open the file named `FILENAME`. If `open` doesn't work you can also open it manually by going to the folder in your file browser. Text files can also be edited within the terminal using vim or nano but I don't recommend it.
 
you are now editing a file!

## 4. push your changes to the repo

a) once you are done improving the file, save it.

b) then do `git add` to move your file to your staging area

You can also use `git add A` to move ALL files in the currrent folder to the staging area. Useful when you work on multiple files.

c) then do `git commit` to commit the file to your local repository

you can also do step b and c at the same time by doing `git commit -a`

d) then do `git push` or `git push origin`

(`origin` is the URL of the online repo, if you want to push to another repo than where you pulled it from you need to replace `origin`. In that case you would probably get an error though which you might be able to solve by adding `-f` to force your changes down the throat of the unwilling repo)

e) you'll need to enter your github username and password. If you're new to all of this you may be surprised that you don't see anything while you type your password, but that's normal.

## 5. merging changes from branch A to branch B

a) go to branch B (use `checkout B`)

b) do `git merge A`