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 [email protected]:username/repo-name.git

Push the local repository to GitHub:

shell
git push -u origin main

Create new branch and switch to it

For example, create a new branch called "devel" and switch to it:

shell
git switch -c devel

This is the modern way to create and switch to a new branch. The older way that is still widely used is:

shell
git checkout -b devel

Git flow

Simple main > devel > main

Switch to devel

shell
git switch devel

Make edits > open PR to main on github.com > complete review > merge devel -> main

After the PR is merged, update your local branches:

shell
git switch main
git pull
git switch devel
git rebase main

Restore a file to the last committed state

This will discard any changes made to the file since the last commit:

shell
git restore filename

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 [email protected]: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.

List branches

List all local branches:

shell
git branch

List all remote branches:

shell
git branch -r

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

Stash and pop changes

Stash local changes:

shell
git stash

Switch branches or do any other operations without worrying about your local changes. Then, apply stashed changes and remove them from the stash list:

shell
git stash pop

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