09. Remote

The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories. Instead of providing real-time access to another repository, they serve as convenient names that can be used to reference a not-so-convenient URL.

git remote # list remote names
git remote -v # list all remotes
git remote show <name>
git remote add <name> <url>
git remote rm <name>
git remote rename <old-name> <new-name>

origin remote

origin – это произвольное имя (можно поменять) удаленного репозитория, с которым связан текущий. Кстати, таких удаленных репозиториев может быть сколько угодно. На практике такое поведение действительно бывает нужно, когда изменения отправляются сразу в несколько мест.

По умолчанию она идет в удаленный репозиторий origin и пушит текущую ветку, при условии что установлен "трекинг".

Трекинг – это отслеживание удаленного репозитория. Крайне важно понимать, что одинаковые названия веток в разных репозиториях для гита ничего не значат. Каждой ветки нужно установить соответствие (когда вы делаете git push, гит сам предложит команду, выполнив которую установится трекинг). Понятно, что на практике название веток почти всегда совпадают.

Если вы хотите отправлять текущую ветку в другой репозиторий или другую ветку, то можно сделать так:

Remote branch

Remote branches are just like local branches, except they represent commits from somebody else repository. You can check out a remote branch just like a local one, but this puts you in a detached HEAD state (just like checking out an old commit). You can think of them as read-only branches. Again, you can inspect these branches with the usual git checkout and git log commands.

git branch -r # review remote branches
git log --oneline master..origin/master # see commits have been added to the upstream master

Synchronize with remote repository

Fetching changes

The git fetch command imports commits from a remote repository into your local repo. The resulting commits are stored as remote branches instead of the normal local branches that we’ve been working with. Since fetched content is represented as a remote branch, it has absolutely no effect on your local development work. This gives you a chance to review changes before integrating them into your copy of the project.

git fetch
git fetch <remote> <branch>

Pulling changes

Merging upstream changes into your local repository is a common task in Git-based collaboration workflows. git pull <remote> is a alias for git fetch <remote> and git merge origin/<current-branch> executing one after each other.

git pull # git fetch + git merge
git pull --rebase # git fetch + git rebase

git config --global branch.autosetuprebase always # setup rebase always

git pull --rebase=interactive
git rebase master −−interactive −−exec=”npm test”

# x stands for "execute command after applying each commit"
git pull -x
git rebase master -x “npm test”

Pushing changes

Pushing is how you transfer commits from your local repository to a remote repo. It's the counterpart to git fetch, but whereas fetching imports commits to local branches, pushing exports commits to remote branches.

git push <remote> <branch>
git push <remote> --all # Push all of your local branches to the specified remote.
git push <remote> <branch>:<another-branch> # push changes from branch to ahother-branch on remote server

git push <remote> <branch> - работает при условии, что у вас есть права на запись и не было добавлено новых коммитов. This creates a local branch in the destination repository.

Last updated