Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions programming-ide/git-commands-reference-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Git commands reference guide

You can keep track of changes to your repls using Git. Here are some basic and advanced Git commands you might find useful. Note that these are not intended as a replacement for the [Git reference docs](https://git-scm.com/docs), but rather a simpler version of the most commonly used commands.

## Basic commands

**Clone a repository**

```
git clone https://github.com/ritza-co/git-demo.git
```

**Add a file that you've created or edited**

```
git add myfile.txt
```

**Add all files that you've created or changed or deleted**

```
git add .
```

**Create a new branch**

```
git switch -c my-cool-feature
```

**Commit your changes with a summary message**

```
git commit -m "fixed the things"
```

**Push your changes to your branch**

```
git push origin my-cool-feature
```

## Advanced commands

If things are going well, you'll probably only need the commands mentioned above. If things go wrong or you make a mistake, you'll probably need some more advanced commands.

**Check the status of new and changed files**

```
git status
```

**Check which branch you're currently on**

```
git branch
```

**See the most recent commits**

```
git log
```

**Merge your feature branch into main**

```
git checkout main
git merge my-cool-feature
```

**Rebase your feature branch from main**

```
git checkout my-cool-feature
git rebase main
```

**Copy a specific commit to a new branch**

```
git checkout my-cool-feature
git cherry-pick a5ece7a7893025541e9f819930f3615677a2c1e0
```

**Reset to a specific commit but keep your changes**

```
git reset --soft a5ece7a7893025541e9f819930f3615677a2c1e0
```

**Reset to a specific commit and throw away your changes [DANGER]**

```
git reset --hard a5ece7a7893025541e9f819930f3615677a2c1e0
```
82 changes: 82 additions & 0 deletions programming-ide/git-with-replit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Using Git with Replit

There are a few ways to use Git and Replit together, using either the GUI controls built into the Replit IDE or the Replit shell.

## Using the GUI controls

In the sidebar, navigate to the version control tab. If your repl isn't yet configured, this will prompt you to connect your repl with a new or existing repository on GitHub. From there, you can switch branches, pull in new changes, or commit and push any changes you've made in the repl back up to GitHub.

![Git Replit GUI controls](/images/programming-ide/git-with-replit/git-replit-gui.png)

## Using the shell

If you want to use a different Git hosting provider or run more advanced Git commands, you can use the shell that is built into the Replit IDE. From here you can run any Git commands you want, and everything will work exactly as it would when using Git from the command line of your local machine.

### Cloning an existing repository from the shell

To clone a repository, use `git clone <url-to-your-repository>`. The URL should start with `https` so that you don't need to configure an SSH client on your repl. If the repository is public, it will immediately be cloned into your Replit Workspace, and you'll see the new directory appear in your files bar.

If the repository is private, you'll be prompted for your username and password. Note that GitHub no longer accepts passwords on the command line, so if you are cloning a private repository for GitHub you should rather put in a [personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) when prompted for your password.

In the example shown below, we run `git clone https://github.com/ritza-co/git-demo.git` and can see the new files appear in a subdirectory on the left called `git-demo`.

![Git Clone](/images/programming-ide/git-with-replit/git-clone.png)

### Making changes on Replit and pushing back to a remote Git host

Once you've cloned a repository, you can make changes using the Replit IDE. Once you're happy with the changes, you can add new files, commit your changes, and push everything back up to your Git hosting provider (e.g. GitHub) by running a few commands.

Before running any of the below, make sure you're in the correct directory. For example, if you cloned your repository using `git clone` as described above, you'll first have to change into the sub-directory, e.g. `cd git-demo` in the example we are using.

1. First, check the status of your repository by running the following command. This will show you any new files, changed files, or removed files:

```
git status
```

If you are happy with all of your changes, you can add them all with the following command:

```
git add .
```

Once you've added the files, you can run `git status` again to see that all the changes are listed in green now, showing that they're tracked by Git. In the example below, we see that we changed one file (`READEME.md`) and removed another one (The `git-demo` subdirectory). We also added a new file called `newfile.md` which is shown under "untracked files".

After running `git add .`, all files and changes are tracked by Git, so they appear under "Changes to be committed".

![Git Status and Git Add](/images/programming-ide/git-with-replit/git-status-git-add.png)

Before we can commit the files, we'll have to tell Git who we are by configuring a name and email address. Run the following commands, using your own name and email address:

```
git config --global user.name "Jane Doe"
git config --global user.email "[email protected]"
```

Now you can commit and push your changes as follows. You may have to change the branch name `main` to whatever your branch is called, e.g. `git push origin master` or `git push origin develop` are some common names.

```
git commit -m "add newfile and some fixes"
git push origin main
```

You'll be prompted for your username and password, whether or not the repository is public. Remember to use your GitHub access token instead of your password if you are using GitHub.

If everything works out, you should see a "Writing Objects..." message, followed by "done", as shown below.

![Git Commit Git Push](/images/programming-ide/git-with-replit/git-config-commit-push.png)

### Pushing without typing in your password or access token

There's always a tradeoff between convenience and security. If you are dealing with sensitive code or information, it's best to store your password or access token in a password manager, and paste it in each time you push changes.

If you prefer, you can store your credentials as part of the remote Git URL as environment variables, using Replit secrets. This means you won't have to type your username and password each time, but it also means that anyone with access to your repl will be able to find your password or token.

Under the secrets tab in the sidebar, add a new secret. Put `GIT_URL` as the key and the URL to your Git repository with your credentials as the value. For example, on GitHub you use the format `https://<username>:<github-access-token>@github.com/<user-or-organization>/<repository>`. Click 'Add Secret'. You may have to restart your shell before this secret becomes available, which you can do by typing `exit` in the shell.

![Add secret](/images/programming-ide/git-with-replit/add-secret.png)

Now you can use `git push $GIT_URL` to push up to your remote Git host without needing to type in your credentials.

For more advanced use of Git, including branching, rebasing, cherry-picking, and more, see our [Git commands reference guide](/programming-ide/git-commands-reference-guide).

8 changes: 8 additions & 0 deletions sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@
"name": "Debugging",
"slug": "debugging"
},
{
"name": "Using Git with Replit",
"slug": "git-with-replit"
},
{
"name": "Git Commands Reference Guide",
"slug": "git-commands-reference-guide"
},
{
"name": "Switch themes",
"slug": "switch-dark-mode-light-mode-themes"
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.