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:

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

Initialize a new git repository:

shell
git init

Add the README.md file to the staging area:

shell
git add README.md

Commit the file to the repository:

shell
git commit -m "Initial commit"

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

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

Push the local repository to GitHub:

shell
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):

shell
git rm --cached filename

For a directory:

shell
git rm -r --cached directory-name

Add the file or directory to .gitignore:

shell
echo "filename" >> .gitignore

Commit the changes:

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

Push the changes to remote:

shell
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:

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

or if you are using SSH:

shell
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:

shell
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.

shell
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.

Delete a branch

Delete a local branch:

shell
git branch -d branch-name

Use -D to force-delete a branch that has not been merged:

shell
git branch -D branch-name

Delete a remote branch:

shell
git push origin --delete branch-name

To delete a branch both locally and on the remote:

shell
git branch -d branch-name
git push origin --delete branch-name

Print last 3 commits:

shell
git log -3

Make empty commit

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

shell
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