---
# System prepended metadata

title: Git Commands and Instructions

---

# Git Commands and Instructions


THE USUAL COMMAND FLOW IN ORDER AFTER SETTING UP REPOSITORY:
```
git status (check whether up to date)
/ and
git pull
git add .
git commit -m "message"
git push
/ or
git push origin master
```

## Git CLI


### Logging in
Set up the username:
```git
git config --global user.name "your_username"
```

Set up the e-mail adress:
```git
git config --global user.email "your_email_address@example.com"
```
(You can also check Settings → Emails → "Keep my email addresses private", and see your `num+username@users.noreply.github.com` address on GitHub.)


### Repository preparation
Clone from online repository:
```bash
git clone https://github.com/user/repository
```

Or, if you're not cloning but already have a local repository:
```git
git init
```

If you want to push to `main` instead of `master`, move to the branch beforehand:
```git
git branch -M main
```


Add the repository to `origin` for future actions from the console (origin is the remote repository):
```git
git remote add origin https://github.com/user/repository
```


### Pushing and Committing 
Ways of adding files for committing:

// Add all files:
```git
git add .
```
// Add one file:
```git
git add specific_file.cs
```
Commit with a message:
```git
git commit -m "message"
```
Push to the branch:
```git
git push -u origin master
```


### Pulling and Reverting Change
Checks whether branch is up to date:
```git
git status
```

You can't push to a branch on which you don't have the latest updates, so before writing any code, always pull from the repository like this:
```git
git pull
```
Reverting to a previous version (locally) pushed to git is possible with `git log` and then resetting using the specific ID of the push instance:
```git
git log --oneline
...
b764644 File with three lines
7c709f0 File with two lines
9ef9173 File with one line
```
```git
git revert 9ef9173
```

OR, if you just want to revert the latest commit,
```git
git revert HEAD
```

(You can then push these reverted files to the repo)


### Branching
Switches to branch (changes must be committed before switching):
```git
git checkout some_branch
```

Pulls from master branch (if you're on it, `git pull` will do)
```git
git pull origin master
```

Checks branch you're on:
```git
git branch
```

Lists branches:
```git
git branch --list
```

Creates and pushes new branch:
```git=
git branch new_branch
git push -u origin new_branch
```

Creates and switches to new branch:
```git
git checkout -b new_branch
```

Deletes branch:
```git
git branch -d new_branch
```


### Merging with master
Switch to the master branch:
```git
git checkout master
```
Update local master branch:
```git
git fetch
```

Merge some_branch with master:
```git
git merge some_branch
```


<br><br>

Check [this](https://training.github.com/downloads/github-git-cheat-sheet/) and [this](https://www.freecodecamp.org/news/10-important-git-commands-that-every-developer-should-know/).