DEV Community

Dakota Lewallen
Dakota Lewallen

Posted on • Updated on

Git Remotes in Five Minutes or Less

Recap

So far in this series we've covered the basics what a git repository is. As well as how to manage git repositories in your local environment. In this entry, we're going to look at how you can take your work, and make it available on the internet.

Vocabulary

  1. Remote
    • A non-local location for a repository. Accessed via URL. Stored as a key-value pair, where a given name is the key and the URL is the value. Note: A local repository may have many remotes.

Commands

user:~/project$ git remote add <name> <URL>
Enter fullscreen mode Exit fullscreen mode
  • When ran in an existing local repository, this command adds the URL to the list of remotes. Any branches from the remote repository, will follow a naming pattern of <name>/<branch>.
user:~/project$ git clone <URL>
Enter fullscreen mode Exit fullscreen mode
  • Creates a new repository containing the contents of the repository located at <URL>. Automatically adds the remote to the repositories list.
user:~/project$ git fetch
user:~/project$ git fetch <name>
Enter fullscreen mode Exit fullscreen mode
  • Retrieves information about remote repositories. The name of a remote can be provided to retrieve its history alone.
user:~/project$ git pull
user:~/project$ git pull <name>
user:~/project$ git pull <name> <branch>
Enter fullscreen mode Exit fullscreen mode
  • Retrieves information from remote repositories
  • Attempts to merge remote branch into local branch.
  • If no remote name is provided, the default is used.
  • If no branch name is provided, it will attempt to merge a remote branch with the same name as the locally active branch.
  • Similar to git merge from the last entry, you can pass flags to the command to prevent automatically merging.
user:~/project$ git push
user:~/project$ git push <name>
user:~/project$ git push <name> <branch>
user:~/project$ git push <name> :
user:~/project$ git push <name> <branch>:<remote>
Enter fullscreen mode Exit fullscreen mode
  1. Send local changes to remote repository.
    • If no remote is specified, will attempt to update the remote for the current branch. You can use the --all flag to push all branches.
  2. Send current branch to <name>.
  3. Push <branch> to <name>.
  4. Using a colon in place of a branch name, will push all branches that exist in both the local and remote repositories.
  5. You can push local branches to remote branches with different names, by specifying the two, separated by a colon. Effectively merging your local branch with the remote branch, in the remote repository.
user:~/project$ git remote rename <name> <new>
user:~/project$ git remote remove <name>
Enter fullscreen mode Exit fullscreen mode
  1. Will change the name that is associated with the remote.
  2. Will remove the remote from the list repositories list.

More Information

Remotes are arguably the most valuable feature of Git. They enable anyone to work on any number of projects at any given time or location. While the commands above will get you running with remotes. The real magic comes from the remote providers. They've made their hosting free and openly available to anyone with an internet connection. By doing so, they've fundamentally changed people's access to software and the information it provides. If you're not familiar already, I would strongly suggest you spend some time getting familiar with them and the people they work with.


Find me on Twitter | LinkedIn
Sponsor me on Github
Like the article? Buy me a coffee!

Top comments (0)