Git configuration

If you use 2-factor authentication in GitHub,  generate a 40 character Personal Access Token that you can use as a password to access GitHub repositories.

Create a Personal Access Token to use it as password in the Git client

  1. Log into GitHub and in the pull down at the upper right select Settings,

  2. On the left select Developer Settings,
  3. On the left select Personal access tokens,
     
  4. Click the Generate new token button,
  5. Enter a description for your token (so you can keep track and revoke them individually later, should you have a security breach),
  6. Select the repo checkbox,
  7. Click the Generate token button,
  8. Copy the token (40 characters long) to the clipboard and use that as your password in the command line.
  9. If your organization use SSO (Single Sign On) click the Enable SSO button
  10. This will open your organization’s authentication pages to enable access for your token.

Store or update your username and password in the Git credential helper


In macOS and Linux

Set the credential helper to store your username and password

To store the Git credentials on your hard disk in unencrypted form, use the Git Credential Store

git config credential.helper store

When you have already stored your password in the credential helper of Git and switching to 2-factor authentication, or just want to update your GitHub username or password


In Windows

  • In the command line execute the following to instruct the Git client to use the Wincred Windows credential helper to store your username and password
    git config --global credential.helper wincred
  • The next time you will access the GitHub repository to fetch, clone or push changes the Git CLI will pup up a dialog box to ask your username and password.

To update an already stored username or password in the Windows Credential Manager

When you access GitHub from your Windows workstation, Windows stores your credentials in the Credential Manager. If you want to enter an updated password, enter your personal access token when you switch to 2-factor authentication or switch between GitHub accounts, delete the GitHub entry from the Credential Manager.

  1. Open the Windows Credential Manager
    1. Open a command window
    2. Execute
      control /name Microsoft.CredentialManager
  2. On the Windows Credentials tab click the down arrow next to github.com
  3. Click the Remove link
  4. Click Yes to delete the link.

Disable database triggers in Microsoft SQL databases

If you need to temporarily disable triggers in Microsoft SQL databases during database maintenance use the following script
--Disable triggers on all tables
DECLARE @enable BIT = 0;
DECLARE @trigger SYSNAME;
DECLARE @table SYSNAME;
DECLARE @cmd NVARCHAR(MAX);
DECLARE trigger_cursor CURSOR FOR SELECT trigger_object.name trigger_name,
table_object.name table_name
FROM sysobjects trigger_object
JOIN sysobjects table_object ON trigger_object.parent_obj = table_object.id
WHERE trigger_object.type = 'TR';
OPEN trigger_cursor;
FETCH NEXT FROM trigger_cursor INTO @trigger, @table;
WHILE @@FETCH_STATUS = 0 BEGIN
IF @enable = 1
SET @cmd = 'ENABLE ';
ELSE
SET @cmd = 'DISABLE ';
SET @cmd = @cmd + ' TRIGGER dbo.'+QUOTENAME(@trigger)+' ON dbo.'+QUOTENAME(@table)+' ';
EXEC (@cmd);
FETCH NEXT FROM trigger_cursor INTO @trigger, @table;
END
CLOSE trigger_cursor;
DEALLOCATE trigger_cursor;
GO

Disable foreign key constraints in Microsoft SQL databases

Databases do not allow the deletion of rows if those are referenced in other tables with the foreign key constraint. You can turn off the validation of foreign keys in Microsoft SQL databases for the duration of the maintenance with the following script.


--Disable foreign keys on all tables
DECLARE @table_name SYSNAME;
DECLARE @cmd NVARCHAR(MAX);
DECLARE table_cursor CURSOR FOR SELECT name FROM sys.tables;
OPEN table_cursor;
FETCH NEXT FROM table_cursor INTO @table_name;
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @cmd = 'ALTER TABLE '+QUOTENAME(@table_name)+' NOCHECK CONSTRAINT ALL';
EXEC (@cmd);
FETCH NEXT FROM table_cursor INTO @table_name;
END
CLOSE table_cursor;
DEALLOCATE table_cursor;
GO

Display the branch structure and other important events in the Git command line interface

To set up the display of the branch structure, tags and pulls in the command line interface first create the following “alias”

git config --global alias.lgb "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches"

To display the branch structure, execute the following “alias”

git lgb

This will execute the above defined “alias” and display the most important events of the branch.

Press q to exit from the display mode to the $ prompt.

Include PBD files in ClickOnce deployment to show the line numbers in the Stack Trace

If there is an error in an application, the line numbers are only included in the Stack Trace when the PBD files are also in the application directory.

To include the PBD files in an application that was distributed with ClickOnce deployment you need to:

  • Right click the main project and select Properties
  • On the Build tab
    • Uncheck Optimize Code to make sure the line numbers in the Stack Trace match the source code
    • Click the Advanced… button at the bottom of the window
    • Set Debug info to full
      VS PBD 01

  • On the Publish tab click the Application Files… button
    • On the Application Files window select the Show all files check box in the lower right corner
    • Change the Publish Status of the PBD files from Exclude (Auto) to Include
      VS PBD 02
    • Click OK to save the changes

How to get rid of the boring unwanted picture in Skype

Since Microsoft purchased Skype there is an annoying, unwanted advertisement on the main page. If you are tired of seeing the same boring picture every time you turn on Skype

  • In the Tools menu select Change language,
  • At the bottom of the list click Edit Skype Language File,
  • In the Language File Editor click Save as and save the copy of your current language file with any name, like NoPicture.
    Skype will try, but for security reasons cannot save the file in the Skype folder, and it will offer an alternate location: c:\Users\[YOUR USER NAME]. This is perfect for us.
  • Open the Tools -> Change language menu option again,
  • Click the Load language file at the bottom of the list,
  • Select the file you just saved,
  • In the File menu select Sign Out to exit Skype,
  • Start Skype again (it will ask your password).

The picture is gone.

Branching and merging in Git

Branching and Merging are very powerful features in Git.

There are may branching strategies, my favorite is well explained at http://nvie.com/posts/a-successful-git-branching-model/

Pick any model you like and stick to it for a while to better understand and evaluate it. If you do not like that try another one.

In this model when we start to work on a new feature we

  • Create a new branch (let’s call it feature) based on the develop branch and switch to it.
    • git checkout develop
      git branch feature
      git checkout feature
  • Develop the new feature in the feature branch and add the changes to the repository.
    • git add .
      git commit -m "Feature is done in feature branch"
  • Before merging the changes of the feature branch back into the develop branch get the latest changes from the develop branch, apply your changes on top of them and test the application
    • git rebase develop
  • If there are merge conflicts, launch the Merge Tool. See Merging in Git with four panels in Windows  to set up and use a great merge tool.
    • git mergetool
  • It is possible that you will encounter merge conflicts in the same file multiple times. Just repeat the git mergetool command as many times as needed.
  • When you are done with merging, complete the rebase
    • git rebase --continue
  • Add and commit your changes in the feature branch
    • git add .
      git commit -m "Feature is tested in feature branch"
  • When you have tested the code of the new feature, merge it back to the develop branch.
    Switch back to the develop branch and merge the changes from the feature branch.

    • git checkout develop
      git merge feature
  • If there are any merge conflicts, Git will ask you to resolve them.

Merging in Git with four panels in Windows

Git is a great repository for small and large projects. It is easy to create and merge branches to separate code for the features you work on. To make merging easier you can use a free 4 panel merging tool, Perforce P4Merge.

Download

  • Download the Perforce P4Merge Visual Merge Tool from https://www.perforce.com/downloads/integrations
  • Select the operating system of your computer and click the Download button.

Install Perforce P4Merge

  • Double click the downloaded EXE file
  • Unselect the Administration tool
    P4Merge 01
  • Leave the server as is, we will not use it
    P4Merge 02
  • Just click OK, the address is not important for us
    P4Merge 03
  • Click Next again on the Client Configuration page
    P4Merge 04
  • On a Windows machine P4Merge will be installed at C:\Program Files\Perforce. Git cannot access P4Merge if the path contains a space character, so once the installation is done move the Perforce folder to the root of the C: drive.

Add the Perforce P4Merge settings to the Git config file

  • To use the same tool in every repository navigate to C:\Users\[YOUR USER NAME] and open the .gitconfig file.
  • If you already have [merge] and [mergetool “p4merge”] entries update them, if not, add the following lines. Even on Windows machines you have to use forward slashes (/) in the path for Git to understand it.
    [merge]
      tool = p4merge
    
    
    [mergetool "p4merge"]
      cmd = "C:/Perforce/p4merge" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
      keepTemporaries = false
      trustExitCode = false
      keepBackup = false

Solve merge conflicts

  • If you use the Git command line merge the branches. Git is case sensitive.
    • Change to the target branch
      • git checkout master
    • Merge the changes from the feature branch
      • git merge Feature
    • If there is a merge conflict type
      • git mergetool
    • Git will display the name of the file that caused the merge conflict
      • $ git mergetool
        Merging:
        Test1.txt
        Normal merge conflict for 'Test1.txt':
          {local}: modified file
          {remote}: modified file
        Hit return to start merge resolution tool (p4merge):
    • Hit Enter and the P4Merge opens with four panels
      • The left (Local) panel shows the changes in the current (target) branch,
      • The middle panel shows the original (Base) version of the file before both changes,
      • The right panel shows the coming changes (Remote) that were made in the branch you are merging from,
      • The bottom shows the result of the merge.
    • Change the file to resolve the conflict and click the Save button to save the result.

Merging is safe, because Git creates backup copies of all three version of the file in the same folder. If there are multiple files with merge conflicts Git will open the merge tool for each of them one-by-one.

  • Add the changes to the next commit
    • git add .
  • Commit the changes
    • git commit -m “Merged with P4Merge”

How to delete a folder from Git, but not from the local disk

I have accidentally included a folder in my Git repository, that I don’t want to include in source control. I wanted to remove it from the Git index, but not from the local hard drive.

The solution is the following command

git rm -r --cached myFolder

If the folder name contains spaces enclose it in quotes "my folder"

This will remove the folder from the Git repository, but leave it on the local drive.