Git & GitHub

Git & GitHub

Git is a version control tool (software) to track the changes in the source code.

GitHub is a web-based cloud service to host your source code(Git repositories). It is a centralized system. Git doesn’t require GitHub but GitHub requires Git.

Pushing & Pulling :

Push: The?git push?command is used to transfer or push the commit, which is made on a local branch in your computer to a remote repository like GitHub. The command used for pushing to GitHub is given below.

Syntax : git push 'remote_name' 'branch_name'

Pull: If you make a change in a repository, GIT PULL can allow others to view the changes. It is used to acknowledge the change that you've made to the repository that you're working on. Or also called a target repository.

Syntax : git pull 'remote_name' 'branch_name'

The?git pull?command is a combination of?git fetch?which fetches the recent commits in the local repository and?git merge, which will merge the branch from a remote to a local branch also 'remote_name' is the repository name and 'branch_name' is the name of the specific branch.

Initializing a new git repository :

The?git init?command creates a new Git repository. It can be used to convert an existing, unversioned project to a Git repository or initialize a new, empty repository.?

Syntax : git?init

1. A new repository from scratch

  • Create a directory to contain the project.
  • Go into the new directory.
  • Type?git init.
  • Write some code.
  • Type?git add?to add the files.
  • Type?git commit.

2. A new repository from an existing project

  • Go into the directory containing the project.
  • Type?git init.
  • Type?git add?to add all of the relevant files.
  • Type?git commit.

Use of git clone and how to use it :

  • git clone?is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location.
  • The original repository can be located on the local filesystem or on remote machine accessible supported protocols.

Syntax : git?clone?<repo>?<directory>

Ignoring some files/folders from pushing :

  • ?when working on a project that uses Git, you’ll want to exclude specific files or directories from being pushed to the remote repository. This is where?.gitignore?file comes in handy.
  • The?.gitignore?file specifies what untracked files Git should ignore.
  • .gitignore?is a plain text file in which each line contains a pattern for files or directories to ignore.
  • You’ll have to create a?.gitignore?file to indicate all of the files you don’t want to track. Use?git add .gitignore, too.

What's a Branch :

  • In Git, a branch is a pointer to a specific commit.
  • The branch pointer moves along with each new commit you make, and only diverges in the graph if a commit is made on a common ancestor commit.
  • The master branch is a default branch in Git. It is instantiated when first commit made on the project.
  • When you make the first commit, you're given a master branch to the starting commit point.
  • When you start making a commit,?then master branch pointer automatically moves forward and a repository can have only one master branch.

Which branch should be used to keep deployment-ready code ?

Master branch is the branch in which all the changes eventually get merged back. It can be called as an official working version of your project. So, Master/Main Bracnch should be used to keep deployment-ready code.

How create a new branch called development from the main branch ?

  • First, make sure that you are in main branch by using $ git branch command.
  • If not, use $ git checkout main command to go the main branch.
  • To create a new branch named development, use the following syntax

$ git branch development

Checkout one more branch deployment from the previous branch

$ git checkout -b deployment

Push different data into both branches

In development branch:

  • touch README.txt
  • git add README.txt
  • git commit -m ‘Adding data to development branch’
  • git push -set--upstream origin development

In deployment branch:

  • touch mySecondFile.html
  • git add .
  • git commit -m ‘Adding data to deployment branch’
  • git push -set--upstream origin deployment

Merge data from both branches to the main branch

  • git checkout master
  • git merge development
  • git merge deployment (An editor will be opened for commit message press ESC and :wq)

How to resolve conflict with merge

  • When you have merge conflicts, you can't click the?Merge?button from the pull request to merge.?To resolve these conflicts, you pull the changes to your local repository and fix them there.?
  • The most direct way to resolve a merge conflict is to edit the conflicted file.?
  • The easiest way to resolve a conflicted file is to open it and make any necessary changes
  • After editing the file, we can use the git add a command to stage the new merged content
  • The final step is to create a new commit with the help of the git commit command
  • Git will create a new merge commit to finalize the merge

What is rebase and how is it different from merge in git?

  • Git rebase compresses all the changes into a single “patch.” Then it integrates the patch onto the target branch.
  • Unlike merging, rebasing flattens history. It transfers the completed work from one branch to another. In the process, unwanted history is eliminated.
  • Git rebase and merge both integrate changes from one branch into another. Where they differ is how it's done.
  • Git rebase moves a feature branch into a master. Git merge adds a new commit, preserving the history.


要查看或添加评论,请登录

社区洞察

其他会员也浏览了