[Git] Git Switch & Fetch & Pull

1 minute read

Git Switch & Fetch & Pull

  • Remote Tracking Branches
  • Git Switch
  • Git Fetch
  • Git Pull

Remote Tracking Branches

  • Where the branch was pointing at the time the user last communicated with the remote repository
  • The pattern is {remote}/{branch_name}
    • e.g. origin/master
# view the remote branches the local repository knows about
git branch -r

# move to the branch where the remote tracking HEAD is pointing to (git checkout {remote}/{branch_name})
git checkout origin/master

Git Switch

  • Create new local branch from the remote branch of the same name & set up to track the remote branch(link the branch with the remote branch)
  • It is possible to move to the branch with git checkout {remote}/{branch_name}
    • But in this case, it will be a detached HEAD
# git switch {branch_name}
git switch feat/branch_name

# same as above(git checkout --track {remote}/{branch_name})
git checkout --track feat/branch_name

Git Fetch

  • Fetching allows us to download changes from a remote repository
  • However, those changes will not be automatically integrated into the working files
  • In Short, git fetch {remote}retrieves the latest information from remote repository(e.g GitHub) but doesn’t affect the working files
  • Latest information includes new branches, and commits(files) inside the branches
# git fetch {remote}, if remote is not specified origin is the default
git fetch origin
# same as above
git fetch

# fetch as specific branch (git Fetch {remote} {branch})
git fetch origin feat/branch_name

# go to that branch(in a 'detached HEAD' state)
git checkout feat/branch_name


Git Pull

  • Retrieve changes from a remote repository, and updates the HEAD branch
    • Similar to git fetch + git merge
  • It matters on which branch this command is executed
# git pull {remote} {branch}
# fetch the lastest information from the origin's master branch and merge those changes into the current branch
git pull origin master

# git pull
# the default remote: is origin
# the default branch: the tracking connection configured to the current branch

Tags:

Categories:

Updated: