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".
-
- General:
- Set "Owner" and "Repository name". Owner can be a user or an organization.
- Set "Description" (optional).
-
- 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.mdInitialize a new git repository:
git initAdd the README.md file to the staging area:
git add README.mdCommit 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 [email protected]:username/repo-name.gitPush the local repository to GitHub:
git push -u origin mainRemove 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 filenameFor a directory:
git rm -r --cached directory-nameAdd the file or directory to .gitignore:
echo "filename" >> .gitignoreCommit the changes:
git commit -m "Remove filename from tracking and add to .gitignore"Push the changes to remote:
git pushThe 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:
-
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.)
-
Update the remote URL in your local repository:
git remote set-url origin https://github.com/username/new-repo-name.gitor if you are using SSH:
git remote set-url origin [email protected]:username/new-repo-name.gitRebase a branch
To rebase a branch onto another branch, you can use the following command:
git rebase target-branchThis 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 mainRebasing 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.