Skip to content

lab 56 Pushing a Change

Goals

Since bare repositories are usually shared on some sort of network server, it is usually difficult to cd into the repo and pull changes. So we need to push our changes into other repositories.

Let’s start by creating a change to be pushed. Edit the README and commit it

README

This is the Hello World example from the git tutorial.
(Changed in the original and pushed to shared)

Execute:

git checkout main
git add README
git commit -m "Added shared comment to readme"

Now push the change to the shared repo.

Execute:

git push shared main

shared is the name of the repository receiving the changes we are pushing. (Remember, we added it as a remote in the previous lab.)

Output:

$ git push shared main
Everything up-to-date

NOTE: We had to explicitly name the branch main that was receiving the push. It is possible to set it up automatically, but I never remember the commands to do that. Check out the “Git Remote Branch” gem for easy management of remote branches.

You Can Push Anything

Pushing to a remote repository requires specifying what you are pushing and where you are pushing it. The git push shared main command uses a shorthand to specify where the branch is going to be pushed. You could read it like this: git push shared main:main, where what comes before the colon is in your local repository and what comes after is in the remote repository. There is a lot of power in this ability. Recall Lab 24 where we looked at the internals of the .git directory. On both sides of the colon you can specify any branch name, or any path starting with the refs/ directory. You can also specify an arbitrary local commit to push. Below are some examples:

Execute:

# Push a commit to a new branch
git push shared <hash>:refs/heads/new-branch
# Rename a remote branch
git push shared refs/remotes/shared/new-branch:refs/heads/new-branch-rename
# Push a local branch under a different name
git push shared greet:greet-rename
# We can confirm they're all there
git remote show shared

Output:

$ git push shared 91b926e:refs/heads/new-branch
To ../hello.git
 * [new branch]      91b926e -> new-branch
$ git push shared refs/remotes/shared/new-branch:refs/heads/new-branch-rename
To ../hello.git
 * [new branch]      shared/new-branch -> new-branch-rename
$ git push shared greet:greet-rename
To ../hello.git
 * [new branch]      greet -> greet-rename
$ git remote show shared
* remote shared
  Fetch URL: ../hello.git
  Push  URL: ../hello.git
  HEAD branch: (unknown)
  Remote branches:
    excitement        new (next fetch will store in remotes/shared)
    greet             new (next fetch will store in remotes/shared)
    greet-rename      tracked
    main              tracked
    new-branch        tracked
    new-branch-rename tracked
  Local refs configured for 'git push':
    excitement pushes to excitement (up to date)
    greet      pushes to greet      (up to date)
    main       pushes to main       (up to date)