Git·19 questions

How to work with remote repositories (origin, fork) in Git?

Answer

A remote repository is a version of your project stored on a server (GitHub, GitLab, Bitbucket). Managing remote repositories is a key skill for team collaboration.

Viewing remotes: git remote -v shows all configured remotes with URLs. By default, the first remote is called origin. The command git remote show origin shows detailed information: branches, tracking settings.

Adding a remote: git remote add <name> <url>. For example, if you forked a project and want to get updates from the original: git remote add upstream https://github.com/author/project.git. Now you can run git fetch upstream to get changes from the original repository.

Removing and renaming: git remote remove <name> removes a remote. The command git remote rename <old> <new> renames it.

Pushing changes: git push origin <branch> sends a local branch to remote. The -u flag (git push -u origin <branch>) sets up tracking — after that, git push and git pull work without specifying remote and branch. The command git push --force forcibly overwrites a remote branch — use only for personal branches after rebase. Safe alternative: git push --force-with-lease — overwrites only if no one else has added commits.

Getting changes: git fetch origin downloads changes but doesn't apply them — you can review them via git log origin/main. The command git pull = git fetch + git merge — downloads and immediately merges. For linear history: git pull --rebase.

Working with forks: typical cycle — fork on GitHub, git clone your fork, git remote add upstream original, create a feature branch, commit, git push origin feature-branch, then pull request via web interface. To sync with the original: git fetch upstream, git checkout main, git merge upstream/main, git push origin main.

Was this answer helpful?

More questions in this topic

Related questions from other topics