Skip to content

Git Knowledge

Updated: 2024-06-15 / JRO

Check & Verify current git user

git config --global user.name
git config --global user.email

How to change Git User / Email

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Changing git users for a specific repository

cd /path/to/your/repository
git config user.name "Your Name"
git config user.email "your.email@example.com"

Git User Notes

  • Identity in Commits: Keep your Git user name and email consistent when you are contributing to projects, as they are used in commit logs to identify the author of each commit.
  • Multiple Git Identities: When you work on different projects (or GitLab, GitHub accounts), consider using different email addresses per repository.

Check out code repository locally

git clone <repository_url>

# Example: 
# git clone https://gitlab.com/opentechnews/opentech.news-website.git

Add Remote repository

git remote add origin <gitlab-repo-url>

list all Remotes associated with a repository

git remote -v

Remove remote repositories

git remote remove origin

Push code to a remote repository

git push -u origin main
  • -u origin main: Sets the upstream branch to origin/main, which means in future git push commands, you can simply use git push without specifying the remote and branch.

Add username to Remote Git repo URL

git remote set-url origin git@<hostname>:<username>/<repository>.git

# Example: 
# git remote set-url origin https://opentech.news@gitlab.com/opentechnews/opentech.news-website.git

Source: https://forum.gitlab.com/t/i-cant-push-to-my-own-public-remote/96008

Rebase local changes on top of the remote changes

git pull --rebase origin main
  • Fetches changes from the remote (origin) main branch and reapplies local commits on top of them. This can create a cleaner history but may require resolving conflicts during the rebase process.