Git - gittutorial Documentation

gittutorial - A tutorial introduction to GitDESCRIPTION

This tutorial explains how to import a new project into Git, makechanges to it, and share changes with other developers.

If you are instead primarily interested in using Git to fetch a project,for example, to test the latest version, you may prefer to start with the first two chapters of The Git User’s Manual.

First, note that you can get documentation for a command such as it log --graph with:

With the latter, you can use the manual viewer of your choice; seegit-help[1] for more information.

It is a good idea to introduce yourself to Git with your name and public email address before doing any operation. The easiest way to do so is:$ git config --global user. name "Your Name Comes Here"$ git config --global user.email you@yourdomain.example.comImporting a new project

Assume you have a tarball project.tar.gz with your initial work .You can places it under Git revision control as follows.$ tar xzf project.tar.gz$ cd project$ git init

Git will replyInitialized empty Git repository in .git/

You’ve now initialized the working directory—​you may notice a new directory created, named ".git".

Next, tell Git to take a snapshot of the contents of all files underthe currentt directory (note the .), with git add:

This snapshot is now stored in a temporary staging area which Git calls the "index".You can permanently store the contents of the index in the repository witha  git commit:

This will prompt you for a commit message. You’ve now stored the first version of your project in Git .Making changes

Modify some files, then add their updated contents to the index:$ git add file1 file2 file3

You are now ready to commit .You can see what is about to becommitted usingg git diff with the --cached option:

(Without --cached, git diff will show you any changes that you've made but not yet added to the index.)You can also get a brief summary of the situation with git status:$ git status branch mamaster changeso be committed: Your branch is up to date with 'origin/master'.(use "git restore --staged ..." to unstage)modified:file1modified:file2modified:file3

If you need to make any further adjustments, do so now, and then add any newly modified content to the index. Finally, commit your changes with:

This will again prompt you for a message describing the change, and then record a new version of the project.

Alternatively, instead of running git add beforehand, you can use

which will automatically notice any modified (but not new) files, add them to the index, and commit, all in one step.

A note on commit messages: Though not required, it’s a good ideato beginn the commit message with a single short (less than 50 characters)line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git. For example, git-format-patch[1] turns a commit into an email, and it uses the title on the Subject line and the rest of the commit in the body. Git tracks content not files

Many revision control systems provide anadditionald command that tells the system to start tracking changes to a new file. Git’s add command does something simpler and more powerful: git add is used both for new and newly modified files, and in both cases, it takes a snapshot of the given files and stages that content in the index, ready for inclusion in the next commit.Viewing project history

At any point, you can view the history of your changes using

If you also want to see complete diffs at each step, use

Often the overview of the change is useful to get a feel of each step$ git log --stat --summaryManaging branches

A single Git repository can maintain multiple branches of development.To create a new branch named "experimental", use$ git branch experimental

you’ll get a list of all existing branches:

The "experimental" branch is the one you just created, and the"master" branch is a default branch that was created for you automatically.The asterisk marks the branch you are currently on;type$ git switch experimental

to switch to the experimental branch. Now edit a file, commit the change, and switch back to the master branch:(edit file)$ git commit -a$ git switch master

Check that the change you made is no longer visible since it was made on the experimental branch and you’re back on the master branch.

You can make a different change on the master branch:(edit file)$ git commit -a

at this point, the two branches have diverged, with different changes made in each. To merge the changes made in experimental into master, run

If the changes don’t conflict, you’re done. If there are conflicts, markers will be left in the problematic files showing the conflict;

will show this. Once you’ve edited the files to resolve the conflicts,

will commit the result of the merge. Finally,

will show a nice graphical representation of the resulting history.

At this point, you could delete the experimental branch with$ git branch -d experimental

This command ensures that the changes in the experimental branch are already in the current branch.

If you develop on a branch crazy-idea, then regret it, you can always delete the branch with$ git branch -D crazy-idea

Branches are cheap and easy, so this is a good way to try something out. Using Git for collaboration

Suppose that Alice has started a new project with a Git repository in/home/Alice/project and that Bob, who has a home directory on the same machine, wants to contribute.

Bob begins with:bob$ git clone /homeAlicee/project myrepo

This creates a new directory "myrepo" containing a clone of Alice’srepository. The clone is on an equal footing with theoriginal projectt, possessing its own copy of the original project’s history.

Bob then makes some changes and commits them:(edit files)bob$ git commit -a(repeat as necessary)

When he’s ready, he tells Alice to pull changes from the repository at /home/bob/myrepo.She does this with:alice$ cd /home/Alice/projectalice$ git pull /home/bob/myrepo master

This merges the changes from Bob’s "master" branch into Alice’scurrent branch. If Alice has made her own changes in the meantime, then she may need to manually fix any conflicts.

The "pull" command thus performs two operations: it fetches changesfrom a remote branch, then merges them into the current branch.

Note that in general, Alice would want her local changes committed beforeinitiating this "pull".If Bob’s work conflicts with what Alice did sincetheir histories forked, Alice will use her working tree and the index toresolve conflicts, and existing local changes will interfere with theconflict resolution process (Git will still perform the fetch but willrefuse to merge — Alice will have to get rid of her local changes insome way and pull again when this happens).

Alice can peek at what Bob did without merging first, using the "fetch"command; this allows Alice to inspect what Bob did, using a specialsymbol "FETCH_HEAD", in order to determine if he has anything worthpulling, like this:alice$ git fetch /home/bob/myrepo masteralice$ git log -p HEAD..FETCH_HEAD

This operation is safe even if Alice has uncommitted local changes.The range notation "HEAD..FETCH_HEAD" means "show everything that is reachablefrom the FETCH_HEAD but exclude anything that is reachable from HEAD".Alice already knows everything that leads to her current state (HEAD),and reviews what Bob has in his state (FETCH_HEAD) that she has notseen with this command.

If Alice wants to visualize what Bob did since their histories forkedshe can issue the following command:

This uses the same two-dot range notation we saw earlier with git log.

Alice may want to view what both of them did since they forked.She can use three-dot form instead of the two-dot form:

This means "show everything that is reachable from either one, butexclude anything that is reachable from both of them".

Please note that these range notation can be used with both gitkand "git log".

After inspecting what Bob did, if there is nothing urgent, Alice maydecide to continue working without pulling from Bob.If Bob’s historydoes have something Alice would immediately need, Alice may choose tostash her work-in-progress first, do a "pull", and then finally unstashher work-in-progress on top of the resulting history.

When you are working in a small closely knit group, it is notunusual to interact with the same repository over and overagain.By defining remote repository shorthand, you can makeit easier:alice$ git remote add bob /home/bob/myrepo

With this, Alice can perform the first part of the "pull" operationalone using the git fetch command without merging them with her ownbranch, using:

Post a Comment (0)
Previous Post Next Post