top of page
Search

Git and Github Datasheet

  • Writer: Umme Salma
    Umme Salma
  • Jul 21, 2022
  • 7 min read





Lets learn some basics of GIT and GITHUB

INDEX 1. Introduction 1.1 What is Git 1.2 Why Git 1.3 Features of Git 1.4 What is Github 2. Configuring Git for the first time

3. General Git Features 3.1 Initializing Git 3.2 Staging files 3.3 Making a commit 3.4 Status of files and log

4. Git Help 5. Git Branching 5.1 Making a new Git Branch 5.2 Checking all available Branches 5.3 Switching to other Branches 5.4 Making a new branch and directly switching to it 5.5 Deleting a Branch 5.6 Merging two Branches 6. Working with Github 6.1 Push local repo to GitHub 6.2 Pull local repo from GitHub 6.3 Pull branch from GitHub 6.4 Push branch to GitHub 7. Git Undo 7.1 Git Revert 7.2 Git Reset 7.3 Git Amend


What is Git :- · Git is a version controlsystem. · Git helps you keep track of code changes. · Git is used to collaborate on code. · Git and GitHub are different things. Why Git :- Over 70% of developers use Git! Developers can work together from anywhere in the world. Developers can see the full history of the project. Developers can revert to earlier versions of a project. Features of Git :- When a file is changed, added or deleted, it is considered modified You select the modified files you want to Stage The Staged files are Committed, which prompts Git to store a permanent snapshot of the files Git allows you to see the full history of every commit. You can revert back to any previous commit. Git does not store a separate copy of every file in every commit, but keeps track of changes made in each commit!

What is GitHub :- Git is not the same as GitHub. GitHub makes tools that use Git. GitHub is the largest host of source code in the world, and has been owned by Microsoft since 2018.


$ git config --global user.email “<Enter your email here>” $ git config --global user.name “<Enter your username here>”

Configuring git for the first time :-

General Git Features :- $ gitinit Initializing Git :-

Git now knows that it should watch the folder you initiat edit on. Git creates a hidden folder to keep track of changes. Staging files/Adding files to Git repo :- Staged files are files that are readyto be committed to the repository you are working on. 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. $ git add <filename with extension>

$ gitadd --all Staging all files in a folder :-

$ gitadd -A OR



Making a Commit :- 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. $ git commit -m“<Enter your message here>” When we commit,we should always include a message.

Git Commit without Stage :- $ git commit -a-m “<Enter your message here>” 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.

$ gitstatus Status of files and log :-

$ gitstatus --short File status in a more compact way :-

Log of a file :- $ git log --oneline $ git log


Log is used to view the history of commits for a repo.


Git Help :- If you are having trouble remembering commands or options for commands, you can use Git help. $ git <command> -help See all the available options for the specific command -

$ git help --all See all possible commands -

If you find yourself stuck in the list view, SHIFT + G to jump the end of the list, then q to exit the view. Git Branching :- In Git, a branch is a new/separate version of the main repository. Branches allow you to work on different parts of a project without impacting the main branch. When the work is complete, a branch can be merged with the main project. We can even switch between branches and work on different projects without them interfering with each other.


Making a new Git Branch :-

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.

$ git branch <name of branch>

When we commit,we should always include a message.

Checking all available Branches:- $ gitbranch

Switching to other Branches:- $ git checkout <branch name>


Making a new branch and directly switching to it :- $ gitcheckout -b <branch name>>

e> Deleting a Branch:-

Merging two Branches :- It’s preferred to change/switch to master branch before any branch needs to be merged with it. $ git merge <branch name>



This will merge the specified branch with our master branch.


Working with Github :- Create a github account to create your remote repositories. Now, create a new repo where we will be uploading our files from local repo.

Note - Local repository (repo.) means the repo. which is on our system whereas, remote repo. means the one which is on other remotesystem/server, for eg. - GitHub, GitLab, Bitbucket, etc.


Push local repo to GitHub :- Copy the url or the link of the repo that we just created. As an example, it should look like this - https://github.com/usalma1990/example.git Paste the copied url in the below git command. $ git remote add origin <paste copied URL here>

git remote add origin <URL>’ specifies that we are adding a remote repository, with the specified URL, as an origin to our local Git repo.

Finally, pushing our master branchto the origin URL (remoterepo) and set it as the default remote branch. $ git push --set-upstream origin master

Go back into GitHub and see that the repository has been updated. Pushing local repo to github after doing the above process at least once :- First commit all the changes. Then push all the changes to our remote origin i.e. remote repo on github. $ git push origin

Pull local repo from GitHub :- Git pull is used to pull all changes from a remote repository into the branch we are working on. It is a combination of fetch and merge. Use it to update your local Git. $ gitpull origin



Pull branch from GitHub :-

First, check which branches we have and where are we working at the moment by ‘git branch’ command. Since we do not have the new branch on out local Git which is to be pulled from the Github.So, to see all local and remote branches, use - $ git branch -a

$ git branch -r For viewing only remote branches :-

Now, the new branch is seen in the console but it is not available on our local repo. So, let’s check it out using ‘git checkout <branch name>’. Now run ‘git pull’ to pull that branch on our local repo. We can now check the available branches using ‘git branch’. Push branch to GitHub :- First, let’s create a new local branch which we will be pushing to Github. Enter the command as ‘git checkout-b <branch name>’. You can check the status of the files in this current branch using ‘git status’. Commit all the uncommitted changes for all the files in this branch using ‘git commit -a -m “<Message>” ’. Now push this branch from our local repo to Github using ‘git push origin <branch name>’. Git clone from GitHub:- We can clone a forkedrepo from Githubon our local repo. A clone is a full copy of a repository, including all logging and versions of files. Move back to the original repository, and click the green "Code" button to get the URL to clone. Copy the URL.


Now in the git bash, enter the following commandto clone the copied repo onto your local machine - $ gitclone <copied URL>

To specify a specific folderto clone to, add the name of the folder after the repository URL, like this - $ gitclone <copied URL> <folder name>


Git Undo :-

  • This will unstage all files you might have staged with git add: git reset

  • This will revert all local uncommitted changes (should be executed in repo root): git checkout . You can also revert uncommitted changes only to particular file or directory: git checkout [some_dir|file.txt] Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory): git reset --hard HEAD

  • This will remove all local untracked files, so only git tracked files remain: git clean -fdx


Git Revert :- ‘revert’ is the command we use when we want to take a previous commit and add it as a new commit, keepingthe log intact. First thing, we need to find the point we want to return to. To do that, we need to go through the log. To avoid the very long log list, use the --oneline option which gives just one line per commit showing – i. The first seven characters of the commithash ii. The commitmessage Git Revert HEAD :- Werevert the latest commit using ‘git revert HEAD’ (revertthe latest change, and then commit). By adding the option --no-edit, we can skip the commit message editor (getting the default revert message). $ gitrevert HEAD --no-edit

Git Revert to any commit :- To revert to earlier commits,use ‘git revert HEAD~x’(x being a number. 1 going back one more, 2 going back two more, etc.) Git Reset :- ‘reset’ is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit. First, get the seven characters of the commit hash from the log for the commit that you want to go back for. Then we reset our repository back to that specificcommit using ‘git reset commithash’ (commithash being the first 7 characters of the commit hash we found in the log).


$ git reset <commithash>

Git Undo Reset :- Even though the commits are no longer showing up in the log, it is not removed from Git. If we know the commit hash, we can reset to it using ‘git reset <commithash>’. Git Amend :- commit --amend is used to modify the most recentcommit. It combines changes in the staging environment with the latest commit, and creates a new commit. This new commit replaces the latest commit entirely. One of the simplest thingsyou can do with --amend is to changea commit message. $ git commit --amend -m “<Commit Message>”

Using this,the previous commitis replaced with our amendedone. Git Amend Files :- Adding files with --amend works the same way as above. Just add them to the staging environment before committing.










Thanks for Reading!!!

Do share your valuable feedback in the comments if you found this content helpful.

You can also email your feedback or suggestions on my email – salma.ds12@gmail.com

 
 
 

Comments


  • Facebook
  • Twitter
  • LinkedIn

©2022 by TechnologyLearning. Proudly created with Wix.com

bottom of page