Git and GitHub

Git and GitHub

Let's start with an application to which you've added a few new features. But it's creating downtime for the application, so you want to go back to the previous Version. Then do you build the entire application or roll back to the previous stable version? we simply roll back to the previous version. Git is a Version control system. Git helps to go back into the application. In open source, many people contribute to one folder of a project, and Git helps us find who changed which file on a particular date and maintains a snapshot of all the changes made in the project. GitHub is an online website that hosts our projects(repositories).

Basic Linux Commands

ls

  • gives you all the folders and files in the current directory.ls -a gives us all the hidden files and folders.ls -l use a long listing format.
ls
ls -a
ls -l

mkdir

  • To create a directory we mkdir command
mkdir project_name

cd

  • To change the directory we use the command cd directory_name . . is current directory and .. is previous directory f
cd project_name

touch

  • touch the command helps to create a file
touch file1.txt

Git commands

To initialize a git in a directory we use git init

git init  

Initialized empty Git repository in C:/Users/user_name/Desktop/project/.git/

Git status

we can check the status of files whether the files are tracked or untracked

Git add

we can track files using git add file_name

if we wanna add all untracked files then we have the command git add ..

but these changes are not stored until we commit the changes

Git commit

To commit all the changes git commit -m "message" is used

Git log

  • git log gives details of the commit at which time, date, Author and with the commit id

Restoring staged files

Untrack any unwanted files we may have unintentionally tracked by using the following command:

git restore --staged file2.txt

Resetting a commit

going back to the stable version by resetting the commit using the commit hash value

git reset 3e3e0365d01dc321c59f6393875447e03e6b6d4c

Stashing

Saving working directory and index state WIP on master.

git stash

calling the stashed area files with the command.

git stash pop

Clear the stash area files if they exist.

git stash clear

GitHub

Create a new Repository

Attaching the remote repository URL to the Local Project

git remote add origin https://github.com/UserName/Project.git

We have a command to find out which URLs point to the local repository.

git remote -v

Moving the files from the local repository to the remote repository.

git push origin master

Branches

Branches in Git are incredibly lightweight.

Don't ever commit anything to the main branch. The main branch is the default branch. All incomplete code must be transferred to a separate branch to prevent the main code.

git branch feature
git checkout feature

The head that was pointing to the main branch has been switched to the feature branch. You need to merge the feature branch with the main branch to add your features to the project.

git merge feature

However, you lack access when it comes to merging branches in other remote repositories. How do we then merge that feature branch?

  1. By forking the project, we first make a copy of it.

  2. make a copy of the forked project in your local repository.

    git clone url
    
  3. We need to add the URL of the remote main repository to the local repository.

    git remote add upstream url-origin
    
  4. Now merge the feature branch of my account with that of the main account main branch

    git push origin feature
    
  5. Now we navigate to the forked repository in our GitHub account and click the green Create Pull Request button. we need to make sure that our accounts feature branch is requesting for merging with the main repository main branch.

    if the request is valid the request is merged by the main repository members.

Removing a commit from the pull request by force

git reset commit_id
get stash
git add .
git commit -m "updated"
git push origin feature -f

Merge Conflict

Git becomes confused about which code needs to be merged when two people modify the same line of code in a single file. By overriding, we can resolve the conflicts.

Did you find this article valuable?

Support Supraja Manda by becoming a sponsor. Any amount is appreciated!