Showing posts with label GIT Configuration. Show all posts
Showing posts with label GIT Configuration. Show all posts

Thursday, 8 August 2019

GIT: HOW TO SET OR CHANGE USERNAME AND E-MAIL?

When we are using GIT, it’s advaisable to set username and e-mail so that we no need to set them everytime we use GIT to do changes. This post will show you on how to set username and e-mail for editing data with in git.
Check if there is any username and email set for your login to use GIT?
To know it we have to use git command or open ~/.gitconfig file.
Checking user name and password is set or not using git config command
 git config user.name

 git config user.email
Output:
 ○ → git config user.name

 Surendra Anne
And for email
 ○ → git config user.email

 surendra.anne@xyz.com
or check in ~/.gitconfig file for information
 grep -E 'name|email' ~/.gitconfig
How to set user name and e-mail for your login.
 git config --global user.name "your user name"

 git config --global user.email "Your e-mail"
Examples:
 git config --global user.name "John who"

 git config --global user.email "john.who@linuxnix.com"
Again to check these settings are saved or not open ~/.gitconfig file and see the changes.

Thursday, 27 December 2018

GIT: Define alias

46.1. Using an alias

An alias in Git allows you to create a short form of one or several existing Git commands. For example, you can define an alias which is a short form of your own favorite commands or you can combine several commands with an alias.

46.2. Alias examples

The following defines an alias to see the staged changes with the new git staged command.
git config --global alias.staged 'diff --cached'
Or you can define an alias for a detailed git log command. The following command defines the git ll alias.
git config --global alias.ll 'log --graph --oneline --decorate --all'
You can also run external commands. In this case you start the alias definition with a ! character. For example, the following defines the git ac command which combines git add . -A and git commit commands.
# define alias
git config --global alias.act '!git add . -A && git commit'

# to use it
git act -m "message"

GIT: Configure files and directories to ignore


8.1. Ignoring files and directories with a .gitignore file

Git can be configured to ignore certain files and directories for repository operations. This is configured via one or several .gitignore files. Typically, this file is located at the root of your Git repository but it can also be located in sub-directories. In the second case the defined rules are only valid for the sub-directory and below.
You can use certain wildcards in this file. * matches several characters. 
For example, the following .gitignore file tells Git to ignore the bin and target directories and all files ending with a ~.
# ignore all bin directories
# matches "bin" in any subfolder
bin/

# ignore all target directories
target/

# ignore all files ending with ~
*~
You can create the .gitignore file in the root directory of the working tree to make it specific for the Git repository.
The .gitignore file tells Git to ignore the specified files in Git commands. You can still add ignored files to the staging area of the Git repository by using the --force parameter, i.e. with the git add --force [paths] command.
This is useful if you want to add, for example, auto-generated binaries, but you need to have a fine control about the version which is added and want to exclude them from the normal workflow.
It is good practice to commit the local .gitignore file into the Git repository so that everyone who clones this repository have it.

8.2. Stop tracking files based on the .gitignore file

Files that are tracked by Git are not automatically removed if you add them to a .gitignore file. Git never ignores files which are already tracked, so changes in the .gitignore file only affect new files. If you want to ignore files which are already tracked you need to explicitly remove them.
The following command demonstrates how to remove the .metadata directory and the doNotTrackFile.txt file from being tracked. This is example code, as you did not commit the corresponding files in your example, the command will not work in your Git repository.
# remove directory .metadata from git repo
git rm -r --cached .metadata
# remove file test.txt from repo
git rm --cached doNotTrackFile.txt
Adding a file to the .gitignore file does not remove the file from the repository history. If the file should also be removed from the history, have a look at the git filter-branch command which allows you to rewrite the commit history.

8.3. Global (cross-repository) .gitignore settings

You can also setup a global .gitignore file valid for all Git repositories via the core.excludesfile setting. The setup of this setting is demonstrated in the following code snippet.
# Create a ~/.gitignore in your user directory
cd ~/
touch .gitignore

# Exclude bin and .metadata directories
echo "bin" >> .gitignore
echo ".metadata" >> .gitignore
echo "*~" >> .gitignore
echo "target/" >> .gitignore
# for Mac
echo ".DS_Store" >> .gitignore
echo "._*" >> .gitignore

# Configure Git to use this file
# as global .gitignore

git config --global core.excludesfile ~/.gitignore
The global .gitignore file is only locally available.

8.4. Local per-repository ignore rules

You can also create local per-repository rules by editing the .git/info/exclude file in your repository. These rules are not committed with the repository so they are not shared with others.
This allows you to exclude, for example, locally generated files.

8.5. Tracking empty directories with Git

Git ignores empty directories, i.e., it does not put them under version control. If you want to track an empty directory in your Git repository, it is a good practice to put a file called .gitignore in the directory. As the directory now contains a file, Git includes it into its version control mechanism.
The file could be called anything. Some people suggest to call the file .gitkeep. One problem with this approach is that .gitkeep is unlikely to be ignored by build systems. This may result in the .gitkeepfile being copied to the output repository, which is typically not desired.

GIT: Git configuration

7.1. Git configuration levels

The git config command allows you to configure your Git settings. These settings can be system wide, user or repository specific.
A more specific setting overwrites values in the previous level. A setting for the repository overrides the user setting and a user setting overrides a system wide setting.

7.1.1. Git system-wide configuration

You can provide a system wide configuration for your Git settings. A system wide configuration is not very common. Most settings are user specific or repository specific as described in the next chapters.
On a Unix based system, Git uses the /etc/gitconfig file for this system-wide configuration. To set this up, ensure you have sufficient rights, i.e. root rights, in your OS and use the --system option for the git config command.

7.1.2. Git user configuration

Git allows you to store user settings in the .gitconfig file located in the user home directory. This is also called the global Git configuration.
For example Git stores the committer and author of a change in each commit. This and additional information can be stored in the Git user settings.
In each Git repository you can also configure the settings for this repository. User configuration is done if you include the --global option in the git config command.

7.1.3. Repository specific configuration

You can also store repository specific settings in the .git/config file of a repository. Use the --local or use no flag at all. If neither the --system not the --global parameter is used, the setting is specific for the current Git repository.

7.2. User credential configuration

You have to configure at least your user and email address to be able to commit to a Git repository because this information is stored in each commit.
# configure the user which will be used by Git
# this should be not an acronym but your full name
git config --global user.name "Firstname Lastname"

# configure the email address
git config --global user.email "your.email@example.org"

7.3. Push configuration

If your are using Git in a version below 2.0 you should also execute the following command.
# set default so that only the current branch is pushed
git config --global push.default simple
This configures Git so that the git push command pushes only the active branch to your Git remote repository. As of Git version 2.0 this is the default and therefore it is good practice to configure this behavior.

7.4. Avoid merge commits for pulling

By default, Git runs the git fetch followed by the git merge command if you use the git pull command. You can configure git to use git rebase instead of git merge for the pull command via the following setting.
# set default so that you avoid unnecessary commits
git config --global branch.autosetuprebase always
This setting helps avoiding merge commits during the pull operation which synchronizes your Git repository with a remote repository. The author of this description always uses this setting for his Git repositories.

7.5. Allow rebasing with uncommited changes

If you want Git to automatically save your uncommited changes before a rebase you can activate autoStash. After the rebase is done your changes will get reapplied. For an explanation of git stash 
git config --global rebase.autoStash true
Before Git v2.6 git pull --rebase didn’t respected this setting.

7.6. Color Highlighting

The following commands enables color highlighting for Git in the console.
git config --global color.ui auto

7.7. Setting the default editor

By default Git uses the system default editor which is taken from the VISUAL or EDITOR environment variables if set. You can configure a different one via the following setting.
# setup vim as default editor for Git (Linux)
git config --global core.editor vim

7.8. Setting the default merge tool

File conflicts might occur in Git during an operation which combines different versions of the same files. In this case the user can directly edit the file to resolve the conflict.
Git allows also to configure a merge tool for solving these conflicts. You have to use third party visual merge tools like tortoisemerge, p4merge, kdiff3 etc. A Google search for these tools help you to install them on your platform. Keep in mind that such tools are not required, you can always edit the files directly in a text editor.
Once you have installed them you can set your selected tool as default merge tool with the following command.
# setup kdiff3 as default merge tool (Linux)
git config --global merge.tool kdiff3

# to install it under Ubuntu use
sudo apt-get install kdiff3

7.9. More settings

All possible Git settings are described under the following link: git-config manual page

7.10. Query Git settings

To query your Git settings, execute the following command:
git config --list
If you want to query the global settings you can use the following command.
git config --global --list