Make sure to do this after installing Git (see next section)
-
Open Terminal or Command Prompt:
- On macOS and Linux, open Terminal.
- On Windows, open Command Prompt or Git Bash.
-
Generate SSH key pair: Tyoe the following command in your terminal after inserting your email into the command:
ssh-keygen -t ed25519 -C "your_email@someplace"
This will give you a prompt like: 'Generating public/private ed25519 key pair. Enter file in which to save the key (/Users/olivias-local/.ssh/id_ed25519):' Enter the file to save the key to, just copy what it gives you in the parenthesis. Then just press enter twice when it asks for a passphrase.
-
Copy Key: Now you can find the key by typing this in the command line:
cat ~/.ssh/id_ed25519.pub
This will print something like:
'ssh-ed25519 SOMERANDOMCHARACTERSTRINGblahblahblahblah your_email@someplace'
Copy that
-
Add key to GitHub: Now add this key to your GitHub account by going to Settings → SSH and GPG keys → New SSH key and paste what you just copied
To clone a GitHub repository, follow these steps:
-
Install Git: Ensure that Git is installed on your computer. You can download it from git-scm.com and follow the installation instructions for your operating system.
-
Open Terminal or Command Prompt:
- On macOS and Linux, open Terminal.
- On Windows, open Command Prompt or Git Bash.
-
Navigate to the Desired Directory: Use the
cd
command to navigate to the directory where you want to clone the repository. For example:cd path/to/your/directory
-
Find the Repository URL: Go to the GitHub page of the repository you want to clone. Click on the "Code" button and go to the "SSH" tab. Copy the URL provided there. It should look something like:
[email protected]:CosmiQuantum/how_to_use_github.git
-
Clone the Repository: Use the
git clone
command followed by the repository URL. For example:git clone [email protected]:CosmiQuantum/how_to_use_github.git
-
Verify the Clone: After the clone process completes, navigate into the cloned repository folder:
cd how_to_use_github
- List Available Branches:
To view all branches in the repository, use the following command:
git branch -a
- Create a New Branch:
To create a new branch (and switch to it), use:
git checkout -b new-branch-name
To switch between branches in Git without affecting the branch you're currently working on, follow these steps:
- Commit or Stash Changes:
Make sure you have no uncommitted changes on your current branch. You can either commit your changes using:
git add . git commit -m "Your commit message"
- Switch Branch:
Switch branches by using one of the two following commands:
or:
git switch branch-name
git checkout branch-name
- Verify Branch:
Make sure you are indeed on the right branch by doing:
git branch
These instructions assume you have Git installed on your computer and have already cloned a repository. If not, you'll need to do that first.
Steps:
-
Open Terminal or Command Prompt: Navigate to the local repository directory using the
cd
command.cd path/to/your/repository
-
Stage your changes: Use
git add
to stage the files you want to include in your commit. You can add individual files, specific directories, or all changes using the following commands:git add <file>
(e.g.,git add my_file.txt
)git add <directory>
(e.g.,git add my_directory/
)git add .
(stages all changes in the current directory and subdirectories)
-
Commit your changes: Use
git commit
to save your staged changes with a descriptive message. This creates a snapshot of your work.git commit -m "Your descriptive commit message"
Replace
"Your descriptive commit message"
with a nice summary of the changes you've made. -
Push your changes: Use
git push
to upload your committed changes to the remote repositorygit push origin <branch_name>
origin
is typically the name of the remote repository. You can check the remote names usinggit remote -v
.<branch_name>
is the name of the branch you're working on. If you're pushing to a different branch, replace<branch_name>
accordingly.
Example:
Let's say you've made changes to my_file.txt
and another_file.js
:
git add my_file.txt another_file.js
git commit -m "Added new features and fixed a bug"
git push origin <branchname>
-
Open Terminal or Command Prompt: Navigate to the local repository directory using the
cd
command.cd path/to/your/repository
-
Pull Changes: Use this command to pull the latest changes from the remote repository.
git pull origin your-branch-name
Replace your-branch-name with the name of the branch you want to pull from.
-
If this throws and error: Probably someone has pushed changes to the repo, and you have not pulled them before making local changes. In this case do these steps:
- Fetch the latest changes from the remote repository. This will update your local copy of the remote branches without merging:
git fetch origin
- Merge the changes into your current branch. If you are on the branch where you want to merge the changes, use:
Replace with the name of the branch you are merging from. If there are any merge conflicts, Git will notify you here. You'll need to resolve those conflicts in your files and then stage the resolved files.
git merge origin/<branchname>
- Commit the merge if it happens automatically or if you had to resolve conflicts:
git commit -m "Merge updates from remote branch"
- Push your changes to the remote repo:
git push origin <branchname>
- Fetch the latest changes from the remote repository. This will update your local copy of the remote branches without merging:
To remove the currently configured GitHub user in the command line and add a new one, use the following instructions.
-
Check Current Git Configuration: Look at the current Git user settings using:
git config --global user.name git config --global user.email
-
Unset or Update Git User Information: unset whatever settings are there now using:
git config --global --unset user.name git config --global --unset user.email
-
Set New User Information: Set your username now by doing:
git config --global user.name "New Username" git config --global user.email "[email protected]"
Replace
"New Username"
and"[email protected]"
with your GitHub username and email. -
Verify the Changes: Ensure that your Git configuration and remote URLs are updated correctly.
git config --global user.name git config --global user.email