git-commands.md 2.0 KB

Git Commands

I use git almost daily so I don't really need my cheat-sheet anymore, but there are always instances that call for uncommon scenarios.

Taking note of these, so I don't have to dig for them the next time they're needed.

See unpublished commits

Stuff that got added after git add filename

git cherry -v

Undo an unpublished commit (without losing the code changes)

git reset --soft HEAD~;

Delete a Commit from the History (Plus it's contents)

If you committed a mistake to the repo that shouldn't stay in the history; a hard reset can clean it up - providing it's executed rapidly. If other systems/users have already received this commit, this will cause a mess.

For use in situations where you know nobody pulled, yet:

  1. Run git log and get the commit hash of the commit you want to return to (delete everything else after)
  2. Reset it:

    git reset --hard [commit hash]
    
  3. Force push it to the repo:

    git push -f
    

All should be well


Specify an SSH Key During git push

This error:

Pushing to git@bitbucket.org:username/repo.git

Forbidden

fatal: Could not read from remote repository.

typically occurs when Bitbucket doesn't find the key it expects to see. (id_rsa, usually)

  • Host = alias for bitbucket.org
  • HostName = actual destination for the alias
  • IdentityFile = path to your preferred SSH key
  • IdentitiesOnly = override sending the wrong key for multiple users/identities (identities are tried in sequence)

In your local ~/.ssh/config:

Host bblocal
HostName bitbucket.org
IdentityFile ~/.ssh/keyname
IdentitiesOnly yes

In the repo's .git/config:

  • All spots where you have entries like git@bitbucket.org:username/repo.git change bitbucket.org to your alias: bblocal

Example

From:

url = git@bitbucket.org:username/repo.git
pushurl = git@bitbucket.org:username/repo.git

To:

url = git@bblocal:username/repo.git
pushurl = git@bblocal:username/repo.git