What is Git?
Git is a Version control system which is free and open-source.
You can manage changes made to the code and as well see older versions .
Your team can work together without messing each others work .
Let see few terms of git so that we can go easy while explaining more. This is not basic course but a refresh of Github for viewers.
Repository: Its more like a folder /main folder that has code.
Branches: Branches are parallel versions of your project. If you want to make changes to code, you generally make a copy of your existing code and the make changes to the copy. similarly in you project, In the repo, you can make a branch and make changes that will not affect your original working code.
Pull Request: a way to move code from one branch to another. Say I have made change in development branch and want to push code , i need to test it test branch, I will push code to test branch using PR.
Working directory: this is where you make changes to your files. It’s like your workspace, holding the current state of your project that Git hasn’t yet been told to track.
Staging area: also called the index, this is where you prepare changes before committing them. It’s like a draft space, allowing you to review and adjust changes before they become part of the project’s history.
Local repository: your local repository is your project’s history stored on your computer. It includes all the commits and branches and acts as a personal record of your project’s changes.
Remote repository: a remote repository is a version of your project hosted on the internet or network. It allows multiple people to collaborate by pushing to and pulling from this shared resource.
Merge: merging is the process of integrating changes from one branch into another. It combines the histories of both branches, creating a single, unified history.
What is Github?
Github is a platform to host your repositories online .Git is a software and Github is one of the platform used to host it.
How the flow works?
Now lets assume few things . Consider there is already a Git repo created and is existing. I will cover the new repo creation in another section.
Git instead of storing differences , it will store snapshots.
This Diagram gives more explanation on how the flow happens. As we already have a Gitrepo , lets clone it into local
git clone <repository url>
Now what is repository url- its the address of the repository on the Github.
Once you provide it from your command prompt, it will create a folder in the path you ran command. The name of the repo will be your folder name. If you wanna specify a different folder name, You can specify it as addition argument.
git clone <repository url> <Foldername>
lest add a file say data_ing.txt. In the local folder just create a file. In the command promys, type the command to check the git status.
git status
this output shows which branch you are in and there are untracked files which will be your newly created file. Now you are on working directory. now add file to staging are.
git add data_ing.txt
if multiple files and you dont want to mention all name then use command.
git add .
this will move file to staging are. no output will be returned.
git commit -m <message>
if you commit anything its always good practice to provide a message on what was being added/changed.
lets now see whats Branch/branches.
A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically.
For creating new branch testing
git branch testing
this will be creating a branch from current branch/main branch.
c0, c1 and c2 are commits for master branch. created a branch iss53$ git branch iss53
$ git checkout iss53
git checkout is command to switch, create new branch, modify files . its more of multipurpose command .
git branch this will lists out all the branches.
git switch <branchname>
when the code is branch and you need to promote you need to merge it.
git switch master
git merge iss53
Occasionally you may see some conflicts while you merge. Use git status to check on conflicts .
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
you can open the conflicts and resolve
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.html
run git add on each of the file and then check for git status and git commit.
what is SHA?
Secure Hash Algorithm is used to identify the snapshot and references of objects. If you commit a change of code, then a commit: sha will be generated. Lets see an example.
git commit -m "Added a comment"
git log
Lets look into some git commands.
To synchronize your work with a given remote, you run a git fetch <remote> command
With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.
git rebase <branchname>
You branched a topic branch (server) to add some server-side functionality to your project, and made a commit. Then, you branched off that to make the client-side changes (client) and committed a few times. Finally, you went back to your server branch and did a few more commits.
Suppose you decide that you want to merge your client-side changes into your mainline for a release, but you want to hold off on the server-side changes until it’s tested further. You can take the changes on client that aren’t on server and replay them on your master branch by using the --onto option of git rebase:
git rebase --onto master server client
This basically says, “Take the client branch, figure out the patches since it diverged from the server branch, and replay these patches in the client branch as if it was based directly off the master branch instead.” It’s a bit complex, but the result is pretty cool.
Comments
Post a Comment