1

Please help to clarify the question below.

I have done the following steps.

  1. git checkout -b test origin/master
  2. Made some code changes
  3. git add followed by git commit and git push
  4. One of my colleagues checked out my test branch. He did not add any new changes in it
  5. Now I updated test branch with new code changes and pushed it.

I would like to know, how would my colleague who has already checked out test branch,be able to see the latest changes that I did in step 5.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Eli Johnes
  • 301
  • 3
  • 13
  • 1
    Your colleague needs to run `git pull` to incorporate new changes from the remote repository. – larsks Jul 01 '22 at 12:00
  • 2
    I'm not sure if this is related to your question or not, but in step 1, you created `test` from a *remote tracking branch* (i.e. `origin/*`) and you didn't specify `--no-track`, so that means by default your local `test` branch will track `origin/master`. In step 3 when you pushed, you might have pushed your commits to `origin/master` instead of `origin/test` like you expected. – TTT Jul 01 '22 at 16:08
  • Please don't spam irrelevant tags – Mad Physicist Jul 02 '22 at 06:50

2 Answers2

1

First, when you create a branch, do so with git switch: git switch -c test origin/master, preferably after a git fetch (in order for origin/master to reflect the latest commit pushed there)

Then make sure your first push is git push -u origin test in order to establish a remote tracking branch origin/test, which will facilitate the subsequent pushes to that branch.

When you colleague do a git switch test (after a fetch), the default guess mode automatically establish a remote tracking branch (as if they typed git switch -c <branch> --track <remote>/<branch>)
That is why a simple git pull would be enough to update their local test branch after your step 5.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

The only way to do this is, as suggested by @larsks, to issue a git pull.

Instead, if anybody need to inspect changes before pulling them, he can:

git fetch --all
git diff HEAD..origin/<BRANCH-NAME>
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • Git pull or git pull origin test? Which out of these commands he should use and also can you please let me know the difference between two commands? – Eli Johnes Jul 01 '22 at 12:09
  • @EliJohnes https://stackoverflow.com/a/15799537/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+Difference+%22git+pull%22+and+%22git+pull+origin%22 – phd Jul 01 '22 at 12:21
  • Thanks. I have gone through the link you shared. One part which seems confusing to me is, if I do only git pull, the commits could come from master branch as well right, but we need the new changes from test branch only. – Eli Johnes Jul 01 '22 at 12:29
  • 2
    @EliJohnes: you generally *don't* want to use `git fetch --all`: that means *all remotes*, not all branches. (It already fetches all branches.) It's not harmful, it's just unnecessary, and people think it will affect `git pull` in ways that, well, it won't. Hence the "generally don't do it" advice. (If you know all about fetch and remotes, and really do want to fetch from all remotes, feel free to use it—although I'd generally recommend using `git remote update` here instead.) – torek Jul 01 '22 at 15:38