diff --git a/dulwich/repo.py b/dulwich/repo.py
index 7ebeec8e..ddedb7ac 100644
--- a/dulwich/repo.py
+++ b/dulwich/repo.py
@@ -168,9 +168,13 @@ def get_user_identity(config, kind=None):
             email = None
     default_user, default_email = _get_default_identity()
     if user is None:
-        user = default_user.encode('utf-8')
+        user = default_user
+        if not isinstance(user, bytes):
+            user = user.encode('utf-8')
     if email is None:
-        email = default_email.encode('utf-8')
+        email = default_email
+        if not isinstance(email, bytes):
+            email = email.encode('utf-8')
     return (user + b" <" + email + b">")
 
 
