# Basic version control and collaboration with git

*Colette Kelly · March 28, 2024 · how-to, computing*

Check out [this website](https://swcarpentry.github.io/git-novice/) for a lot more detail and explanation!

## clone a GitHub repository and start a new branch based on an existing one
1. Navigate to the directory where you want to clone this repository. You can use the `cd` command to change directories.

2. Clone the GitHub repository using the `git clone` command. Replace `https://github.com/boom-lab/n2o-py.git` with the URL of the GitHub repository you want to clone.
```bash
git clone https://github.com/boom-lab/n2o-py.git
```

3. Change your current directory to the cloned repository.
```bash
cd n2o-py
```

4. Check out the existing branch ckelly using the git checkout command.
```bash
git checkout ckelly
```

5. Create a new branch based on the existing "ckelly" branch using the `git checkout -b` command. Replace `new-branch-name` with the desired name for your new branch.
```bash
git checkout -b new-branch-name
```
You are now on the new branch. You can start making changes, committing them, and pushing them to the repository as needed.

6. Make changes to your files using any text editor or IDE available on the cluster.

7. Add your changes to the staging area using the `git add` command.
```bash
git add --all
```

8. Commit your changes using the `git commit` command.
```bash
git commit -m "Your commit message here"
```

9. Push the new branch to the GitHub repository using the `git push` command. Replace `new-branch-name` with the name of your new branch.
```bash
git push origin new-branch-name
```

You have now successfully cloned a GitHub repository and started a new branch based on the existing branch ckelly. You can continue working on your branch and collaborate by pushing your changes to the repository.

## stage, commit, and push changes
Replace `branch-name` with the name of your branch.
```bash
git add --all
git commit --all -m "your commit message here"
git push origin branch-name
```

## view current local and remote branches
```bash
[colette.kelly@poseidon-l1 n2o-py]$ git branch -a
* ckelly
  dnich-floats
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/ckelly
  remotes/origin/dnich-floats
  remotes/origin/master
  remotes/origin/predict
```

## delete a branch
```bash
[colette.kelly@poseidon-l1 n2o-py]$ git branch -d branchname
```

## switch to a different branch
```bash
[colette.kelly@poseidon-l1 n2o-py]$ git checkout dnich-floats
Switched to branch 'dnich-floats'
```

## fetch latest changes from remote repository
```bash
[colette.kelly@poseidon-l2 n2o-py]$ git fetch origin
From https://github.com/boom-lab/n2o-py
   ce5a8e4..055751d  master     -> origin/master
```

## view changes on branch ckelly from the remote repository
```bash
[colette.kelly@poseidon-l1 n2o-py]$ git log --graph --oneline origin/ckelly
* 0db96ac include .env file in version control
*   2d42910 try making a commit after resolving conflicts
|\  
| * 41124ab Update environment.yml
* | 6c005ed test change
* | fce698e add model output for training on surface data only
* | 8a3411f add user-specific output directory to RF notebook
```

## merge changes from branch ckelly into your branch
```bash
[colette.kelly@poseidon-l1 n2o-py]$ git merge origin/ckelly
```

## create local 'master' branch to track changes on the remote master branch
```bash
[colette.kelly@poseidon-l2 n2o-py]$ git checkout master
Branch master set up to track remote branch master from origin.
Switched to a new branch 'master'
[colette.kelly@poseidon-l2 n2o-py]$ git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.
```

## check to make sure changes on remote master branch have been merged to local master branch
```bash
[colette.kelly@poseidon-l2 n2o-py]$ git merge origin/master
Already up-to-date.
```
