BookmarkSubscribeRSS Feed

DevOps Applied to SAS Viya 3.5: Top Git Commands with Examples

Started ‎04-13-2020 by
Modified ‎07-19-2021 by
Views 5,517

Today, Git is effectively the de-facto standard for software version control, and it is truly an expected tool in every developer’s toolbox. Whether you are working with GitHub or GitLab, sooner or later you will need to use git commands. Here are the Git commands we will cover: git clone, git checkout, git add, git status, git commit, git log, git push, git remote, git pull, git branch.

 

Pre-Requisites

 

GitHub Credentials and GitHub Project

You need a GitHub user and password.

Project

You need a git repository. To follow the examples two projects are provided:

  1. https://github.com/bteleuca/git-demo
  2. https://github.com/bteleuca/git-demo2

Variables

Two variables will be used, {your-user} and {your-user-branch} . Replace them with your GitHub user.

 

Fork an existing repository

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project. Go to https://github.com/bteleuca/git-demo.

 

600-git-demo-GitHub-project.png

From top right, press Fork. Sign-in if necessary.

The project becomes https://github.com/{your-user}/git-demo e.g. https://github.com/bteleuca/git-demo , for my user.

The screenshots in the post are taken while working with this project.

 

A SAS Viya Machine

You also need a SAS Viya Machine (SAS Viya 3.5 used in this post).

 

Connect with MobaXterm to the viya host (sasviya01 machine, in my example).

Create a folder under $HOME and grant rights to this folder.

 

cd $HOME
pwd
mkdir $HOME/devops
chmod -R 755 devops
cd devops
ls -l

You should see:

total 0

In my case, the environment variables correspond to:$USER = sbxbot and $HOME = /home/sbxbot

 

git clone

Usage: git clone [url]  

Clone an existing repository from a URL on the local machine. One of the most used commands.

Inside /devops folder:

git clone https://github.com/{your-user}/git-demo.git

e.g. git clone https://github.com/bteleuca/git-demo.git 

Cloning into 'git-demo'...
remote: Enumerating objects: 32, done.
remote: Counting objects: 100% (32/32), done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 32 (delta 5), reused 29 (delta 4), pack-reused 0
Unpacking objects: 100% (32/32), done.

 

Confirm cloning

cd $HOME/devops/git-demo/Data-Management/scripts/
ls -l

You should see a CAS script from the cloned remote repository.

total 4
-rw-r--r--. 1 sbxbot sasusers 819 Apr 13 19:29 080_load_CAS.sas

 

Create a CAS script

Option: SAS Studio V

In your environment, open SAS Studio V, log with your user and password.

Navigate to: intviya01/Home/devops/git-demo/Data-Management/scripts/

400-sas-studio.png

Create a new SAS program. Copy the SAS code below.

cas casauto;
caslib _all_ assign;
proc casutil incaslib="Public";
list files; list tables;
quit;
cas casauto terminate;

Paste it.

Save the program under intviya01/Home/devops/git-demo/Data-Management/scripts/ as 010_list_files.sas .

Back to MobaXterm and the command line.

 

Option: MobaXterm

Browse to:

cd $HOME/devops/git-demo/Data-Management/scripts/

200-new-sas-script.png

Create a new file (right click) named 010_list_files.sas .

Copy the SAS code above.

Paste it.

300-new-sas-script-content.png

 

Save.

 

git checkout

Create a new branch, named {your-user-branch}, for new work to go into:

610-GitHub-git-checkout-branches.PNG

 

Change {your-user-branch} in the statement below:

cd $HOME/devops/git-demo/
git checkout -b {your-user-branch}
[sbxbot@intviya01 scripts]$ git checkout -b sbxbot
Switched to a new branch 'sbxbot'

 

git add

Usage: git add [file]  

This command adds a file to the staging area.

git add .

. adds all changes to the staging area. Or,

git add -p 010_list_files.sas

. adds only the file to the staging area.

 

git status

Usage: git status  
This command lists all the files that have to be committed.

git status
# On branch sbxbot
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       new file:   010_list_files.sas

 

Change after a commit

Edit the 010_list_files.sas with SAS Studio or MobaXterm (whatever you used earlier). Add an Enter after casauto;

caslib _all_ assign;
proc casutil incaslib="Public";
list files; list tables;
quit;
cas casauto terminate;

 

git diff

Usage: git diff

This command shows the file differences which are not yet staged. Very useful to understand the changes.

git diff
diff --git a/Data-Management/scripts/010_list_files.sas b/Data-Management/scripts/010_list_files.sas
index c0dc022..6752fad 100644
--- a/Data-Management/scripts/010_list_files.sas
+++ b/Data-Management/scripts/010_list_files.sas
@@ -1,4 +1,5 @@
 cas casauto;
+
 caslib _all_ assign;
 proc casutil incaslib=”Public”;
 list files; list tables;

 

git commit

Usage: git commit -m “[ Type in the commit message]”  

This command records or snapshots the file permanently in the version history.

git commit -m "Added new .sas program"
You committed without the change. To incorporate the change, do git add . or git add – p  for a specific change.
[sbxbot 76d01e3] Added new .sas program
 1 file changed, 6 insertions(+)
 create mode 100644 Data-Management/scripts/010_list_files.sas

 

git log

Usage: git log
This command lists the version history for the current branch.

git log
commit 76d01e3b0578ddfc4ca60bd8fd555b7e7c703603
Author: “sbxbot” <Bogdan.teleuca@sas.com>
Date:   Fri Apr 10 08:44:49 2020 -0400
    Added new .sas program
...

Scroll with Enter until (END) then CTRL + Z.

 

git push

Usage: git push [variable name] master

This command sends the committed changes of the master branch to your remote repository.

{your-user-branch} is your branch you created in the git checkout command (GitHub user):

 

git push origin {your-user-branch}

For the sake of simplicity, we will use https as a method to connect (user and password). You could use ssh too, several other steps are required. For more info on how to set-up ssh for git, see Xavier’s video.

You might see:

Username for 'https://github.com': bteleuca
Password for 'https://bteleuca@github.com':
Counting objects: 8, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 606 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)

The remote branch {your-user} has been pushed remotely:

620-GitHub-git-push-remote-changes.PNG

Navigate to https://github.com/{your-user}/git-demo/tree/{your-user-branch}/Data-Management/scripts

630-GitHub-git-push-remote-changes-scripts.PNG

 

Fork a second repository

Let’s assume you want to push the changes in another repository: https://github.com/bteleuca/git-demo2.git

640-git-demo2-GitHub-project-fork.png

From top right, press Fork. Sign-in if necessary.

The project becomes https://github.com/ {your-user}/git-demo e.g. https://github.com/bteleuca/git-demo2.git in my case.

 

git remote

Usage: git remote add [variable name] [Remote Server Link]  

This command is used to connect your local repository to the remote server.

Change the remote

cd $HOME/devops/git-demo/
git remote rename origin old-origin
git remote add origin https://github.com/bteleuca/git-demo2.git
git remote -v
old-origin      https://github.com/bteleuca/git-demo.git (fetch)
old-origin      https://github.com/bteleuca/git-demo.git (push)
origin  https://github.com/bteleuca/git-demo2.git (fetch)
origin  https://github.com/bteleuca/git-demo2.git (push)

The new origin becomes the git-demo2 repository.

 

Push the changes to the new remote

git push origin {your-user-branch}
git push origin sbxbot
sbxbot

At this point you will see in the target repository https://github.com/{your-user}/git-demo 

650-GitHub-git-demo2-push-remote-changes.png

 

git pull

Usage: git pull [Repository Link]  

This command fetches and merges changes from the remote server to your working directory.

Suppose another developer updated a program in the /scripts folder, and pushed it in the remote repository https://github.com/{your-user}/git-demo  . You need to update your local copy before you develop further.

 

git pull old-origin master
From https://github.com/bteleuca/git-demo
 * branch            master     -> FETCH_HEAD
Already up-to-date

 

No changes remotely, therefore you can proceed safely.

 

git branch

Lists all branches in the repository.

git branch -av
master                    bd0b252 Delete 100_create_star_schema.sas
* sbxbot                    b85200a Merge branch 'sbxbot' of https://github.com/bteleuca/git-demo into sbxbot
  remotes/old-origin/HEAD   -> old-origin/master
  remotes/old-origin/master bd0b252 Delete 100_create_star_schema.sas
  remotes/old-origin/sbxbot b85200a Merge branch 'sbxbot' of https://github.com/bteleuca/git-demo into sbxbot
  remotes/origin/sbxbot     b85200a Merge branch 'sbxbot' of https://github.com/bteleuca/git-demo into sbxbot

 

Resources

Setting up Git repository in SAS Studio 5.2 (excellent video by Xavier, SSH setup clearly explained).

Conclusions

We looked at the most common git commands you might need to write, if you are in a DevOps scenario involving SAS Viya. Next, you might want to read the DevOps Applied to SAS Viya 3.5 series:

Acknowledgements

Mark Thomas, Rob Collum, Stephen Foerster for their contributions.

Want to Learn More about Viya 3.5?

Thank you for your time reading this post. Please comment and share your experience with the git commands and help others.

Version history
Last update:
‎07-19-2021 02:24 AM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started