0% found this document useful (0 votes)
5 views37 pages

Git 2

The document provides a comprehensive overview of working with Git locally, covering key operations such as creating repositories, staging changes, committing, and managing files. It also explains essential commands for configuration, tracking changes, and inspecting project history. Additionally, it highlights the importance of ignoring certain files and offers guidance on undoing changes and managing commit history.

Uploaded by

shubh.jpg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Git 2

The document provides a comprehensive overview of working with Git locally, covering key operations such as creating repositories, staging changes, committing, and managing files. It also explains essential commands for configuration, tracking changes, and inspecting project history. Additionally, it highlights the importance of ignoring certain files and offers guidance on undoing changes and managing commit history.

Uploaded by

shubh.jpg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Git:

Working with Git Local


Persistent University
Key Learning Points

o Creating local repository, adding files, and committing changes


o Viewing log and diffs
o Staging changes as multiple changes
o Deleting and renaming files
o Ignoring Files
o Undoing/redoing changes to the local copy and repository
o Cleaning the working copy
Confidential © 2020 Persistent Systems
Git Local Operations
Configuring Git: Simple Command Line

• Open Git Bash Terminal $ pwd

• Where am I?? /c/Users/asif_immanad


- pwd – print working directory

• How do I move around??


- cd – change directory
- cd c:/csm_classes – change to a specific path
- cd .. – change to parent directory

• What files are there?


- ls – list
- ls - l – list with details

Confidential © 2020 Persistent Systems 4


Tell Git who you are

• Update your configuration, one time only


- git config – Global [Link] “Asif Immanad”
- git config – Global [Link]
asif_immanad@[Link]
- git config – Global [Link] notepad++
- git config – List (or git config –l)

Confidential © 2020 Persistent Systems 5


Workflow of Git

A Basic Workflow
• Edit files Working Working Staging .git Directory
• Stage the changes Directory Directory Area (Repository)

• Review your changes


• Commit the changes Git add Checkout the project

Index

Stage fixes

Git commit

Repository Commit

Confidential © 2020 Persistent Systems 6


Basic Steps

• Configure Git

• Initialize Git

• Add files – Adds to Staging area

• Commit – Log everything to repository

• Check Status – View what you are up to

• View diff – View the difference between two states

• View log – View all commit log

Confidential © 2020 Persistent Systems 7


Getting Started

• Create a Java Project (mine is GitIntro).

• cd into project directory.

• Use git init command to initialize the repository.

• Create a class (mine is HelloGit).

• This is one-time process.

Confidential © 2020 Persistent Systems 8


Ignoring files

• It’s important to tell Git what files you do not want to track

• Temp files, executable files, etc. do not need version control (and can cause major issues when merging!)

• [Link]

• Example (place in root of repo):


- *.class
- .project
- .classpath
- .settings/

Confidential © 2020 Persistent Systems 9


More .gitignore files

Example of .gitignore files :-

• # Directories # • # Windows image file • # Eclipse


caches
• /build/ • .project
• /bin/ • [Link]
• .metadata
• # OS Files # • # Folder config file • bin/**
• .DS_Store • tmp/**
• [Link]
• *.class • *.tmp
• # Package Files # • # OSX
• [Link]
• *.jar • .classpath
• .DS_Store
• *.war • .settings/
• .svn
• *.ear • .loadpath

Confidential © 2020 Persistent Systems 10


What’s the Status?

• Once we have initiated the repository and made some changes on to the file, we may check the status.

• To check the status use Git status.

Nothing tracked yet.

Get used to reading helpful messages

Confidential © 2020 Persistent Systems 11


Let’s Track a File

• Once we have done with making the changes in file, we may add them to repository to start tracking the file.

• To start tracking a file use Git add.

Git add – Tells Git to track a file

Confidential © 2020 Persistent Systems 12


Let’s Track a File

• The Git add command adds a change in the working directory to the staging area.

• However, Git add doesn't really affect the repository in any significant way—changes are not actually recorded
until you run Git commit.

• In conjunction with these commands, you'll also need Git status to view the state of the working directory and
the staging area.

• Git add <file>


- Stage all changes in <file> for the next commit.

• Git add <directory>


- Stage all changes in <directory> for the next commit.

Confidential © 2020 Persistent Systems 13


Saving Changes, Let’s Commit…

• When we feel the changes are ready to commit.

• The Git commit command commits the staged snapshot to the project history.

• Committed snapshots can be thought of as “safe” versions of a project — Git will never change them unless
you explicitly ask it to.

• Along with Git add, this is one of the most important Git commands.

• To commit the changes we need to provide some message along.


- Git commit -m “<message>”

• To add and commit as well we can use.


- Git commit –am “<message>”

Confidential © 2020 Persistent Systems 14


Commit the file…

When you commit, you must provide a comment


(if you forget, Git will open a text editor so you can write one).

Confidential © 2020 Persistent Systems 15


Let's Inspect…

• The Git status command displays the state of the working directory and the staging area.

• It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.

• To inspect use:
- Git status

• List which files are staged, unstaged, and untracked.

Confidential © 2020 Persistent Systems 16


What if you change the file?

Notice more helpful hints Git provides. You could add


to the staging area OR add & commit in one step.

Be careful if you add to the staging area and then make more changes – The file can appear as both staged and
unstaged. For now, we can use –am.
Confidential © 2020 Persistent Systems 17
You made some changes!!! But What?

• We have made some changes but not sure what we have made!!!

• Let's check the changes and compare with what we have in repository.

• To differentiate working version and committed version we can use:


- Git diff
- Show changes between commits, commit and working tree, etc.

Confidential © 2020 Persistent Systems 18


You made some changes!!! But What?

This command compares your working directory with your staging area.
These are the changes that are not yet staged.

Confidential © 2020 Persistent Systems 19


What if you’ve committed all your changes?

If all the changes are committed, using diff command doesn’t show anything.

diff doesn’t have anything to display.

Confidential © 2020 Persistent Systems 20


What if you remove a file?

If we remove a file from folder Git replies saying that file has deleted.

File added not committed

Now I remove [Link]

Confidential © 2020 Persistent Systems 21


What if you remove a file?

• To remove file from being tracked use:


- git rm <filename>

This removes the file from being tracked. If you’ve already committed, the file is still in the database.

Confidential © 2020 Persistent Systems 22


Worked whole day…but what all have I done?

• The Git log command displays committed snapshots.

• It lets you list the project history, filter it, and search for specific changes.

• While Git status lets you inspect the working directory and the staging area, Git log only operates on the
committed history.

• So to go back and see what all we have done use:


- Git log

• Display’s the entire commit history using the default formatting.

Confidential © 2020 Persistent Systems 23


So what all have I done?

There are many useful options for Git log.

Confidential © 2020 Persistent Systems 24


Difficulty in Finding Log?

• git log -n <limit>


- git log -n 3 will display only 3 commits.

• git log –oneline


- Condense each commit to a single line.

• git log --author=“<pattern>”


- Search for commits by a particular author. The <pattern> argument can be a plain string or a regular expression.

• git log --grep=“<pattern>”


- Search for commits with a commit message that matches <pattern>, which can be a plain string or a regular expression.

• git log <since>..<until>


- Show only commits that occur between <since> and <until>.

• git log <file>


- Only display commits that include the specified file. This is an easy way to see the history of a particular file.

Confidential © 2020 Persistent Systems 25


Git Log Options

Can specify a format, such as: Can filter, such as: Can redirect output to file,
such as:
• git log –oneline • git log –since=[Link]
• git log >> [Link]
• git log –oneline -3 • includes filters like –since,
<branchname> –after, –until, –before, –
author, etc.
• git log –pretty=oneline
• git log –grep= “file” → checks
for matching commit message

Confidential © 2020 Persistent Systems 26


Undoing Changes

• The git checkout command serves three distinct functions:


- Checking out files,
- Checking out commits, and
- Checking out branches.

• Checking out a commit makes the entire working directory match that commit.

• View an old state without altering your current state in any way.

• Let’s you see an old version of that particular file, leaving the rest of your working directory untouched.

• Use: git checkout master.

• Use: git checkout a1e8fb5 – (a1e8fb5 is SHA key for commit).

• This makes your working directory match the exact state of the a1e8fb5 commit.

Confidential © 2020 Persistent Systems 27


Reverting

• The Git revert command undoes a committed snapshot.

• When you want to remove an entire commit from your project history.

• This can be useful, for example, if you’re tracking down a bug and find that it was introduced by a single
commit.

• Instead of manually going in, fixing it, and committing a new snapshot, you can use Git revert to automatically
do all of this for you.

• Use: git revert <commit>

Confidential © 2020 Persistent Systems 28


Resetting

• If Git revert is a “safe” way to undo changes, Git reset is the dangerous method.

• There is no way to retrieve the original copy.

• It is a permanent undo.

• It’s one of the only Git commands that has the potential to lose your work.

• Git reset:
- Reset the staging area to match the most recent commit but leave the working directory unchanged.

• Git reset <file>:


- Remove the specified file from the staging area but leave the working directory unchanged.

Confidential © 2020 Persistent Systems 29


Reverting vs. Resetting

• Git revert undoes a single commit—it does not “revert” back to the previous state of a project by removing all
subsequent commits.

• Reverting doesn’t change the project history.

• Git revert is able to target an individual commit at an arbitrary point in the history, whereas Git reset can only
work backwards from the current commit.

Confidential © 2020 Persistent Systems 30


Un-stage and Rename File

• We can simply use a ‘git mv’ to rename a file where mv stands for move in Linux for renaming.
eg.: git mv [Link] [Link].

• The way we stage a file to track, the same way we can upstage the file to untrack.
eg.: git reset HEAD [Link].

Confidential © 2020 Persistent Systems 31


FAQs

• What is Version Controlling?

• What is SVN and its Pro’s and Con’s?

• History of Git?

• What is Git?

• Benefits of Git over other Version Controlling Tools?

• How Git works – Three States?

Confidential © 2020 Persistent Systems 32


Summary

• With this we have come to an end of our first session, where we discussed about.
- What is Git?
- Benefits and working of Git.

• At the end of this session, we see that you are now able to answer following questions:
- What is Version Controlling?
- How Git is Distributed Version Control?
- What are the states in Git?

• In the next session we will discuss about.


- Working with Git and Local Operations on Git.

Confidential © 2020 Persistent Systems 33


Reference Material: Websites & Blogs

• [Link]

• [Link]

• [Link]

• [Link]

• [Link]

Confidential © 2020 Persistent Systems 34


Reference Material: Books

Pro Git Version Control with Git


• By Scott Chacon and Ben Straub • By Jon Loeliger, Matthew McCullough
• Publisher: Apress • Publisher: O'Reilly Media

Confidential © 2020 Persistent Systems 35


Key Contacts

Git Interactive Vice President


Vaishali Khatal Shubhangi Kelkar
shubhangi_kelkar@[Link]
vaishali_khatal@[Link]
Asif Immanad
asif_immanad@[Link]

Confidential © 2020 Persistent Systems 36


Thank you!
Persistent University

You might also like