MASTERCLASS - TASK ON GIT AND GITHUB
Akanksha Naik
Web Developer | WordPress Developer | Front-End Developer at Tru Performance INC
What’s the difference between Git & Github?
GitHub: GitHub is a web-based Git repository hosting service, which offers all of the distributed revision control and source code management (SCM) functionality of Git as well as adding its own features.
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. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
????????????????????????????????????
What is a repository?
The repository is like a data structure used by VCS to store metadata for a set of files and directories. It contains the collection of the files as well as the history of changes made to those files. Repository in Git is considered as your project folder. A repository has all the project-related data. Distinct projects have distinct repositories.
General workflow?
You clone the Git repository as a working copy.
Shown below is the pictorial representation of the work-flow.
?????????????????????????????
Basic Git Commands :
git init :
This command turns a directory into an empty Git repository. This is the first step in creating a repository. After running git init, adding and committing files/directories is possible.
---$ git init
git add :
Adds files in the to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). There are a few different ways to use git add, by adding entire directories, specific files, or all unstaged files.
---$ git add <file or directory name>
git commit :
Record the changes made to the files to a local repository. For easy reference, each commit has a unique ID.
It’s best practice to include a message with each commit explaining the changes made in a commit. Adding a commit message helps to find a particular change or understanding the changes.
---$ git commit -m "Commit message in quotes"
git status :
This command returns the current state of the repository. Git status will return the current working branch. If a file is in the staging area, but not committed, it shows with git status. Or, if there are no changes it’ll return nothing to commit, working directory clean.
---$ git status
git config :
With Git, there are many configurations and settings possible. git config is how to assign these settings. Two important settings are user user.name and user.email. These values set what email address and name commits will be from on a local computer. With git config, a --global flag is used to write the settings to all repositories on a computer. Without a --global flag settings will only apply to the current repository that you are currently in.
---$ git config <setting> <command>
git branch :
To determine what branch the local repository is on, add a new branch, or delete a branch.
---$ git branch <branch_name>
---$ git branch -a
---$ git branch -d <branch_name>?
git merge
Integrate branches together. git merge combines the changes from one branch to another branch. For example, merge the changes made in a staging branch into the stable branch.
?---$ git merge <branch_name>
git pull
git pull is one of many commands that claim the responsibility of 'syncing' remote content.
????---$ git pull
git push
The git push command is used to upload content to a remote repository.
---$ git push
What is the difference between pushing and pulling?
????????????????????????????????????????????????????????????
Pull
A pull grabs any changes from the GitHub repository and merges them into your local repository.
It combines changes from upstream into your local repository, which is a regular activity in Git-based collaborations. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment.??
---$ git remote add origin <central repository's link>
This command is used to make a central repository the origin.
---$ git pull origin master
This command will copy all files from the remote repository’s master branch to your local repository.
---$ git pull origin <branch-name>
This command is used for pulling files from a different branch.
Push
Pushing sends the recent commit history from your local repository up to GitHub. If you're the only one working on a repository, pushing is fairly simple. If there are others accessing the repository, you may need to pull before you can push.
Git push is a command that pushes your local modifications to a central repository. When one has accumulated multiple local commits and is ready to share it with the rest of the team, use the following command to push them to the central repository:
---$ git push <remote>
Note: When using the pull command, this remote refers to the remote repository that was previously configured.
This syncs the changes from the local repository to the remote repository, including all commits and internal objects. In the target repository, this generates a local branch.
---$ git push origin master
This command is used to reflect the already committed files in the master branch of the central repository.
Git does not enable push when it results in a non-fast-forward merging in the destination repository to prevent overwriting.
---$ git push <remote> --force
Even if the push operation results in a non-fast-forward merging, the above instruction forces it.
How to initialize a new Git repository?
---$ git init
The above command initializes a brand new Git repository and begins tracking an existing directory. It adds a hidden subfolder within the existing directory that houses the internal data structure required for version control.
Common usages and options for git init
$ git init <directory>
Transforms a directory in the current path into a Git repository
$ git init --bare
Creates a new bare repository (a repository to be used as a remote repository only, that won’t contain active development)
git init vs git clone
It can be tricky to get started on a new project. It’s not always clear if one should use git init, git clone, or both.
git init: Creating a Local Repository by One Person
Although ones project may already exist locally, it does not currently have Git. git init is most likely the best option for one. Even if other participants share the project, this is only run once.
git clone: The Remote Already Exists
---$ git clone
If there are commits and files in the remote repository but one would still like it to contain his/her project files, git clone that repository. Then, move the project’s files into that cloned repository. git add, git commit, and git push to create a history that makes sense for the beginning of their project. Then, the team can interact with the repository without git init again. One should use git clone instead of git init if the repository already exists on a remote.
One may need to do a few extra steps if one creates a remote repository first and then moves the project to it later. If the remote repository has no commits, one can use the git init command as described above.
git init: Existing Folder
The default behavior of git init is to turn the current directory into a Git repository. Navigate to the root directory of an existing project to make it a Git repository. After that, run git init.
Alternatively, one can create a new repository in his/her current path. Specify the directory to turn into a Git repository with git init <directory>.
git init: Gone Wrong
If one runs git init in the wrong location, one will end up with undesired repositories. When using Git, one may have encountered unusual error messages. One may think another parent directory is a Git repository as well.
To correct this, one must first determine which directory contains the unwanted repository. To see if Git is tracking the current directory, type git status. If that’s the case, one can use ls -al to search for an otherwise hidden .git directory.
If one don’t see it, use cd… to go one level up in the directory hierarchy. Use git status in conjunction with ls -al once again. Repeat until .git directory is located.
What is the use of git clone and how to use it?
The git clone command is used to duplicate a repository or a branch inside a repository.
领英推荐
Git is a version control system that is distributed. By cloning, you may reap the benefits of having a whole repository on your own workstation.
---git clone https://github.com/github/training-kit.git
When one clones a repository, unlike other centralized version control systems, one does not obtain a single file. When one clones a repository with Git, his/she gets the complete repository, including all files, branches, and commits.
Cloning a repository is usually done only once, at the start of a project’s interaction with him. One would clone a repository that already exists on a remote, such as GitHub so that one could interact with it locally. One would not need to clone a repository again to undertake normal development once he/she has cloned it.
All developers will be able to work more freely now that they have access to the complete repository. Working on a feature branch allows one to make changes without being constrained by the files one can work on.
Most common uses and options of git clone:
---$ git clone [url]
??????????---$ git clone --mirror
?????????---$ git clone --single-branch
?????????---$ git clone --sparse
---$ git clone --recurse-submodules[=<pathspec]
How to ignore some files/folders from pushing?
There are a few common solutions if one is trying to git push but is having trouble.
Examine your branch:
With git status, one can see which branch one is on right now. One might not be able to push commits directly to the remote if he/she is working on a protected branch like master. It’s okay if this happens to one! There are a few options for resolving this.
There was no work on any branch at the time:
---$ git checkout -b [branchname]
---$ git push -u origin [branchname]
Committed to the incorrect branch by accident:
---$ git checkout [branchname]
---$ git merge [master]
---$ git push
---$ git reset --hard
What do you mean by Branch?
Working on multiple versions of a repository at the same time is known as branching.
Few common branch names used are:
A repository contains one main branch by default, which is considered the definitive branch. Branches are used to test and make changes before committing them to the main branch.
When one builds a branch of the main branch, he/she is essentially producing a replica of the main as it was at the moment. One could pull in updates from the main branch if they were made while one was working on his/her branch.
Let’s imagine you want to add a new feature (which is still in development), but you’re unsure whether or not to make changes to your primary project. Branches allow one to switch back and forth between different project states/versions. In the example, one may establish a new branch and test the new feature without affecting the master branch. When finished, merge the changes from the new branch into the main branch. The master branch is the main branch in this case, which is present by default in your repository.
● Which branch should be used to keep deployment-ready code?
There is a master/production branch with a fresh branch for testing that is used to keep the deployment-ready code, as shown in the above figure. Two sets of changes are made under thi s branch, and once they are finished, they are merged back into the main branch.
● Create a new branch called development from the main branch.
We can use the git branch command to create, list branches on the command line.
---$ git checkout <branch name>
---$ git branch developement
To create a new branch using the Git command line and to switch to the newly created branch, issue the command:
---$ git checkout <branch name>
---$ git checkout developement
?Check out one more branch deployment from the previous branch.
---$ git checkout -b deployement developement
?Push different data into both branches.
---$ git push origin developement
---$ git push origin deployement
Merge data from both branches to the main branch.
---$ git checkout developement
---$ git merge main
?
?
?
?
?
?
?
?
?
?
?
?
?
?
$ git add <file or directory name>
?
?
?
?
?
?