Vous êtes sur la page 1sur 16

GIT CONTROL

BASICS OF GIT FOR DEVELOPERS AND SYSTEM ADMINISTRATORS

Jennifer Watson
System Engineer
University of Maine System

NECWIC 2017
REASONS TO USE GIT

• Working on complex code project


• Working on code from more than one computer
• Collaborating with others on code or text file
• Maintain version control on code or file
• Be able to see when code or configuration files have changed and be able to note/read
why it was changed
• Want to compare or merge different versions
• Be able to roll back quickly to last known working version after changes break something
• Distributed copies of code or file provide back up in case of lose
INSTALLING GIT

• Installed by default on modern *nix Operating systems, but may still want to
grab latest version
• https://git-scm.com/downloads
• May also consider GUI client
• SourceTree (free, Mac and Win)
https://www.sourcetreeapp.com/
CONFIGURING GIT

From a command line:


• git config –global user.name “FirstName LastName”
• git config –global user.email “yourname@maine.edu”
• git config –global color.ui “auto”
• Mac/linux
• git config – global core.editor “nano –w”
• Windows
• git config –global core.editor “’c:/program files (x86)/Notepad++/notepad++.exe’ –multiInst –notabbar –nosession
noPlugin”

• git config --list


CREATE A GIT REPOSITORY

• From the command line, Make and/or navigate to the directory/folder for Git
to track
• Type the command:
• git init
GIT COMMANDS
• git status
• git add <file>
• git add .
• git commit –m “message”
• git log --graph
• git diff
• git diff –staged
• git diff HEAD~1 (can substitute 3 version back you want to compare to current version)
• git diff CommitIHash
GIT COMMANDS (CONTINUED)

• git checkout <branch>


• switch branches
• git checkout HEAD~1
• Reverts to previously committed version
• can be used to undelete a file
• git reset HEAD <file>
! Be careful with reset commands
• Un-stage file
• git reset --hard
• Everything reverted to last commit
aka Staging

Notice the difference between pull and fetch


WORKING WITH REMOTE REPOSITORIES

• git clone <url>


• git remote add <remote> <url for repository on gitLab or ssh link>
• git remote –v
• Lists existing remotes
• git fetch <remote> <branch> Recommend using fetch instead of pull
• git push <remote> <branch>
USING REMOTES WITH SSH

• cd ~/.ssh
• ssh-keygen
• Open the rsa.pub file created and copy the contents to your remote
repository
• Begins with ssh-rsa and ends with username@computername
TYPICAL DAY WITH GIT
Really Good Day Not so Good Day
• git fetch <remote> <feature branch> • git fetch <remote> <feature branch>
• Or git pull • Work on code
• Work on code • Get stuck 
• git status • git status
• git add <file(s) I worked on/edited> • git add <file(s) I worked on/edited>
• git commit –m “<message about what I fixed • git checkout –b <remote> <nameForNewBranch>
with maybe a tag for ticket closure or release>” • git commit –m “[WIP]<what I was trying to fix and
• git fetch <remote> <feature branch> my attempted approach>”
• merge/Resolve differences • Pull Request (aka Merge Request) to teammate
• git push <remote> <feature branch> • Receive feedback/help/resolution --> unstuck 

• Submit a Pull Request for approval • Continue on with Really Good Day workflow
WORKING WITH A TEAM IN GIT VS ALONE
TEAM ALONE
• Make sure editor/IDE configured for team • Commit often and feel free to push every
agreed upon line lengths
time as it creates a backup of your code
• Commit often, but generally push working
code that fixes an issue or completes feature • Only need to fetch and merge changes if
• Make sure to fetch prior to push to then you had pushed work from another
merge in any changes that have occurred on computer to the remote
the remote
• Pull requests allow you to ask someone to • This is when to try out and learn about
review your code for approval, but don’t rebasing (only on private branches)
forget it can also be helpful for asking for
help if you get stuck
• More prescribed branching
! Do NOT rebase on public branch
GIT BRANCHING

Source: https://leanpub.com/git-flow/read
GIT TIPS

• Create .gitignore file in at root of git respository


• List files to ignore
• Can use *.file-extension
• can still force add exceptions using git add – f file
• Great for excluding files your IDE may add to your Git tracked folder on your local computer

• Create hidden file in new folders you create with the following command:
• touch .gitkeep
• git cannot track an empty folder, so this is basically a placeholder file
GIT FOR SYSTEM ADMINISTRATORS

• git init on the folders with important configurations


• Know if someone else changed them by using git status
• And what they changed with git log and git diff
• Explain why you changed them with git commit –m “<reason>” so other admins will know
• Quickly bring back an earlier working version of the file with git checkout -- <file>
• Back up those key configurations easily by git push <remote> <branch>
Note: git keeps track of changes NOT multiple copies of the file, so it does not
take up a lot of space
LEARNING RESOURCES

• Code Academy https://www.codecademy.com/learn/learn-git


• https://www.tutorialspoint.com/git/
• More on How to Use in a Team https://www.sitepoint.com/getting-started-git-
team-environment/
• https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things
• https://www.atlassian.com/git/tutorials/merging-vs-rebasing