1.3 - More Git - Environment Variables RIT
1.3 - More Git - Environment Variables RIT
7
A Detached HEAD?!
● If you make a mistake when performing a A “detached HEAD” state can happen if you use git
checkout, you may end up in a “detached checkout with a hash but without specifying a
HEAD” state. But what does this mean? filename.
● First, HEAD is just a shortcut for the most
recent version of your repository. C:\Users\Hermione\SoftDevI> git checkout ade14e0
Note: switching to
○ git checkout HEAD will always revert a file to 'ade14e00980ec3235a9608cdf4974b5ef74e6399'.
the last version that you committed.
You are in 'detached HEAD' state. You can look around,
● Git includes support for branches. A branch make experimental changes and commit them, and you can
is a virtual copy of your repository where you discard any commits you make in this state without
impacting any branches by switching back to a branch.
can make independent changes without
C:\Users\Hermione\SoftDevI> git checkout master
affecting the files in the other branches. Previous HEAD position was ade14e0 updated spell.txt file
○ The default branch is called master. Switched to branch 'master'
○ We won’t be doing anything with branches this Your branch is up to date with 'origin/master'.
C:\Users\Hermione\SoftDevI> _
semester.
● A “detached HEAD” state means that your The fix is to check out the master branch. This will
local repository is disconnected from its reattach your local repository.
8
branch on the remote repository.
1.3.
6 Detach and Reattach Your
HEAD
Using git checkout without a specific filename will cause
your repository to be in a "detached HEAD" state. Thankfully,
it is easy to fix by checking out the master branch. Force it to
happen now, and then fix it.
● If necessary, launch a new command prompt and navigate to your
repository.
● Run a git log to see the version history.
○ Note that the top entry in the log is labeled HEAD -> master, meaning that
it is the most recent version of the master branch.
● Use git checkout with one of the previous versions of your
repository. Do not specify a file to check out.
○ You should see the “detached HEAD” message.
● Use git checkout master to reconnect your local repository with the
master branch.
9
● Sometimes you would like to discard local
changes that you made to a file.
○ You break some code that was working.
Git Restore ○ You accidentally delete something
important.
○ You just want to start over.
A git restore can be used to revert back to ○ etc.
the most recently committed version of a file, ● The git restore <FILENAME> command
and even restore files that have been deleted.
will replace the working copy of a file with
the last version that was committed to
C:\Users\Hermione\SoftDevI> rm primes.txt
C:\Users\Hermione\SoftDevI> cat primes.txt the local repository.
cat : Cannot find path 'primes.txt' because it does not ○ Instead of a specific filename you may also
exist.
C:\Users\Hermione\SoftDevI> git restore primes.txt use wildcards.
C:\Users\Hermione\SoftDevI> cat primes.txt ○ For example, you could use *.py to restore
2 3 5 7 11 13 17 19 23 29
C:\Users\Hermione\SoftDevI> _ all of your Python files.
● Using git restore is essentially identical
It should be used with caution, though! Once a to using git checkout HEAD
file is restored any changes are permanently <FILENAME>.
lost! Make sure that you really want to restore a ○ In both cases the HEAD version of the file
file before you do it! replaces the working copy.
○ Arguably, git restore is a little easier to
use. 10
1.3.
7
Git Restore
The git restore command can be used to revert a modified
(or even deleted!) file to the version that was most recently
committed to the local repository. This is a convenient way to
undo any changes you made since the last commit. Try it
now.
● If necessary, run a command prompt and navigate to your repository.
● Use Notepad to open your git_log.txt file and delete the contents.
● Use cat to verify that the file is now empty.
● Use git restore to restore the git_log.txt to the HEAD version.
○ e.g. git restore git_log.txt
● Use cat to verify that the file has been restored.
11
1.3.
7 Creating a Conflict
Clone another copy of your repository and then make
conflicting edits to the same text file in two different places.
● If necessary, start a command prompt and navigate to your repository.
● Use Notepad to create a file called lyrics.txt that contains the lyrics
from one of your favorite songs (feel free to use Google).
● Use the Git workflow to push the new file to your repository.
● Create a new directory under Unit01 named temp and change into it.
● Clone your repository into this directory.
○ e.g. git clone https://www.github.com/SoftDevI/assignment-03-hjg
● Edit the lyrics.txt file to add your name to the first line of the file,
and use the Git workflow to push it to your remote repository.
● Change back into the other copy of your repository. Do not pull.
● Edit the lyrics.txt file to add today’s date to the first line of the file,
and use the Git workflow to push it to your remote repository.
○ What happens?
○ What do you see when you run a git status check?
12
● Throughout this semester you may do some
work on a computer in the classroom, and then
clone your repository to continue working on a
Merge Conflicts
different computer, e.g. your laptop or a
desktop in your dorm. When you execute a git pull, Git will notify
○ In fact, you may move back and forth several
you if it can’t automatically merge the local copy
times.
of a file with the remote version.
● This means that you will have multiple copies
C:\Users\Hermione\SoftDevI> git pull
of the same GitHub project in more than one remote: Enumerating
objects: 6, done.
location. remote: Counting objects: 100% (6/6), done.
○ If you push a change from one, you will need to remote: Compressing objects: 100% (1/1), done.
pull the change to the other. remote: Total 4 (delta 3), reused 4 (delta 3), pack-
reused 0
○ In fact, if your local repository is not up to date Unpacking objects: 100% (4/4), 460 bytes | 51.00 KiB/s,
with the remote repository Git will require you to done.
From https://github.com/SoftDevI/assignment-03-hjg
synchronize with a git pull before you can
59a676c..fbb9adf master -> origin/master
push. Auto-merging lyrics.txt
● Having more than one copy of your repository CONFLICT (content): Merge conflict in lyrics.txt
Automatic merge failed; fix conflicts and then commit the
in different places means that you may end up result.
This will require you to solve the merge conflict
making different changes to the same file.
manually, and then commit and push the
● Most of the time Git transparently merges merged file to the repository.
changes together to create a version of the file
13 with all of the changes.
●
Resolving Merge Conflicts Manually
● After executing a git pull, Git will notify you
Local changes...
if there are any changes that cannot be
<<<<<<< HEAD
merged automatically. Saturday May 2nd, 1998
● A version of the file will have been created =======
Hermione Granger
with all of the changes separated by special >>>>>>> fbb9adfa5eb87b0705aa1d3027fc2604794094e2
Double, double, toil and trouble.
characters. Fire burn and cauldron bubble. ...remote
○ <<<<<<< HEAD indicates the beginning of the Double, double, toil and trouble. changes.
changes in the HEAD version in the local Something wicked this way comes!
Resolve a Merge
Saturday May 2nd, 1998
Hermione
Conflict
Resolve the merge conflict in your file and then push the
merged file to your repository.
Granger
● If necessary, launch a command prompt and navigate to your
repository.
○ Not the temporary repository that you created to cause the conflict.
● Synchronize your local repository with a git pull.
○ You should see that the lyrics.txt file could not be merged.
● Use Notepad to open the file.
● Edit the file so that both changes are included (your name and
today’s date).
○ Keep both sets of changes.
Hermione Granger
Saturday May 2nd, ○ Remove any unwanted text, e.g. special characters.
1998 ● Use the Git workflow to push the merged file to your repository.
15
Environment Variables
Environment variables are stored on a virtual drive
● Environment variables are important named env that is accessible from the command
values that affect how the programs and prompt.
operating system on a computer
behave. You may display the environment variables on a
● Every environment variable has a computer by listing the contents of the virtual
unique name and value. Some drive, e.g. ls env:
important variables include:
○ PATH - determines where your operating You may display an individual environment
system will search for executable variable using $env:<NAME> where <NAME> is
applications or scripts. the name of the variable, e.g. $env:PATH
○ PATHEXT - determines which file
extensions are considered executable. You may also redirect output to a file using the >
○ HOMEPATH - the path to the user operator after any command. For example, if you
directory. wanted to store the list of files in C:\ in a file
named “files.txt”: ls C:\ > files.txt
16
1.3.
8
Displaying
Environment Variables
Listing the environment variables on your computer can
PS C:\Users\ron> ls env:
Name
----
ALLUSERSPROFILE
APPDATA
Value
-----
C:\ProgramData
C:\Users\ron\AppData\Roaming
I a quick way to see the variables that have been set
be
CommonProgramFiles
CommonProgramFiles(x86)
CommonProgramW6432
COMPUTERNAME
C:\Program Files\Common Files
C:\Program Files (x86)\Common Files
C:\Program Files\Common Files
THEBURROW
and/or confirm that a variable has been set to the right
value. Try it now.
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
●
GIT_SSH C:\Program Files\PuTTY\plink.exe
HOMEDRIVE
HOMEPATH
LOCALAPPDATA
C:
\Users\ron
C:\Users\ron\AppData\Local
If necessary, launch a command prompt and navigate to your
Day02 directory.
LOGONSERVER \\THEBURROW
NAME Ron
NUMBER_OF_PROCESSORS 16
OneDrive C:\Users\ron\OneDrive
OneDriveConsumer
OS
Path
C:\Users\ron\OneDrive
Windows_NT
C:\Program Files (x86)\Razer\
● Recall that the gdr command can be used to list the drives on a
ChromaBroadcast\...
PATHEXT
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;....
PROCESSOR_ARCHITECTURE AMD64
computer. Use it and find the virtual drive named “env” in the list.
●
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 158 Stepping 13,
Genui...
PROCESSOR_LEVEL
PROCESSOR_REVISION
6
9e0d
List the contents of the drive to display all of the environment
variables on your computer.
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PSModulePath
PUBLIC
SESSIONNAME
C:\Users\ron\OneDrive\Documents\WindowsPowe...
C:\Users\Public
Console
● List the variables again, but this time redirect the output to a file
SystemDrive C:
SystemRoot
TEMP
TMP
C:\Windows
C:\Users\ron\AppData\Local\Temp
C:\Users\ron\AppData\Local\Temp
named “envars.txt”.
●
USERDOMAIN THEBURROW
USERDOMAIN_ROAMINGPROFILE
USERNAME
USERPROFILE
THEBURROW
ron
C:\Users\ron
Use the Git workflow to upload the new file to your remote
repository.
windir C:\Windows
PS C:\Users\ron> _ 17
○ status, add, commit, push.
● While it is possible to permanently set
environment variables from the command
Editing Environment
line, it is fairly convoluted. Variables
● It is far easier to use the environment
variables editor provided by Windows to
add or change environment variables.
● There are two different categories of
environment variables.
○ User variables are environment variables
specific to your user account.
○ System variables are system-wide and
apply to any account logged into the
computer.
● Depending on your permissions, you may
only be able to change the user variables.
● Launching the editor is not very intuitive:
○ Press the Windows key. Changes made to environment variables through
○ Type “environment” into the search field. the editor are “sticky” and system-wide.
○ Use the arrows keys to choose Edit
environment variables for your
They will not apply to any currently opened
account.
command prompts; you will need to restart
● From here you can create, edit, or
18
PowerShell to see the changes.
delete environment variables.
1.3.
9 Add a New Environment
Variable II
You may need to view, create, or modify the environment
variables on your computer to change the way programs
behave.
● Launch the Environment Variables editor.
● In the User variables section, click the New… button.
● Create a new environment variable named NAME with your first name
as the value.
Name
----
ALLUSERSPROFILE
Value
-----
C:\ProgramData
Use the command prompt to create a new environment
APPDATA C:\Users\ron\AppData\Roaming
variable.
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME
ComSpec
DriverData
THEBURROW
C:\Windows\system32\cmd.exe
C:\Windows\System32\Drivers\DriverData
Create a new environment variable and display it.
● If necessary, launch a command prompt.
GIT_SSH C:\Program Files\PuTTY\plink.exe
HOMEDRIVE C:
HOMEPATH \Users\ron
LOCALAPPDATA C:\Users\ron\AppData\Local
LOGONSERVER
NAME
NUMBER_OF_PROCESSORS
\\THEBURROW
Ron
16
● Create a new environment variable NAME and assign it a value of
OneDrive C:\Users\ron\OneDrive
OneDriveConsumer
OS
Path
C:\Users\ron\OneDrive
Windows_NT
C:\Program Files (x86)\Razer\
your first name.
● Display the new environment variable.
ChromaBroadcast\...
PATHEXT
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;....
PROCESSOR_ARCHITECTURE AMD64
● Close the command prompt and launch a new one. Display all of
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 158 Stepping 13,
Genui...
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 9e0d
ProgramData
ProgramFiles
ProgramFiles(x86)
C:\ProgramData
C:\Program Files
C:\Program Files (x86)
the environment variables. What do you notice?
ProgramW6432 C:\Program Files
PSModulePath C:\Users\ron\OneDrive\Documents\WindowsPowe...
PUBLIC C:\Users\Public
SESSIONNAME Console
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\ron\AppData\Local\Temp
TMP C:\Users\ron\AppData\Local\Temp
USERDOMAIN THEBURROW
USERDOMAIN_ROAMINGPROFILE THEBURROW
USERNAME ron
USERPROFILE C:\Users\ron
windir C:\Windows
PS C:\Users\ron> _
21
● Without doing anything special, running
programs requires you to type the path to the
The Path program.
○ From the current directory: .\hello.exe
The PATH is actually divided into two parts: the ○ Two directories up: ..\..\hello.exe
system path, which is global to all users on the ○ etc.
computer, and the user path, which is specific ● But what if you wanted to run a program from
to the currently logged in user. anywhere without having to type the path?
● The path is an environment variable that is
Depending on your level of access, you may not essentially a list of directories in your file
be able to change the system path (e.g. on lab system.
computers). ○ Usually, each directory contains at least one
executable program.
● Whenever you try to execute a command,
The full path is created by concatenating the Windows will search for an executable file with
two together - the system path always comes the same name in each of the directories in
first, and therefore will be searched first.
your path, in order.
○ It will attempt to execute the first matching file
that it finds.
This is just one reason why it is not a good idea
○ This means if you have two programs with the
to name your executables the same as existing
same name, it will execute the one that
system commands.
appears earlier in your path.
○ 22
This can happen if you have two different
1.3.1
0
23
Summary & Reflection