Jump to content

GitHub

From Sinfronteras
# Go to https://github.com and create a new repository
# I think it's better to create the repository without adding a README or .gitignore file at this stage. These files can be added later from the local repository. If we create them now on the remote github repository, then, when pushing changes from our local repository, we might need to force push (git push -f origin main) because files (README, .gitignore) already exists on the remote github repository.


# Create a local folder
mkdir my-project
cd my-project


# Initialize a new Git repository locally
git init


# Ensure the branch is named "main". Traditionally, Git’s default branch was named master. github now use main as the default branch name. When you run git init, your default branch might still be called master (depending on Git version and config). To align your local repo with github's default naming (main), you should rename the branch. If you skip this and your branch is called master, pushing to github might create a new branch master instead of main, which can be confusing.
git branch -M main
# If you want to check your current branch name: 
git branch --show-current


# Crate a README
echo " " >> README.md


# Create a .gitignore. You can use github templates from https://github.com/github/gitignore
vi .gitignore


# Stage files for the first commit
git add .    # Use this if you only want to stage changes in the current directory, and don't want to stage deletions (files you delete locally will NOT be deleted on github)
git add -A   # Use this for a complete, global staging of all changes across the whole repository.
# ----------
Example: 
repo/
├── folder1/
│   └── file1.txt
├── folder2/
│   └── file2.txt
└── file3.txt
# If you run "git add ." inside folder1:   It stages new/modified files inside folder1 and its subfolders. It does not stage changes in folder2/ or file3.txt at the root. You might want to stage changes only in the current directory in scenarios where you're working on specific parts of a project and don't want to commit unrelated changes made elsewhere in the repository.
# If you run "git add ." at the repo root: It stages new/modified files everywhere in the repo. But still won’t stage deletions.
# ----------


# Commit the changes
git commit -m "first commit"


# Link your local repo to github
git remote add origin git@github.com:adeloaleman/webapp.git          # Requires you to have an SSH key set up and added to your github account. No need to enter username/password on every push/pull. Generally more secure and preferred if you work frequently with github.
git remote add origin git@github-codeastute:codeastute/webapp.git    # ... if using multiple github accounts


# Push to github
git push -u origin main
# -u = remember the remote/branch you're pushing to. So, for the next push you just need to do: git push


# Then, every time we want to push changes to github
git add -A   # git add .
git commit -m "Your message"
git pull origin main   # Optional - If you're collaborating with others, it could be necessary to sync with the remote first
git push origin main   # or just 'git push' if the option -u was previously specified 


# How to configure SSH for multiple github accounts
# Create or edit ~/.ssh/config:
# Codeastute github account
Host github-codeastute
    HostName github.com
    User git
    IdentityFile ~/.ssh/codeastute_ssh

# After above configurations. Link your local repo to github if it hasn't been done:
git remote add origin git@github-codeastute:codeastute/webapp.git 

# Change the remote URL if the repository had already been linked:
git remote set-url origin git@github-codeastute:codeastute/webapp.git

# To clone remote repository in GitHub
git clone git@github.com:adeloaleman/nextjs.git  # Or... 
git pull origin main                             # ... To sync with the remote if the local repository has already been cloned/created