How can I edit my Git config? - macos

I feel like this issue has been addressed before, but I couldn't find it after search through a bunch of posts.
I am doing a beginner's tutorial on Git. Someone had this Mac before me and used multiple Git Applications (SourceTree, etc) that seem to have added all kinds of config preferences.
When I type git config --list, I get an enormous list (shown below), unlike the tutorial that I am watching on Lynda.com where the author only shows a list of user.name and user.email.
core.excludesfile=~/.gitignore
core.legacyheaders=false
core.quotepath=false
core.pager=less -r
mergetool.keepbackup=true
push.default=simple
color.ui=auto
color.interactive=auto
repack.usedeltabaseoffset=true
alias.s=status
alias.a=!git add . && git status
alias.au=!git add -u . && git status
alias.aa=!git add . && git add -u . && git status
alias.c=commit
alias.cm=commit -m
alias.ca=commit --amend
alias.ac=!git add . && git commit
alias.acm=!git add . && git commit -m
alias.l=log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'
alias.ll=log --stat --abbrev-commit
alias.lg=log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
alias.llg=log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commit
alias.d=diff
alias.master=checkout master
alias.spull=svn rebase
alias.spush=svn dcommit
alias.alias=!git config --list | grep 'alias\.' | sed 's/alias\.\([^=]*\)=\(.*\)/\1\ => \2/' | sort
include.path=~/.gitcinclude
include.path=.githubconfig
include.path=.gitcredential
diff.exif.textconv=exif
credential.helper=osxkeychain
mergetool.sourcetree.cmd=--null
user.name=username
user.email=email
Ultimately, I would like to remove all of these additional settings. I tried uninstalling Git, but that didn't make a difference. Is there a command to undo all of these? Any help would be appreciated.

Your Git configuration is spread across up to three config files (one for each level).
You can edit your config (add/remove entries) by editing those files, or by using the right git-config commands (more robust, but possibly more tedious). The problem is simply to locate which part of your config is located in which config file. Instead of running
git config --list
which lists the contents of all three files (i.e. your entire Git configuration), you can run three different commands for listing the contents of each of those three config files.
System-level config file
Usually located at /usr/local/etc/gitconfig; at least, that's where mine is on my Mac (I installed Git with Homebrew). You can list its contents by running:
git config --system --list
User-level config file
Usually located at $HOME/.gitconfig. You can list its contents by running:
git config --global --list
Repository-level config file
Usually located at <repo-root-folder>/.git/config. You can list its contents by running:
git config --local --list
If you only want to retain the user.name and user.email fields from your current config and get rid of all the rest, the easiest thing is to delete all three config files and then simply run
git config --global user.name <your-name>
git config --global user.email <your-email>
(The user.name and user.email fields are most commonly set in the user-level config file.)

Related

Git doesn't create .gitconfig when it's inside a function

I'm a fairly new user in Bash and Git in general and I'm scratching my head about what the problem could be. I'm creating a code that checks if .gitconfig exists and if it doesn't it allows you to configure it almost automatically using a read command to get your email and username and apply them in a line of code.
Code example:
#!/bin/bash
# colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NO_COLOR='\033[0m'
# function git
git () {
printf "${RED}Set your Git email\n${NO_COLOR}"
read GIT_AUTHOR_EMAIL
git config --global user.email "$GIT_AUTHOR_EMAIL"
printf "${RED}Set your Git username\n${NO_COLOR}"
read GIT_AUTHOR_USERNAME
git config --global user.name "$GIT_AUTHOR_USERNAME"
git config --list | grep user.email && git config --list | grep user.name
}
# git check & configuration
if [ -f ".gitconfig" ]; then
printf "${YELLOW}Git was previously configured\n${NO_COLOR}"
exit
else
git
printf "${YELLOW}Done\n${NO_COLOR}"
exit
fi
If it doesn't exist, it calls a function to configure it but after some quick debugging using the set -x command I figured out the file .gitconfig is not created at all but it does when I do it myself outside a function. All it does is go back to read GIT_AUTHOR_EMAIL, apply the code git config --global user.email "$GIT_AUTHOR_EMAIL" and go back to the first read command. I want the code to check for .gitconfig and if it exists it'll also ask if the user wants to re-configure their Git details. I'm super close on doing so.
Is there any way I can fix it or do it another way?
Once you've defined the function git, any invocation of the name git in that shell process will refer to that shell function. If you'd like to invoke the program git, then you need to prefix it with the built-in command:
command git config --global user.name "$GIT_AUTHOR_USERNAME"
If it wasn't your intention to override the git command, then you probably want to name your shell function differently, which avoids the problem altogether.

Creating git aliases

I am trying to add the following aliases in ubuntu
alias l=log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
$ source ~/.aliases
bash: alias: --decorate: not found
bash: alias: --decorate: not found
bash: alias: --numstat: not found
I could use this command outside with git
I am not so sure why? Can someone help me? I tried googling but I did not go far with it. I do not know bash so much.
This is bit older question but it is very important to understand and create git alias as this will save lot of time of yours.
In your question you are close to answer just a silly mistake done is you are trying to create alias using script.
Alias needs to be defined in .gitconfig file. Not just alias but all config part like
[core], [color], [pack], [help], [alias] etc
I would like to share some basic and useful alias with you to have things handy and you can change it further per your need and daily usage
[alias]
lg = log -p
lol = log --graph --decorate --pretty=oneline --abbrev-commit
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
st = status
co = checkout
ci = commit -a -m
br = branch
ls = ls-files
po = push origin
f = fetch
p = pull
delete = branch -d master
com = checkout master
cob = checkout -b
unstage = reset HEAD
url = remote set-url origin
ign = ls-files -o -i --exclude-standard
cp = cherry-pick
You can also create an alias for a combination of multiple git commands in a single one as, for instance:
rdev = !git checkout dev && git pull && git checkout - && git rebase dev
Let me know if any other understanding needed.
You are almost there. You just need to put the alias in the right file. Because Git doesn’t automatically infer your command if you type it in partially, you can easily set up an alias for each command using git config like so:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
And then you use it the aliases like: git ci, git co, git br, git st in any repo.
You can also run an external command through an alias. In that case, you start the command with a ! character. This is useful if you write your own tools that work with a Git repository:
git config --global alias.visual '!gitk'
You might have also noticed that the config command takes in several parameters (like the --global one). If we look at the docs man git config:
For writing options: write to global ~/.gitconfig file rather than the repository .git/config, write to $XDG_CONFIG_HOME/git/config file if this file exists and the ~/.gitconfig file doesn’t.
For reading options: read only from global ~/.gitconfig and from $XDG_CONFIG_HOME/git/config rather than from all available files.
See also the section called “FILES”.
There is also --system, which writes to /etc/gitconfig, --local, for the local repo .git/gitconfig, and --worktree, which is similar to --local.
But you can just directly edit the files themselves. It will look similar to this:
# in ~/.gitconfig
[alias]
lg = log --all --stat --pretty=oneline --graph --format='%h %d %an %cr %s' --oneline
l = log --all --stat --graph --format='%h %d %an %cr %s'
up = pull --rebase
br = branch --verbose -a
sfp = push --force-with-lease
You should set the alias in your git aliases and use it from the command line
You can directly edit the configuration file or do it from CLI:
Git Alias
Use the git config --global alias.<name> in order to add git alias
git config --global alias.l 'log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate'
now you should be able to use it with: git l
Ubuntu Alias
If you wish to add an alias to your shell in Ubuntu:
alias gitl='git l'

Pre commit hook solution

I'm looking for a solution where before I push, it determines what host it's pushing to and then changes to the appropriate git user. For example if I'm pushing to a bitbucket then I should use
example#bitbucket.com
if pushing to git then I should use
example#github.com.
Links to similar solutions or just high level of the approach is fine.
currently I have a switch_user bash script that I use to toggle between usernames, but I could forget this.
Following github's help page, you can set the email that you commit as in the git repository with git config user.email "email#example.com". The lack of the --global flag makes it so that it only applies to this particular repository.
If you want to do this automatically for new projects you add, I recommend adding a git alias script to figure it out for you. Something like git smart-clone and git smart-remote-add.
So I found out I could actually do a global hook
On your global gitconfig which can be found here
$ vi ~/.gitconfig
Edit the core.hookspath
hookspath=/path/to/git-hooks
Then I did something like this
#!/bin/sh
File=$PWD/.git/config
if grep -q github.com "$File"; then
git config --global user.name "Foo Bar"
git config --global user.email example#github.com
else
git config --global user.name "Bar Foo"
git config --global user.email example#bitbucket.com
fi
git commit nom_rep
git push nom_rep or fichier // pour ajouter les fichier
git status

Multiple git commands in single command executed in order they are encountered by compiler

I have following list of commands that I run in respective order so that a source project can be committed and pushed to the repository on Bitbucket:
git init
git remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git
git config user.name "[BitBucket Username]"
git config user.email "[BitBucket Email ID]"
## if email doesn't work then use below ##
git config --global user.email \<\>
git add *
git commit -m "[Comment]"
git push -u origin master
Now instead of putting each and every line at their respective time and order, I want to know, if there is a possibility that I can chain all these into single git command and maintain the same order, something like below ?
git init remote add origin https://[BitBucket Username]#bitbucket.org/[BitBucket Username]/[BitBucket Repository Name].git config user.name "[Username]" ....
Or atleast combine multiple same category params like below ?
git config user.name "[BitBucket Username]" user.email "[BitBucket Email ID]"
I need to know possibility of both scenarios with examples.
We can use list off command in single command for example:
git add . && git commit -m "updated pom file" && git push
or:
git stash pop && git add . && git commit -m "updated pom file" && git push
&& -> if 1st command successfully executes then execute next command else it won't execute next command.
& - it executes all the command
|| - execute next command if 1st one failed
If you are in a Windows Powershell:
git add . ; git commit -m "Testing one line command" ; git push
I have gitbash on Windows system and I am not as good with Win batch as with Linux shell.
You still can write a bash script (interpreted by the msys2 bash embedded with Git for Windows).
As mentioned in the comments by Lasse V. Karlsen, and as I mentioned before in "Which shell used for git '!' aliases?", you can write a shell script in a file (in your %PATH%) named git-xxx, and call it with git xxx.
That script would begin with:
#!/bin/bash
I created a file called reset.txt and in that file I have the commands
git reset --hard
git clean -d -f
[this is a newline - very important to have it]
I just copy and paste this into my terminal and it executes the commands in order.

Add new command on Windows Git Bash

I would like to add new command on my Git Bash (just now i am under Windows OS). I tryed to look on Web different solutions but I did not find anything. The command that i like to add is:
commitall -> git add --all && git commit -m "$*"
There is a way to add this command on Windows Git Bash?
Thanks
Use Git aliases, like so:
git config --global alias.commitall '!git add --all && git commit -m'
There's no need to use $* because all the arguments you specify will simply be appended to the line above, i.e. if you run:
git comitall "a message"
…the following will be executed:
git add --all && git commit -m "a message"

Resources