Display all teams you belong to in GitHub

To get the information on all teams you are part of in Github

  1. Open a Terminal window
  2. Execute this command with your Personal Access Token
curl -H "Authorization: token YOUR_PERSONAL_ACCESS_TOKEN" https://api.github.com/user/teams

The returned JSON will display all information on the teams you belong to.

To generate a personal access token

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

GitKraken installation and configuration

GitKraken is a Git user interface to manage Git repositories.

Installation

  1. Download the GitKraken installer from https://www.gitkraken.com/download

Configuration

  1. Start the GitKraken application
  2. Login with your GitHub account, or create a new account

    or
  3. Connect to a Git repository
  4. Click the Open a Repository button
  5. Select a repository folder and click the Open button

Using Git

Frequently used Git commands

Git runs entirely on your workstation, and a copy of the entire repository is also on your local hard drive. GitHub, BitBucket, and other providers only give you a storage space to allow you to share your repository with others and provide a web user interface to manage it. You can use any provider’s application on your workstation to manage any of your Git repositories. Many developers use SourceTree, the great application written by Atlassian, the owner of BitBucket, to manage Git repositories on their workstations that are shared at GitHub.

Here are the most frequently used Git commands.

Create the local repository

Initialize a new Git repository in the current directory. This command creates the .git sub-directory to store your repository and its configuration file.

git init

Display the local repository status

Display the list of added, deleted and modified files in the local repository

git status

Display your changes in the files

Display the changes in files since the last git add

git diff

Display the changes in the local stage area after you have executed  git add

git diff --staged

Stage your changes in the local repository

Add your changes to the local stage area

git add .

Save the changes to the repository

Commit your changes from the local stage area to the local repository with a message

git commit -m "My message"

Edit the last commit message

You can edit the last commit message even after the push.

git commit --amend

When you execute git status you get the message

Your branch and ‘origin/master’ have diverged,
and have 1 and 1 different commits each, respectively.
(use “git pull” to merge the remote branch into yours)
nothing to commit, working tree clean

To synchronize your local repository with the remote, execute

git pull

The merge window pops up. You can leave the default message, or type your explanation. To save your message and close the window

  1. Press the ESC key on your keyboard
  2. Press the keys (including the colon at the beginning) :wq

Push your changes to the remote

git push

Send your changes to the remote repository

Push the changes to the remote repository at GitHub, Bitbucket, or others

git push

Get the latest from the remote repository

Pull the latest changes from the remote repository

git pull

Advanced topics

Push an existing repository from the command line

git remote add origin https://github.com/ORGANIZATION/REPOSITORY_NAME.git
git push -u origin master

Branching

Forks and Pull Requests

 

Custom Git Status and Git Pull commands to work with all of your repositories

If you work with many Git repositories it is easy to miss a repository when you commit your changes, and very time consuming to pull from all repositories.

It is easy to automate both processes.

In this example, we will place all files in the ~/Git/devops-scripts folder. If you want to place your files at another location change it in the files below.


\/  \/  \/  \/  \/  \/  \/  \/  \/  \/

In Windows

To find your home directory on a Windows workstation, open a Bash window (Git Bash) and execute

echo ~

/\  /\  /\  /\  /\  /\  /\  /\  /\  /\


Create the Bash script files

  • Create a file with the list of Git repositories and save it as ~/Git/devops-scripts/git-repositories.txt
    You can separate groups of repositories with empty lines.
# Local_Path+Remote_Path+Rename_the_folder_to(optional)
~/Git/MY_REPO1+https://github.com/MY_REPO1.git+RENAME_TO1
~/Git/MY_REPO2+https://github.com/MY_REPO2.git

~/Git/MY_REPO3+https://github.com/MY_REPO3.git
  • Create a file to check the status of all Git repositories and save it as ~/Git/devops-scripts/git-status.sh
#!/bin/bash

# git-status.sh
# Shows the GIT STATUS of the Git repositories and shows the recommended actions
# The list is in git-repositories.txt

eval input="~/Git/devops-scripts/git-repositories.txt" # Need EVAL, otherwise the Bash loop cannot interpret ~ as the home directory if the path contains ~

RED='\033[0;31m'
NC='\033[0m' # No Color

while IFS= read -r folder
do

 # Get the length of the path
 # On Windows the empty line manifests itself as a 1 character long not empty string
 path_length=${#folder}

 if [[ ( -n "$folder" ) ]] && [[ ( 1 < $path_length ) ]] ; then
 # The path is not empty and the length is greater than 1

 # Ignore the lines starting with #
 if [[ ! $folder == \#* ]] ; then
 # The line is not a comment, process it

 # Split the local and remote path
 eval local_path=$(echo "$folder" | cut -f1 -d+) # Need EVAL, otherwise the IF statement below does not work if the path contains ~
 remote_path=$(echo "$folder" | cut -f2 -d+)

 if [ ! -d "$local_path" ]
 then

 # The local directory doesn't exist.
 echo -e "${RED}$local_path does not exist, PULL to get $remote_path${NC}"

 else

 eval cd $local_path # Need EVAL, otherwise CD does not work if the path contains ~
 pwd | tr -d '\n'

 status=$(git status)

 # --------------------------------------------------------------------------
 # To handle

 # On branch master
 # Your branch is up-to-date with 'origin/master'.
 # Untracked files:
 # (use "git add <file>..." to include in what will be committed)

 if [[ $status == *"Untracked files"* ]]; then
 echo -e " ---- ${RED}New files added, please ADD${NC}" | tr -d '\n' # -e tells echo to enable backslash escapes
 fi

 # --------------------------------------------------------------------------
 # To handle

 # On branch master
 # Your branch is up-to-date with 'origin/master'.
 # Changes not staged for commit:
 # (use "git add <file>..." to update what will be committed)
 # (use "git checkout -- <file>..." to discard changes in working directory)

 if [[ $status == *"Changes not staged for commit"* ]]; then
 echo -e " ---- ${RED}Files changed, please ADD${NC}" | tr -d '\n' # -e tells echo to enable backslash escapes
 fi

 # --------------------------------------------------------------------------
 # To handle

 # On branch master
 # Your branch is up-to-date with 'origin/master'.
 # Changes to be committed:
 # (use "git reset HEAD <file>..." to unstage)

 if [[ $status == *"Changes to be committed"* ]]; then
 echo -e " ---- ${RED}Please COMMIT${NC}" | tr -d '\n' # -e tells echo to enable backslash escapes
 fi

 # --------------------------------------------------------------------------
 # To handle

 # On branch master
 # Your branch is ahead of 'origin/master' by 1 commit.
 # (use "git push" to publish your local commits)
 # nothing to commit, working tree clean

 if [[ $status == *"git push"* ]]; then
 echo -e " ---- ${RED}Please PUSH${NC}" | tr -d '\n' # -e tells echo to enable backslash escapes
 fi

 # --------------------------------------------------------------------------
 # To handle

 # On branch master
 # Your branch and 'origin/master' have diverged,
 # and have 1 and 1 different commits each, respectively.
 # (use "git pull" to merge the remote branch into yours)
 # nothing to commit, working tree clean

 if [[ $status == *"git pull"* ]]; then
 echo -e " ---- ${RED}Repos diverged, PULL to merge${NC}" | tr -d '\n' # -e tells echo to enable backslash escapes
 fi

 # --------------------------------------------------------------------------

 echo

 fi

 fi

 fi
done < "$input"
  • Create a file to pull from all Git repositories and save it as ~/Git/devops-scripts/git-pull.sh
#!/bin/bash

# git-pull.sh
# Pulls Git repositories and clones the missing ones
# The list is in git-repositories.txt

eval input="~/Git/devops-scripts/git-repositories.txt" # Need EVAL, otherwise the Bash loop cannot interpret ~ as the home directory if the path contains ~

RED='\033[0;31m'
NC='\033[0m' # No Color

while IFS= read -r folder
do

 # Get the length of the path
 # On Windows the empty line manifests itself as a 1 character long not empty string
 path_length=${#folder}

 if [[ ( -n "$folder" ) ]] && [[ ( 1 < $path_length ) ]] ; then
 # The path is not empty and the length is greater than 1

 # Ignore the lines starting with #
 if [[ ! $folder == \#* ]] ; then
 # The line is not a comment, process it

 # Split the local and remote path
 eval local_path=$(echo "$folder" | cut -f1 -d+) # Need EVAL, otherwise the IF statement below does not work if the path contains ~
 remote_path=$(echo "$folder" | cut -f2 -d+)
 rename_folder_to=$(echo "$folder" | cut -f3 -d+)

 # Check if the local path exists
 if [ ! -d "$local_path" ]; then
 # The local directory doesn't exist.
 echo
 echo -e "${RED}$local_path does not exist, cloning $remote_path${NC}"

 # Get the parent directory name
 parent_dir="${local_path%/*}"

 # Create the parent directory
 mkdir -p $parent_dir

 # Go to the parent_dir
 cd $parent_dir

 # Clone the Git repository
 git clone $remote_path

 if [[ ( -n "$rename_folder_to" ) ]] ; then

 # Rename the folder if necessary
 end_of_url="${remote_path##*/}"
 filename="${end_of_url%.*}"

 echo -e "${RED}Renaming $filename to $rename_folder_to${NC}"

 mv $filename $rename_folder_to

 fi


 else
 echo
 eval cd $local_path # Need EVAL, otherwise CD does not work if the path contains ~
 pwd
 git pull

 fi

 fi

 fi

done < "$input"

Enable the script execution


\/  \/  \/  \/  \/  \/  \/  \/  \/  \/

 In Mac OS

  • Open a terminal window in the ~/Git/devops-scripts folder
  • Run these commands to enable the execution of the Bash files
chmod u+x git-status.sh
chmod u+x git-pull.sh

/\  /\  /\  /\  /\  /\  /\  /\  /\  /\


Create an alias

Aliases allow you to execute commands from any folder in the command line and hide the complexity of the entire command.


\/  \/  \/  \/  \/  \/  \/  \/  \/  \/

In Mac OS

If you use the iTerm terminal window

iTerm reads the configuration settings from the~/.zshrc file. If you use iTerm add the alias definitions to the end of the ~/.zshrc file.

  • Open the~/.zshrc file and add the lines below to the end of it
If you use the built-in OSX Terminal

The built-in Terminal windows reads the configuration settings from the~/.bash_profile file.

  • Open the ~/.bash_profile file and add the lines below to it

In Windows

If you use Git Bash
  • Navigate to your home directory ( cd ~ )
  • Create the Bash config file if does not exist
    cp > ~/.bashrc

    Bash will show the “cp: missing file operand” an error message, but it will create the file that you can edit with any text editor

  • Add the lines below to the file

/\  /\  /\  /\  /\  /\  /\  /\  /\  /\


 

alias status='~/Git/devops-scripts/git-status.sh'
alias pull='~/Git/devops-scripts/git-pull.sh'

Refresh the settings in the Bash shell environment


\/  \/  \/  \/  \/  \/  \/  \/  \/  \/

In MacOS

source ~/.bash_profile

In Windows

Close the Bash window and open it again

/\  /\  /\  /\  /\  /\  /\  /\  /\  /\


How to use it

Open a terminal window in any folder

To check if you need to add, commit or push files in a Git repository:

status

If you have added new files to the repository you get:

/Users/YOUR_USER_NAME/Git/devops-scripts —- New files added, please ADD

If you have changed files in a Git repository you get:

/Users/YOUR_USER_NAME/Git/devops-scripts —- Files changed, please ADD

If you have already executed the git add . command, but have not yet committed the changes:

/Users/YOUR_USER_NAME/Git/devops-scripts—- Please COMMIT

If you have already committed the changes, but have not pushed the to the remote repository:

/Users/YOUR_USER_NAME/Git/devops-scripts —- Please PUSH

To pull from every Git repository before you start to work and clone the missing ones

pull

 

 

Add SSH key to a Jenkins Git step

To access a Git repository Jenkins can use an SSH key.

To add the SSH key to the Jenkins server use the following Chef script

Store the SSH key in an encrypted data bag called “keys”.

{
 "id": "ci_private_keys",
 "ci_github_key": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----",
}

 

Add the following to the Jenkins Chef recipe

  • Install Git
package 'git'
  • Install the Git and Credentials Jenkins plugins
jenkins_plugin 'git'
jenkins_plugin 'credentials'
  • Copy the SSH key to the Jenkins server
rsa_key = data_bag_item('keys', 'ci_private_keys')
file '/var/lib/jenkins/.ssh/id_rsa' do
  content "#{rsa_key['ci_github_key']}"
  owner 'jenkins'
  group 'jenkins'
  mode '0600'
end
  • Add github.com to the known hosts
bash 'provide github.com RSA fingerprint' do
  code <<-EOF
   ssh-keyscan github.com >> /var/lib/jenkins/.ssh/known_hosts
   chown jenkins.jenkins /var/lib/jenkins/.ssh/known_hosts
  EOF
  not_if{system('grep github.com /var/lib/jenkins/.ssh/known_hosts')}
end

 

To specify the SSH key in the Git step

  1. When the Jenkins server is operational, navigate to the Web interface
  2. Create a new Jenkins project
  3. In the Source Code Management section
    1. Select Git
    2. Enter the SSH URL of the repository
    3. When you are adding the first project, click the Add button to create the credential

      1. Click Jenkins to select the credentials provider
      2. Select SSH Username with private key as the Kind
      3. Enter the username you used when you created the SSH key for the Git repository
      4. Select From the Jenkins master ~/.ssh as the Private Key
      5. Click the Add button
    4. In the Credentials drop down select the credential you have created (the Git user name)

 

How to edit the GitHub README.md file

The GitHub repositories usually contain a README.md file to describe how to use the project. The GitHub web site has a simple editor, but it has a few limitations

  • If you accidentally refresh the page, you lose your changes,
  • The preview pane is wider than the page that will display your file, so the formatting can be different,
  • You have to switch between the editor and the the preview every time you want to see the result.

One of the simplest ways to edit the README.md is to use the Atom editor.

  • Open the README.md file with Atom,
  • In the Packages menu select Markdown Preview, or press Ctrl-Shitft-M to open the preview pane.

As you type in the editor window the preview pane will show the live review of the file.

For the Markdown syntax visit https://guides.github.com/pdfs/markdown-cheatsheet-online.pdf

Permission denied message when you try to upload your new repository to GitHub from a Windows computer

If you work on a Windows computer and create a new GitHub repository, you can copy the code from the GitHub page to set the remote address and push the existing code to the GitHub server. You may get the following error message:

$ git push -u origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Make sure you have selected the HTTPS button to see the correct lines for Windows.
github-01-create-repo

To change to address of the remote server execute the following
git remote set-url origin https://github.com/.....

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 on the command line.

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.