LCM Logo
Git & GitHub

Git

Create a new repository on GitHub

Create a new GitHub repository on github.com

  • Go to GitHub and create a new repository. You can do this by clicking the "+" icon in the top right corner and selecting "New repository".
    1. General:
    • Set "Owner" and "Repository name". Owner can be a user or an organization.
    • Set "Description" (optional).
    1. Configuration:
    • Choose "Public" or "Private" visibility.
    • Leave the rest unchanged.
  • Click "Create repository" at the bottom right.

Create a new local git repository on your system and push it to GitHub

Initialize a README.md file:

echo "# repo-name" >> README.md

Initialize a new git repository:

git init

Add the README.md file to the staging area:

git add README.md

Commit the file to the repository:

git commit -m "Initial commit"

Add the remote repository URL (replace username and repo-name with your GitHub username and repository name):

git remote add origin git@github.com:username/repo-name.git

Push the local repository to GitHub:

git push -u origin main

Remove file from remote and add to .gitignore

If you accidentally added/committed/pushed a file that should be ignored, you probably want to remove it from the remote repository and add it to your .gitignore file, while keeping it locally.

Remove the file from git tracking (but keep it locally):

git rm --cached filename

For a directory:

git rm -r --cached directory-name

Add the file or directory to .gitignore:

echo "filename" >> .gitignore

Commit the changes:

git commit -m "Remove filename from tracking and add to .gitignore"

Push the changes to remote:

git push

The file or directory will now be removed from the remote repository but will remain in your local working directory.

Rename a repository

If you need to rename a repository that you have already cloned on your local machine:

  1. Rename the repository on GitHub:

    • Go to the repository on GitHub.
    • Click on "Settings" (the gear icon).
    • Under the "Repository name" section, enter the new name and click "Rename". (May have to click a second time to confirm.)
  2. Update the remote URL in your local repository:

git remote set-url origin https://github.com/username/new-repo-name.git

or if you are using SSH:

git remote set-url origin git@github.com:username/new-repo-name.git

Rebase a branch

To rebase a branch onto another branch, you can use the following command:

git rebase target-branch

This will take all the commits from the current branch and replay them on top of the target-branch.

Scenario: you are working on feature-branch "superfeat", which was branched off "main" a few days ago. In the meantime, "main" has received some updates. You can rebase "superfeat" onto "main" to incorporate the latest changes from "main" into your branch. The result will be as if you had created "superfeat" from the latest commit on "main" and then applied your commits on top of it.

git checkout superfeat
git rebase main

Rebasing can lead to a cleaner commit history, but it can also cause issues if not used carefully. Avoid using it with shared branches. Always make sure to communicate with your team and understand the implications of rebasing before using it.

Make empty commit

If you want to create an empty commit, i.e. a commit without any changes, you can use the following command:

git commit --allow-empty -m "Trigger CI"

This can be useful to trigger CI/CD pipelines or to create a commit that serves as a marker in the commit history without changing any files.

Resources

On this page