Home > other >  Where are the files pushed when you do "git push" when on branch?
Where are the files pushed when you do "git push" when on branch?

Time:08-05

So I was on a branch and making a commit. I used git push instead of git push --set-upstream origin branch"and I didn't see the data on repository anywhere.

git status showed that data was pushed and I was up to date with remote but I couldn't see the data on the branch on GitLab.

Where should I check for the data that was pushed?

CodePudding user response:

With git branch --show-current or git status you will see the current branch where you have execute git push (assuming you haven't switched branches).

Now you know the name and can find the branch on GitLab.

Annotation: by default, git push only updates the corresponding branch on the remote. So, if you are checked out to the master-branch when you execute git push, then only the master-branch will be updated.

CodePudding user response:

(First, a brief note: git push pushes commits, not files. The commits contain files, so you might think that there's no big deal / big difference here, but later, you'll find out that it really is a big deal and a big difference. That said, let's move on.)

The full answer is loaded with caveats, because:

  • there's a push.default setting; and
  • there are settings for remote.remote.pushurl, remote.remote.push, and so on

and these can all interact with exactly what you ran.

By default, however, as SwissCodeMen mostly said, git push is equivalent to:

branch=$(git rev-parse --abbrev-ref HEAD)
remote=$(git config --get branch.$branch.remote || echo origin)
git push $remote $branch

provided that your Git version is at least 2.0 and you have not fiddled with all the other settings. But: if you have not set an upstream, this command would give you an error. You did not mention any errors occurring, so presumably you did not get an error, which means that either you're using an older Git version, or you already had an upstream set. So we're already in "something seems questionable" here territory. This means we need to know more about your actual situation; only you know the answers, so you'll have to read on.

The first two lines, that set $branch and $remote, just figure out which branch you're on—i.e., the branch name that git status would print, or that git branch would put an asterisk * next to, and then the name of the remote that corresponds to that branch. If there isn't one yet—if the current branch has no upstream set—the remote name defaults to origin.

Next, since you didn't provide a refspec, Git goes on to look up the default push refspec for the remote. That is, Git runs the internal equivalent of:

git config --get remote.$remote.push

By default, this isn't set, so this lookup fails; that tells Git to go on to query the setting of push.default:

git config --get push.default

This can be set to any of multiple values, including matching and simple. The default in Git versions 2.0 and higher is simple, while the default in older Git versions is matching.

In simple mode, the git push gives you an error if:

  • the upstream is not set yet; or
  • the upstream setting's branch component does not match this branch name.

The upstream setting of a branch can be shown by two git config --get operations:

git config --get branch.$branch.remote
git config --get branch.$branch.merge

—either or both can fail, in which case the upstream isn't set—or, provided it's actually set, by:

git rev-parse --symbolic-full-name @{upstream}

If there's no upstream set, this produces, e.g.:

fatal: no upstream configured for branch 'main'

The next section assumes push.default is simple

If you did have an upstream set, you don't need to run git push --set-upstream:

I used git push instead of git push --set-upstream origin branch

The need for --set-upstream—the word need is a little too strong, but I'll leave it in without further explanation—is only there for the first git push from this branch. It tells git push to run git branch --set-upstream-to if (after) the git push part succeeds, so that future git push operations don't need anything other than plain git push. You can of course just run your own git branch --set-upstream-to command, but --set-upstream or just -u in the git push command is shorter to type, at least.

The purpose of the simple setting, which has been the default since Git version 2.0, is to make git push work the way normal humans (those who haven't been exposed to too much pre-2.0 Git like me

  • Related