Git - Versus

Last Updated: 2023-01-15

git fetch vs git pull

git pull = git fetch + git merge

  • git fetch: remote repo to local repo.
  • git merge: local repo to working directory.
  • git pull: remote repo to local repo AND working directory.

check if local repo is up to date

$ git fetch --dry-run

git checkout vs git switch

git switch -c <branch name> is equivalent to git checkout -b <branch name>.

To replace two common uses of git checkout: git switch to switch to a new branch after creating it if necessary, and git restore to restore changes from a given commit.

git rebase vs git merge

Both are designed to integrate changes from one branch into another branch.

  • git merge feature main will create a new "merge commit" in the feature branch to incorporate chagnes in the main branch.
  • git rebase main will move the feature on top of the main branch (incorporating all new commits in the main branch.) Much cleaner project history.

Patch a CL: checkout vs branch vs cherry-pick vs pull

before patch:


Checkout and create a new branch

$ git fetch foo bar && git checkout -b baz FETCH_HEAD

You will be in a new baz branch


$ git fetch foo bar && git checkout FETCH_HEAD

You will be in a HEAD detached at FETCH_HEAD branch (use git checkout -b baz to create a new baz branch)


$ git fetch foo bar && git cherry-pick FETCH_HEAD

You will stay in the main branch, check git log:


Pull and rebase

$ git pull foo bar --rebase

You will stay in the main branch, check git log:


Pull and merge

$ git pull foo bar --no-rebase (i.e. merge)

You will stay in the main branch, check git log:


https vs ssh

You can use either https or ssh to connect to remote repo:

  • https:
    • https://github.com/foo/bar.git
    • it will ask for your username and password when you try to connect remote repo
  • git:
    • [email protected]:foo/bar.git
    • it uses ssh keys to verify you identity.
      • Copy and paste your public key(~/.ssh/id_rsa.pub) to Github account
      • Specify the private key in ~/.ssh/config as
Host heroku.com
HostName heroku.com
IdentityFile /Users/username/.ssh/heroku_key

To check your remote

$ git remote show origin
* remote origin
    Fetch URL: https://github.com/foo/bar.git
    Push  URL: https://github.com/foo/bar.git
    HEAD branch: master