Git and Github
Git and Github
What is Git?
Git is a popular distributed copy based version control system.
It is used for:
What is GitHub?
● The key difference between git and github is that git is a free open source version control that
developers install locally on their personal computer while github is an online service built to run git
on cloud.
HOW TO USE GIT
STEP -1
STEP -2
STEP -3 - NOW CHECK WHETHER THE GIT IS INSTALLED OR NOT FOR THAT WE HAVE TO
CHECK THE VERSION OF GIT BY USING GIT COMMAND (git --Version)
Note: Use global to set the username and e-mail for every repository on your computer.
If you want to set the username/email for just the current repo, you can remove global
● Once you have navigated to the correct folder, you can initialise Git on that folder:
● Note: Git now knows that it should watch the folder you initiated it on.Git creates a hidden
folder to keep track of changes.
● You just created your first local Git repo. But it is empty.
● So let's add some files, or create a new file using your favourite text editor. Then save or
move it to the folder you just created.
● For this example, I am going to use a simple HTML file like this:
● And save it to our new folder as index.html.
● Let's go back to the terminal and list the files in our current working directory:
● ls will list the files in the directory. We can see that index.html is there.
● Then we check the Git status and see if it is a part of our repo:
● Now Git is aware of the file, but has not added it to our repository!
● Tracked - files that Git knows about and are added to the repository
● Untracked - files that are in your working directory, but not added to the repository
When you first add files to an empty repository, they are all untracked. To get Git to track them,
you need to stage them, or add them to the staging environment.
STEP -6 GIT STAGING ENVIRONMENT
In the staging environment we Staged files files that are ready to be committed to the
repository you are working on.
● For now, we are done working with index.html. So we can add it to the Staging
Environment:
We can Add More than One File
STEP-7- COMMIT
● Since we have finished our work, we are ready to move from stage to commit for our
repo.
● Adding commits keep track of our progress and changes as we work.
● Git considers each commit change point or "save point". It is a point in the project you can
go back to if you find a bug, or want to make a change.
● When we commit, we should always include a message.
● By adding clear messages to each commit, it is easy for yourself (and others) to see what
has changed and when.
● The commit command performs a commit, and the -m "message" adds a message.
● The Staging Environment has been committed to our repo, with the message:
"First Change"
We can multiple time edit and modify and commit
Git Commit without Stage
Sometimes, when you make small changes, using the staging environment seems like a waste of
time. It is possible to commit changes directly, skipping the staging environment. The -a option
will automatically stage every changed, already tracked file.
NOTE
We can check the status of our repository. But this time, we will use the --short option to see
the changes in a more compact way:
GITIGNORE
Gitignore is a file that tells git which files and folders to ignore. It is a way to prevent git from
tracking certain files or folders. You can create a gitignore file and add list of files and folders to
ignore by using the following command
GIT HELP
● git command -help - See all the available options for the specific command
● git help --all - See all possible commands
GIT BRANCHES
Head In Git
The head pointer to the current branch on which you are working. It points to the latest
commit in the current branchages.when you create a new branch it will automatically set as the
head of the branch.
● git branch - This command lists all the branches in the current repository.
● git branch bug-fix - This command creates a new branch called bug-fix.
● git switch bug-fix /git checkout bug-fix- This command switches to the bug-fix
branch.
● git log - This command shows the commit history for the current branch.
● git switch master/git checkout master- This command switches to the master
branch.
● git switch -c dark-mode - This command creates a new branch called dark-mode.
the -c flag is used to create a new branch
EMERGENCY BRANCH
● Now imagine that we are not yet done with hello-world-images, but we need to fix an error
on master.
● I don't want to mess with master directly, and I do not want to mess with
hello-world-images, since it is not done yet.
● So we create a new branch to deal with the emergency
Using the -b option on checkout will create a new branch, and move to it, if it does not EXIST
MERAG BRANCHES
Fast-forward merge
git merge bug-fixThis one is easy as the branch that you are trying to merge is usually
ahead and there are no conflicts.
When you are done working on a branch, you can merge it back into the main branch. This is done
using the following command
Managing conflicts
There is no magic button to resolve conflicts. You have to manually resolve the conflicts. Decide
what to keep and what to discard. VSCode has a built-in merge tool that can help you resolve the
conflicts. I personally use the VSCode merge tool. Github also has a merge tool that can help you
resolve the conflicts but most of the time I handle them in VSCode and it gives me all the options
to resolve the conflicts.
Overall it sounds scary to beginners but it is not, it’s all about communication and understanding
the code situation with your team members.
● Rename a branch
● git branch -m <old-branch-name> <new-branch-name>
● Delete a branch
● git branch -d <branch-name>
Conflicting changes will not allow us to switch branches without committing the changes. Another
alternative is to use the git stash command to save our changes in a temporary location.
git stash- This command saves your changes in a temporary location. It is like a stack of
changes that you can access later
git stash save "work in progress on X feature" -You can also name the stash
by using the following command:
git stash list - You can view the list of stashes by using the following command:
git stash apply- You can apply the stash by using the following command:
git stash apply stash@{0}-You can apply the specific stash by using the following
command:Here stash@{0} is the name of the stash. You can use the git stash list
command to get the name of the stash.
git stash pop - This command applies the stash and drops it from the stash list.
git stash drop -You can drop the stash by using the following command:
git stash apply stash@{0} <branch-name> -You can apply the stash to a specific
branch by using the following command:
git stash clear- You can clear the stash by using the following command:
What is Github?
Github is a web-based Git repository hosting service. It is a popular platform for developers to
collaborate on projects and to share code. Github provides a user-friendly interface for
managing and tracking changes to your code, as well as a platform for hosting and sharing
your projects with others.
● Gitlab
● Bitbucket
● Azure Repos
● Gitea
What is SSH
SSH is a secure shell network protocol that is used for network management, remote file
transfer, and remote system access.
SSH uses a pair of SSH keys to establish an authenticated and encrypted secure network
protocol. It allows for secure remote communication on unsecured open networks.
PUSH TO GITHUB
STEP-1 - Create a new Repo on your system first, add some code and commit it.
STEP-2- Check remote URL settingns This will show you the remote url of your repository.A remote URL is Git's
fancy way of saying "the place where your code is stored." That URL could be your repository on GitHub, or
another user's fork, or even on a completely different server.
Here we can see that there is no remote url on which we can push our code , so first we have to add a remote
repository by running the following command:
origion - This is used to refer to the remote repository in future or we can say that origin is the name of remote
repositery
STEP-4 - Now remote connection is established and you can now push code to remote repository by using the given
code
git push remote-name branch-name
remote-name- remote-name is the name of the remote repository in which you want to push
branch-name- branch-name is the name of the branch that you want to push.
Fork - A fork is a copy of a repository. This is useful when you want to contribute to someone
else's project or start your own project based on theirs.
Clone- A clone is a full copy of a repository, including all logging and versions of files.
git clone https://github.com/w3schools-test/w3schools-test.github.io.git
Push- Now we push them to our GitHub fork:
Pull Request- A pull request (PR) is a way for developers to propose changes to a Git
repository and request that they be merged into the main codebase. PRs are also known as
merge requests.