DevOps - Unit - 2.pptx
DevOps - Unit - 2.pptx
• Git is foundation of many services like Github and Gitlab but we can
use Git without using any other Git services.
• Scalable
Git is scalable, which means when the number of users increases, the Git
can easily handle such situations.
• Distributed
One of Git's great features is that it is distributed.
• Also, instead of just having one central repository that you send changes
to, every user has their own repository that contains the entire commit
history of the project.
• Instead of trunk, the default development branch is called main and all
changes are committed into this branch.
• We’ll see how two developers, John and Mary, can work on separate
features and share their contributions via a centralized repository.
• In his local repository, John can develop features using the standard Git
commit process: edit, stage, and commit.
• Remember that since these commands create local commits, John can
repeat this process as many times as he wants without worrying about
what’s going on in the central repository.
• Meanwhile, Mary is working on her own feature in her
own local repository using the same edit/stage/commit
process.
• Like John, she doesn’t care what’s going on in the central
repository, and she really doesn’t care what John is doing
in his local repository, since all local repositories
are private.
• Once John finishes his feature, he should publish his local
commits to the central repository so other team members
can access it.
• He can do this with the git push command.
• But, since her local history has diverged from the central
repository, Git will refuse the request with a rather
verbose error message:
error: failed to push some refs to '/path/to/repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
• This prevents Mary from overwriting official commits. She needs to
pull John’s updates into her repository, integrate them with her local
changes, and then try again.
• Mary can use git pull to incorporate upstream changes into her
repository.
• It pulls the entire upstream commit history into Mary’s local
repository and tries to integrate it with her local commits.
• The --rebase option tells Git to move all of Mary’s commits to the
tip of the main branch after synchronising it with the changes from
the central repository.
• The pull would still work if you forgot this option
• Rebasing works by transferring each local commit to the
updated main branch one at a time.
• This means that you catch merge conflicts on a
commit-by-commit basis rather than resolving all of them
in one massive merge commit.
• This keeps your commits as focused as possible and
makes for a clean project history.
• This makes it much easier to figure out where bugs were
introduced and, if necessary, to roll back changes with
minimal impact on the project.
Working with remote repository : Git remote
• To be able to collaborate on any Git project, you need to know how
to manage your remote repositories.
• Remote repositories are versions of your project that are hosted on
the Internet or network somewhere.
• You can have several of them, each of which generally is either
read-only or read/write for you.
• Collaborating with others involves managing these remote
repositories and pushing and pulling data to and from them when
you need to share work.
• Managing remote repositories includes knowing how to add remote
repositories, remove remotes that are no longer valid, manage
various remote branches and define them as being tracked or not,
and more.
Showing Your Remotes
• To see which remote servers you have configured, you
can run the git remote command.
• It lists the short names of each remote handle you’ve
specified.
• If you’ve cloned your repository, you should at least
see origin — that is the default name Git gives to the
server you cloned from.
• You can also specify -v, which shows you the URLs that Git has
stored for the short name to be used when reading and writing to
that remote.
• If you have more than one remote, the command lists them all. For
example, a repository with multiple remotes for working with
several collaborators might look something like this.
Adding Remote Repositories
• To add a new remote Git repository as a shortname you can
reference easily, run git remote add <shortname> <url>
• Now you can use the string pb on the command line instead of the
whole URL.
• For example, if you want to fetch all the information that Paul has
but that you don’t yet have in your repository, you can run git fetch
pb:
• The command goes out to that remote project and pulls down all the
data from that remote project that you don’t have yet.
• After you do this, you should have references to all the branches
from that remote, which you can merge in or inspect at any time.
• If you clone a repository, the command automatically adds that
remote repository under the name “origin”.
• So, git fetch origin fetches any new work that has been pushed to
that server since you cloned (or last fetched from) it.
• It’s important to note that the git fetch command only downloads
the data to your local repository — it doesn’t automatically merge it
with any of your work or modify what you’re currently working on.
• You have to merge it manually into your work when you’re ready.
Pushing to Your Remotes
• When you have your project at a point that you want to share, you
have to push it upstream.
• The command for this is simple: git push <remote> <branch>.
• If you want to push your master branch to your origin server (again,
cloning generally sets up both of those names for you
automatically), then you can run this to push any commits you’ve
done back up to the server.
• This command works only if you cloned from a server to which you
have write access and if nobody has pushed in the meantime.
• If you and someone else clone at the same time and they push
upstream and then you push upstream, your push will rightly be
rejected.
• You’ll have to fetch their work first and incorporate it into yours
before you’ll be allowed to push.
Inspecting a Remote
• If you want to see more information about a particular remote, you
can use the git remote show <remote> command.
• If you run this command with a particular short name, such
as origin, you get something like this.
• It lists the URL for the remote repository as well as the tracking
branch information.
Renaming and Removing Remotes
• You can run git remote rename to change a remote’s short name.
• For instance, if you want to rename pb to paul, you can do so with git
remote rename.
• It’s worth mentioning that this changes all your remote-tracking branch
names, too.
• What used to be referenced at pb/master is now at paul/master
• If you want to remove a remote for some reason
• Once you delete the reference to a remote this way, all remote-tracking
branches and configuration settings associated with that remote are also
deleted.
Git Configuration
Setting Up a Git Repository
Local File Changes
Declare Commits
Branching
GIT branching
• Branching is a feature available in most modern version control
systems.
• In Git, branches are a part of your everyday development process.
• Git branches are effectively a pointer to a snapshot of your changes.
• When you want to add a new feature or fix a bug—no matter how
big or how small—you spawn a new branch to encapsulate your
changes.
• This makes it harder for unstable code to get merged into the main
code base, and it gives you the chance to clean up your future's
history before merging it into the main branch.
• The before diagram visualizes a repository with two
isolated lines of development, one for a little feature, and
one for a longer-running feature.
• Create a new branch called <branch>. This does not check out the
new branch.
After clicking on “Invite a collaborator” fill the required details and then
you are done.