Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 20, 2020 01:01 pm GMT

Git and Github for beginners

What is Git

Git is a distributed version-control system for tracking changes in source code during software development.It is designed for coordinating work among programmers, but it can be used to track changes in any set of files.

This basically means:

  • It's a system that records changes to our files over time.
  • You can recall and go back a specific version of that file at any time.
  • Other developers can collaborate and have a local version of the files in their own computers.

Why use git ?

  • You can store revisions of your project in one directory.
  • Easily go back to your revisions at anytime.
  • Work on new features without messing up with the original codebase.
  • Collaborate with other developers without a geographically constriction.

What is Github?

Github is an online service that you can host your project, share your code and helps other developers to download and work on it. Later they can upload their code edit and merge with main branch/codebase( master branch).

How to install Git

  • Go to the Git website
  • If you are using windows i would recommend Cmder. It's a command line interface for windows. Download the full version that comes with git installed. (I have used it and it's absolutely worth it).

How set your username & email

Open your cdmer (I am going to use it as I work through this post)

   git config --global user.name jane tracy 

For setting up the email

   git config --global user.email [email protected] 

To see user details that you registered as

   git config user.name   git config user.email 

Basic command controls

  • To create a folder : mkdir test
  • To create a file: touch index.html style.css app.js
  • To delete a file : rm index.html
  • To see inside a folder : ls (Ls)/ dir
  • To move up a folder : cd ..
  • To delete a folder : rmdir test
What is a Git repository (.git)

This is a folder inside your project where git tracks all the changes made in the files and build a history reference over time. In your project the git repository folder you will see a .git folder.
Note: The git repository should be at the root of the project folder, for it to track changes to the whole project.

   git init

A work through the stages in git

Project Image

1) git status

Running git status will show you which files are currently in the staging area.

   git status

If the names of the files listed are in red that means they are not in the staging area. But if they are green, they are in the staging and are ready to commit.

2) git add

Running git add moves the file/files in the staging area. This helps you to review your changes before you commit.
To add a single file

   git add index.html 

To add multiple files

   git add . 

3) git rm

This helps to unstage any file in the staging area.

    git rm --cached index.html 

4) git commit

Running this will commit your files in the staging area. You can also add a descriptive message to the commit to help you when reviewing your project folder in the future.

    git commit -m "added index and styles file"

5) git log / git log --oneline

git log helps you to see the commit history. Each commit has a unique id, the author details, date, time and the commit message.

    git log/ git log --oneline

git log --oneline show the shorter version(log in one line). It includes the id and the commit message.

    ## get commits by a specific author    git log --author jane    ## get commits by message    ## get commit that contains 'index'    git log --all --grep index    ## get commit in the last 2 weeks    git log --since=2.weeks

Undoing things

git undoing image

6) git checkout commit

This helps you to go back to a previous commit. But any changes done will not be saved and the commit history won't be ruined. This is read only stage and this is why it's safer than revert or reset. If you want to create a new branch to retain commits you create, you can by using -c with the switch command.

    git switch -c <new-branch-name>    ## undo this by:    git switch -

7) git revert

It inverts the changes introduced by the commit and creates a new commit with the inverse code.This is safer than using git reset plus it doesn't delete the commit permanently.

8) git reset

This doesn't delete a commit but the commit will not have a direct path from a ref to access them. It alters the commit history. This commits can be found and restored using git reflog.

   git checkout 91471e4   git revert 91471e4   git reset 91471e4   ## Any pending work that was hanging out in the    Staging Index and Working Directory will be lost.   git reset 91471e4 --hard   ##The Staging Index is reset to the state of the specified      commit.Any changes that have been undone from the Staging      Index are moved to the Working Directory.      git reset 91471e4 --mixed  ##The ref pointers are updated and the reset stops there. The     Staging Index and the Working Directory are left untouched.    git reset 91471e4 --soft

For more details about git reset and how it works.

9) git branch

git branch
If you have a new feature you want to try out instead of making commits to the master branch, you can create a new branch which copies the state of code in the master branch, then add new commits and finally merge it to the master branch at the end. If you are not happy with the new feature, you can just delete the branch and go back to the initial codebase.

    ##To create a branch    git branch feature-1    ##To switch to a branch    git checkout feature-1    ## To create and switch to a branch    git checkout -b feature-a    ## To check the branches    git branch -a    ## To delete a branch    git branch -d feature-1    ## To forcefully delete a branch even if it's unmerged    git branch -D feature-1    ## To rename a current branch    ## Rename feature-1 to feature-a    git branch -m feature-a

10) git merge

It will combine multiple sequences of commits into one example you can use it to combine feature-1 to the master branch.

   git merge feature-1

When you have a conflict it might be someone has changed code in the master branch when you were working on another branch. The merge can't happen until you solve the conflict and you can do it manually. Read more about git merge

How to create a repository in Github

11) git push

1) Method one

Let's say you have a project you have been coding and you want to publish it in github for collaboration or just hosting. You can go in github and create a new repository, copy the url of your repo and run:

   ## To push it into the master branch   git push <url link> master

When you want to edit code and push it again to github. You can first create an alias for the remote repo by:

   git remote add origin <url>   git remote add origin https://github.com/muchirijane/git-learning.git   ## To push it into the master branch   git push origin master

12) git clone

2) Method Two

Create a repository in Github and clone it to your computer

   git clone <url>   ## you have a url repo   git clone https://github.com/muchirijane/git-learning.git   ## To push it up in Github to the master branch   git push origin master   ## To see urls of the remote repositories   git remote -v

Collaborating in Github

13) git pull

1) Step 1

This fetches and downloads code from the remote repo(github repo) and updates the local repository to match it.

   git pull origin master
2) step 2

Create a new branch, add your code and make commits

   git checkout -b feature-2
3) Step 3

Push your branch to github and create a pull request

   git push origin feature-2

Conclusion

Git is very useful and important to learn if you want to level up in the tech field. I haven't cover ever git or github command but this are the basics that can help you start. You can check tutorials in youtube to learn more. In future i will make part 2 with more advanced commands.
If you want to know how to create git aliases use this website.


Original Link: https://dev.to/janetracydev/git-and-github-for-beginners-po3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To