Git

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/xinzhong/.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

Config

Config file location:

  • Global: ~/.gitconfig
  • System: /etc/gitconfig
  • Local: /git/configcoding

Set a name

$ git config --global user.name "My Name"
$ git config --local user.name "Foo Bar"

Show the name

$ git config --local user.name
Foo Bar

Checkout a Remote Branch

$ git fetch
$ git checkout test

or

$ git checkout -b <branch> --track <remote>/<branch>

Tags

List all tags:

$ git tag -l
1.0.0

Checkout a tag:

$ git checkout tags/<tag_name>
$ git checkout tags/1.0.0

Remove a tag:

# remove local tag
$ git tag -d <tag_name>

# remove remote tag
$ git push origin :[tag]
$ git push origin :refs/tags/<tag_name>

Add All

To add everything to stage, including additions and removals, use

$ git add -A

Abandon Changes

Abandon changes and revert your tree to the "clean" state of your current branch. Don't use 'git revert', use:

$ git reset --hard HEAD

Reset to a remote branch

$ git fetch origin
$ git reset --hard origin/master

Mac OS X Uses Wrong Git Account

If multiple GitHub accounts are logged in using Mac OS X, the account info might be stroed by Keychain Access. Maven release plugin may fails for using wrong Github account.

[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] remote: Permission to <...>/<...>.git denied to <...>.
[ERROR] fatal: unable to access 'https://github.com/<...>/<...>.git/': The requested URL returned error: 403

Solution: find Keychain Access in spotlight or by

Applications->Utilities->Keychain Access.app

Search for github and remove the records.

Upstream Remote

Add your own fork as origin remote

$ git remote add origin https://github.com/<your_fork>/<project_name>.git

Add the central repo as upstream remote

$ git remote add upstream https://github.com/<central_repo>/<project_name>.git

To sync up with upstream

$ git fetch upstream
$ git merge upstream/develop

Or

$ git pull upstream develop

Commit in Wrong User/Email

If user is specified in .git/config as

[user]
    name = your_name
    email = [email protected]

however if git is using the wrong user settings after commit

Author: your_name <[email protected]>

Check if you have these settings in env

  • GITAUTHORNAME
  • GITAUTHOREMAIL
  • GITCOMMITERNAME
  • GITCOMMITEREMAIL

Remove those then try again

How do I make Git ignore file mode (chmod) changes?

git config core.fileMode false

or

git config --global core.filemode false

or open .git/config and modify the core section.