Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
127 views
Learn Git in 30 Minutes - Tutorialzine
Learn git in 30 minutes tutorial for fast learning git by tutorialzine
Uploaded by
Umashankar Sahu
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Learn Git in 30 Minutes _ Tutorialzine For Later
Download
Save
Save Learn Git in 30 Minutes _ Tutorialzine For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
127 views
Learn Git in 30 Minutes - Tutorialzine
Learn git in 30 minutes tutorial for fast learning git by tutorialzine
Uploaded by
Umashankar Sahu
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Learn Git in 30 Minutes _ Tutorialzine For Later
Carousel Previous
Carousel Next
Save
Save Learn Git in 30 Minutes _ Tutorialzine For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 11
Search
Fullscreen
asz016 Learn Gitin 30 Minutes |Tuoriatzine tutorialzine articles newsletter —_ the book Learn Git In 30 Minutes in 30 Minutes Learn Gi Danny Markov June 2nd, 2016 Like Share Ga (20 Git has exploded in popularity in recent years. The verison control system is used by huge open source projects like Linux with thousands of contributors, teams of various sizes, solo developers and even students, Beginners are often terrified by all the cryptic commands and arguments that git requires. But you don't need to know all of that to get started. You can begin by mastering a handful of the mast often used ones, and then slowly build from there. And this is exactly what we will teach you today. Let's begin! hiipunocalane.com2018)06.earr-g-n-30- mines! mtasz016 git The basics Gitis a collection of command line utilities that track and record changes in files (most often source code, but you can track anything you wish). With it you can restore old versions of your project, compare, analyze, merge changes and more. This process is referred to as version control. There are a number of version control systems that do this job. You may have heard some of them — SVN, Marcurial, Perforce, CVS, Bitkeeper and more, Git is decentralized, which means that it doesn't depend on a central server to keep old versions of your files. Instead it works fully locally by storing this data as a folder on your hard drive, which we call a repository. However you can store a copy of your repository online, which makes it easy for multiple people to collaborate and work on the same code. This is what websites like GitHub and BilBuckel are used for. 4. Installing Git Installing Git on your machine is straightforward: + Linux - Simply open up a new terminal and install git via your distribution’s package manager. For Ubuntu the command is: sudo apt-get install git-all + Windows — we recommend ait for windows as it offers both a GUI client and a BASH comand line emulator, + OS X ~The easiest way is to install homebrew, and then just run brew install git from your terminal If you are an absolute beginner, then a graphical git client is a must. We recommend GitHub Desktop and Sourcetree, but there are many other good and free ones online. Getting to know the basic git commands is still important even if you use a GUI app, so for the remaining of this lesson, this will be our only focus. 2. Configuring Git Now that we've installed git on our computer, we will need to add some quick configurations. There are a lot of options that can be fiddled with, but we are going to set up the most important ones: our username and ‘email. Open a terminal and run these commands $ git config --global user.nane "My Nane’ § git config --global user.enail nyEnailgexanple.com Every action we do in Git will now have a stamp with our name and address on it. This way users always know who did what and everything is way more organized, 3. Creating a new repository — git init hiipunocalane.com2018)06.earr-g-n-30- mines! antasz016 Learn Gitin 30 Minutes |Tuoriatzine ‘As we mentioned earlier, git stores its files and history directly as a folder in your project. To set up a new repository, we need to open a terminal, navigate to our project directory and run git init. This will enable Git for this particular folder and create a hidden .git directory where the repository history and configuration will be stored. Create a folder on your Desktop called git_exercise, open a new terminal and enter the following! 5 cd Desktop/git_exercise/ § git init ‘The command line should respond with something along the lines of: This means that our repo has been successfully created but is still empty. Now create a simple text file called hello.txt and save it in the git_exercise folder. 4, Checking the status - git status Git status is another must-know command that retums information about the current state of the repository: is everything up to date, what's new, what's changed, and so on, Running git status in our newly created repo should retum the following: $ git status (on branch master Init commit Untracked fi (use "git add..." to include in what will be comni hello.txt The retumed message states that hello.txt is untracked. This means that the file is new and Git doesn't know yet if it should keep track of the changes happening to that file or just ignore it. To acknowledge the new file, we need to stage it 5. Staging — git add Git has the concept of a ‘staging area’. You can think of this like a blank canvas, which holds the changes which you would like to commit. It starts out empty, but you can add files to it (or even single lines and parts of files) with the git aéd command, and finally commit everything (create a snapshot) with git In our case we have only one le 30 let's add that Ite ward to 84d everyting nthe ecto, we can use: hiipunocalane.com2018)06.earr-g-n-30- mines! antasz016 Learn Gitin 30 Minutes |Tuoriatzine Checking the status again should retum a different response from before. § git status (on branch master Changes to be comitted: (use “git rm —-cached ..." to unstage) new file: hello.tet Our file is ready to be commited, The status message also tells us what has changed about the files in the staging area — in this case its new file, but it can be moaiffed or deleted, depending on what has happened toa file since the last git aed 6. Commiting ~ git commit ‘A.commit represents the state of our repository at a given point in time. It's ike a snapshot, which we can go back to and see how thing were when we took it, To create a new commit we need to have at least one change added to the staging area (we just di that with git ada) and run the following $ git commit -m “Initial comit This will create a new commit with all the changes from the staging area (adding hello.txt). The -» “Initial conmait" part is a custom user-written description that summarizes the changes done in that commit. It is considered a good practice to commit often and always write meaningful commit messages. @ Bitbucket Remote repositories Right now our commit is local — it exist only in the .git folder. Although a local repository Is useful by itself, in most cases we will want to share our work and deploy it to a server or a repository hosting service. 1. Connecting to a remote repository — git remote add In order to upload something to a remote repo, we first have to establish a connection with it. For the sake hiipunocalane.com2018)06.earr-g-n-30- mines! antasz016 Learn Gitin 30 Minutes |Tuoriatzine of this tutorial our repository's address will be https://github. com/tutorialzine/awesome-project. We advise you to go ahead and create your own emply repository at GitHub, BitBucket or any other service. The registration and setup may take a while, but all services offer good step-by-step guides to help you. ‘To link our local repository with the one on GitHub, we execute the following line in the terminal: 4 This is only an example. Replace the URT with your own repository address. $ git renote add onigin https://github.con/tutorialzine/awesore-project.git A project may have many remote repositories at the same time. To be able to tell them apart we give them different names. Traditionally the main remote repository in git is called origin. 2. Uploading to a server — git push Now it's time to transfer our local commits to the server. This process is called a push, and is done every time we want to update the remote repository. The Git command to do this is git push and takes two parameters — the name of the remote repo (we called ours origin) and the branch to push to (masteris the default branch for every repo). $ eit push onigin master Counting objects: 3, done. writing objects: 100% (3/3), 212 bytes | @ bytes/s, done Total 3 (delta @), reused @ (delta ®) To https://github.con/tutorialzine/awesone-project git * [new branch] master -> naster Depending on the service you're using, you will need to authenticate yourself for the push to go through, If everything was done correctly, when you go in your web browser to the remote repository created earlier, hello.txt should be available there 3, Cloning a repository — git clone ‘At this point, people can see and browse through your remote repository on Github. They can download it locally and have a fully working copy of your project with the it clone command: § git clone https://github.con/tutortalzine/awesone-project. ‘Anew local respository is automatically created, with the github version configured as a remote 4. Getting changes from a server ~ git pull I you make updates to your repository, people can download your changes with a single command — pull $ eit pull origin master From https://github.con/tutorialzine/awesone-project + branch aster -> FETCH HEAD Already up-to-date. Since nobody else has commited since we cloned, there weren't any changes to download. hiipunocalane.com2018)06.earr-g-n-30- mines! sitasz016 Learn Gitin 30 Minutes |Tuoriatzine @-@ @-0@-0e—® Branches ‘When developing a new feature, itis considered a good practice to work on a copy of the original project, called a branch, Branches have their own history and isolate their changes from one another, until you decide to merge them back together. This is done for a couple of reasons: + An already working, stable version of the code won't be broken. ‘+ Many features can be safely developed at once by different people. + Developers can work on their own branch, without the risk of their codebase changing due to someone else's work. ‘+ When unsure what's best, multiple versions of the same feature can be developed on separate branches and then compared. 1. Creating new branches ~ git branch The default branch of every repository is called master, To create additional branches use the git branch
command: $ git branch anazing new feature This just creates the new branch, which at this point is exactly the same as our master. 2. Switching branches - git checkout Now, when we run git branch, we will see there are two options available: § git branch amazing_new_feature Master is the current branch and is marked with an asterisk, However, we want to work on our new amazing features, so we need to switch to the other branch, This is done with the git checkovt_ command, expecting one parameter — the branch to switch to. § pit checkout anazing_new_ feature 3. Merging branches - git merge (Our “amazing new feature” is going to be just another text file called feature.txt. We will create it, add it, and commit it. hiipunocalane.com2018)06.earr-g-n-30- mines! entasz016 Learn Gitin 30 Minutes | Tuoriatzine $ git add feature.txt $ git commit =n "New feature conplete. The new feature is complete, we can go back to the master branch. $ git checkout master Now, if we open our project in the file browser, we'll notice that feature.txt has disappeared. That's because ‘we are back in the master branch, and here feature.txt was never created. To bring it in, we need to it rerge the two branches together, applying the changes done in amazing_new_feature to the main version of the project. sit merge anazing_new feature The master branch is now up to date. The awesome_new_feature branch is no longer needed and can be removed, git branch -d awesone_new_feature Advanced In the last section of this tutorial, we are going to take a look at some more advanced techniques that are very likely to come in handy. 4. Checking difference between commits Every commit has it's unique id in the form of a string of numbers and symbols. To see a list of all commits and their ids we can use git log 5 git log commit ba2scar#3ee:b24025915704209F8#5417408007 Author: Tutordalzine Date: Mon May 30 17:25:28 2016 +9300 Now feature complete commit biecci23#e35Sc02a0eseF9f9R60821 505964 author: Tutordalzine hiipinocalane.com/2018)06/earr--n-30-mins! mtasz016 Learn Gitin 30 Minutes |Tuoriatzine Date: Mon May 30 16:30:04 2026 +0300 Added content to hello. txt commit enbdecc37147084078e4 122346074874¢2059 author: Tutorialzine Date: Sat May 28 17:52:14 2016 +0300 ini commit ‘As you can see the ids are really long, but when working with them it's not necessary to copy the whole thing - the first several symbols are usually enough. To see what was new in a commit we can run git show (commit) $ git show becc123 commit b1@cci238e355¢02a0eseff9R60R11FF605C9b4 author Tutorialzine Date: Mon May 30 16:30:68 2626 +0300 Added content to hello.txt Gift --git a/nelto.txt b/helo.txt dex 969de29. 0546821 100644 s+ ayhello.txt st bjhello.txt 8 -0,0 11 6 sNice weather today, isn't 4¢? To see the difference between any two commits we can use git aif* with the [commitrom]..[commit-to] syntax: § git dif esbasce. .barscorr AIF --git a/featune. txt b/feature. txt new file node 200644 index 0900000, .e65de29 aife --git a/helio.txt b/hello. txt sndex e69de29...S46221 190644 == ayhellontxt s+ bjhetlo.txt 0-00 11 dice weather today, isn’t it? ‘We've compared the first commit to the last one, so we see all the changes that have ever been made. 2. Fixing a commit If you notice that you've made a typo in your commit message, or you've forgotten to add a file and you see right after you commit, you can easily fix this with git conait --anend, This will add everything from the last commit back to the staging area, and attempt to make a new commit. This gives you a chance to fix your commit message or add more files to the staging area, For more complex fixes that aren't in the last commit (or if you've pushed your changes already), you've got to.use git revert . This will take all the changes that a commit has introduced, reverse them, and create a new commit that is the exact opposite. The newest commit can be accessed by the HEAD alias. hiipunocalane.com2018)06.earr-g-n-30- mines! antasz016 Learn Gitin 30 Minutes | Tuoriatzine For other commits it's best to use an id, ‘When reverting older commits, keep in mind that merge conflicts are very likely to appear. This happens when a file has been altered by another more recent commit, and now Git cannot find the right lines to revert, since they aren't there anymore. 3, Resolving Merge Conflicts ‘Apart from the scenario depicted in the previous point, conflicts regularly appear when merging branches or pulling someone else’s work, Sometimes conflicts are handled automatically by git, but other times the person dealing with them has to decide (and usually handpick) what code stays and what is removed. Let’s look at an example where we're trying to merge two branches called john_branch and tim_branch, Both John and Tim are writing in the same file a function that displays all the elements in an array, John is using a for loop: 1/ Use a For 1a0p to console. log contents. For(var $98; icarr. length; i++) { console. 1og(arr{3])3 y Tim prefers forEach: 1/ Use forach to console.log contents. arr.foréach(Function(iten) { console. 1og(‘ten) ; Ds They both commit their code on their respective branch. Now if they try to merge the two branches they will see the following error message: $ git merge tis branch Autocmerging print_array.s COUFLICT (content): Merge conflict in print_array.is autonatic merge failed; #1x conflicts and then commit Git wasn't able to merge the branches automatically, so now it's up to the devs to manually resolve the conflict. If they open the file where the conflict resides, they'l see that Git has inserted a marker on the conflicting lines, 11 Use 2 for Loop to console.tog contents. For(uar $0) icarr.length; i++) ( console. 20g(arr(£]): » 1/ Use forkach to console.log contents. arr.foréach(function(iten) { hiipinocalane.com/2018)06/earr--n-30-mins! ontasz016 Learn Gitin 30 Minutes |Tuoriatzine console. 2og(stem) ; ys doo9p0> Tha’ conn Above the we have the current HEAD commit, and below the conflicting one. This way we can clearly see the differences, and decide which is the better version, or write a new one all together. In this situation we go for the latter and rewrite the whole thing, removing the markers to let Git know we're done. 11 Not using for loop on ForEach, 11 Use Array.toString() to console.1og cor console. log(aer.testring()); When everything is set, a merge commit has to be done to finish the process. $ git add “A $ git commit ‘Array printing conflict resolved. ‘As you can see this process is quite tiresome and can get extremely hard to deal with in large projects. Most developers prefer to resolve conflicts with the help of a GUI client, which makes things much easier. 4. Setting up .gitignore In most projects there are files or entire folders that we don't want to ever commit. We can make sure that they aren't accidentally included in our git add -A by creating a .gitignore file: 1. Manually create a text file called .gitignore and save it in your project's directory. 2. Inside, list the names of files/directories to be ignored, each on a new line. 3. The .gitignore itself has to be added, committed and pushed, as itis just like any other file in the project. Good examples for files to be ignored are: + tog files + task runner builds + the node_modules folder in node,js projects + folders created by IDEs fke Netbeans and Intel ‘+ personal developer notes A itignore banning all of the above will look like this: +208 busi node_nodules/ idea/ ny_notes. txt The slash at the end of some of the lines signals that this Is a folder and we are ignoring everything inside it recursively. The asterisk serves it's usual function as a wild card. hiipunocalane.com2018)06.earr-g-n-30- mines! sontasz016 Learn Gitin 30 Minutes |Tuoriatzine Conclusion This ends our tutorial on git! We tried our best to bring you only the most important information you need, presented as short and concise as possible. Git is quite complex and has a lot more features and tricks to offer. If you wish to find out more, here are some leaming resources we recommend: + The official Git docs, including a whole book and video lessons — here. hore + Getting git right - Atlassian’s collection of tutorials and articles + Allist of GUI clients — here. + Git cheat sheet (PDF) here. QUICK LEARN Article License Privacy Policy Contact Form = Advertise Zine EOOD © 2009-2016 hip ocalane.com2018)06/earr-g-n-30-mins! wm
You might also like
Really Friendly Git Intro
PDF
No ratings yet
Really Friendly Git Intro
26 pages
Common Git Commands
PDF
No ratings yet
Common Git Commands
9 pages
06 GIT Workflow PDF
PDF
No ratings yet
06 GIT Workflow PDF
1 page
Linus Torvalds For Linux Kernel Development
PDF
No ratings yet
Linus Torvalds For Linux Kernel Development
8 pages
Git Basic
PDF
No ratings yet
Git Basic
45 pages
Git and GitHub
PDF
No ratings yet
Git and GitHub
40 pages
GIT AND GITHUB
PDF
No ratings yet
GIT AND GITHUB
21 pages
Git Handbook
PDF
No ratings yet
Git Handbook
6 pages
Easy Version Control With Git
PDF
No ratings yet
Easy Version Control With Git
18 pages
Learn The Basics of Git in Under 10 Minutes
PDF
No ratings yet
Learn The Basics of Git in Under 10 Minutes
18 pages
Github Basics V 0.1: Aaron Cheng March 21 2014
PDF
No ratings yet
Github Basics V 0.1: Aaron Cheng March 21 2014
3 pages
Git Commands
PDF
No ratings yet
Git Commands
20 pages
Git Tutorial
PDF
No ratings yet
Git Tutorial
8 pages
Learning Git
PDF
No ratings yet
Learning Git
17 pages
Github Short Notes
PDF
No ratings yet
Github Short Notes
10 pages
GIT
PDF
No ratings yet
GIT
33 pages
Version Control With Git
PDF
No ratings yet
Version Control With Git
25 pages
GitHub Learn final content (1)
PDF
No ratings yet
GitHub Learn final content (1)
12 pages
Git Slides
PDF
No ratings yet
Git Slides
34 pages
10 Basic Git Commands to Get You Started
PDF
No ratings yet
10 Basic Git Commands to Get You Started
22 pages
Version Control
PDF
No ratings yet
Version Control
15 pages
Git Cheatsheet r2v1 PDF
PDF
No ratings yet
Git Cheatsheet r2v1 PDF
6 pages
Git and Github Commands 1719287860
PDF
No ratings yet
Git and Github Commands 1719287860
4 pages
Git
PDF
No ratings yet
Git
31 pages
Git
PDF
No ratings yet
Git
37 pages
Goal Oriented Git
PDF
No ratings yet
Goal Oriented Git
62 pages
Git Docs1
PDF
No ratings yet
Git Docs1
12 pages
GIT Docs
PDF
No ratings yet
GIT Docs
9 pages
GUI All The Git Commands
PDF
No ratings yet
GUI All The Git Commands
12 pages
Git-Workflow-quick-guide
PDF
No ratings yet
Git-Workflow-quick-guide
15 pages
1_Git Tutorial for beginners
PDF
No ratings yet
1_Git Tutorial for beginners
14 pages
A Git Cheat Sheet (Git Command Reference) - A Git Cheat Sheet and Command Reference
PDF
No ratings yet
A Git Cheat Sheet (Git Command Reference) - A Git Cheat Sheet and Command Reference
14 pages
GITHUB
PDF
No ratings yet
GITHUB
38 pages
CSE 390 "Lecture 11": Version Control With Git
PDF
No ratings yet
CSE 390 "Lecture 11": Version Control With Git
23 pages
Git Tutorialedureka
PDF
No ratings yet
Git Tutorialedureka
23 pages
Lecture 1 Notes
PDF
No ratings yet
Lecture 1 Notes
9 pages
Git For Scribd
PDF
No ratings yet
Git For Scribd
43 pages
Ruby Specialisation: Siddhant Pyasi June 3, 2017
PDF
No ratings yet
Ruby Specialisation: Siddhant Pyasi June 3, 2017
6 pages
Git Intro PDF
PDF
No ratings yet
Git Intro PDF
2 pages
Git Github PDF
PDF
No ratings yet
Git Github PDF
77 pages
?git Cheat Sheet
PDF
No ratings yet
?git Cheat Sheet
2 pages
COSC Git Workshop
PDF
No ratings yet
COSC Git Workshop
23 pages
Git & Github: Basic Resources To Follow Status
PDF
No ratings yet
Git & Github: Basic Resources To Follow Status
8 pages
Basic GIT Commands: Understanding The GIT Workflow
PDF
No ratings yet
Basic GIT Commands: Understanding The GIT Workflow
6 pages
GIT AND GITHUB LESSON
PDF
No ratings yet
GIT AND GITHUB LESSON
51 pages
Lecture 2 Git and Github
PDF
No ratings yet
Lecture 2 Git and Github
16 pages
Git Notes ?-1
PDF
No ratings yet
Git Notes ?-1
71 pages
INFO2180
PDF
No ratings yet
INFO2180
67 pages
Git For 61C
PDF
No ratings yet
Git For 61C
9 pages
Workshop 2022-03-30 Getting Started With Git and GitHub
PDF
No ratings yet
Workshop 2022-03-30 Getting Started With Git and GitHub
47 pages
FER202_Slot01_Exercise 3_GIT
PDF
No ratings yet
FER202_Slot01_Exercise 3_GIT
10 pages
[Git Notes] - TheTestingAcademy (Pramod Sir) (1)
PDF
No ratings yet
[Git Notes] - TheTestingAcademy (Pramod Sir) (1)
21 pages
Git Work Book
PDF
100% (3)
Git Work Book
74 pages
1.2_ Git Basics _ Environment Variables (2201)-Class (2)
PDF
No ratings yet
1.2_ Git Basics _ Environment Variables (2201)-Class (2)
23 pages
390aGitIntro 12au
PDF
No ratings yet
390aGitIntro 12au
23 pages
01 Version Control
PDF
No ratings yet
01 Version Control
37 pages
Beginners Python Cheat Sheet PCC Git BW
PDF
No ratings yet
Beginners Python Cheat Sheet PCC Git BW
2 pages
Amie Section B Computer Science Question Paper Operating System
PDF
100% (1)
Amie Section B Computer Science Question Paper Operating System
23 pages
Life Is Beautiful
PDF
No ratings yet
Life Is Beautiful
4 pages
Ciena Acronyms Guide
PDF
No ratings yet
Ciena Acronyms Guide
26 pages
624 Handbook of Computer Networks
PDF
No ratings yet
624 Handbook of Computer Networks
2 pages