Skip to main content

Command Palette

Search for a command to run...

git & GitHub

Published
5 min read
S

I love teaching and passionate about coding!!

Introduction: git is a version control system that helps us to maintain changes in our code. Any changes made to your project will be picked by git. A git repository is a folder, where all changes are made. Whenever you work on a new feature in your repository, you create a separate branch. git comes with built-in GUI tools and CLI for committing and browsing. But mostly CLI terminal is used for git.

version-control-fig2.png

git uses 3-tier architecture.

  1. Working Directory: It is a folder where your files are stored.
  2. Staging Area: It is an area where files that you want to send to commit (to create a snapshot of files) and
  3. Local Repository: It is a repository where committed files are stored.

Git-3.webp

Linux Commands for git:

  1. ls -->gives list(ls) of files of the current folder.
  2. mkdir <(directory-name)> --> to make a directory of given directory-name.
  3. cd <(project-name)> --> change the current directory to given directory-name.
  4. ls -a –->shows all the hidden files
  5. touch <(file-name)> --> used to create a file.
  6. cat <(file-name)> --> gives all lines in the file. Now do, git add . and then git status, this shows the modified file in the staging area.
  7. rm -rf <(file-name)> --> permanently deletes the files from the system.

git commands for CLI:

EKw-jzoUYAA-9WS.jpg

  1. git init : it initializes an empty GIT repository. It creates. git folder (dot(.) indicates hidden files). It stores the complete history of your files for a project.
  2. git status : to see changes that are not saved(untracked files) in git.
  3. git add . : shows all files in the staging area.
  4. git commit -m <"(message)"> : the commit command performs commit and -m adds a message. Now when you do git status, it shows “nothing to commit” as all files are saved.
  5. git restore –staged <(file-name)> : let’s assume, you don’t want to save the staged files or added to the stage by mistake then you use the restore command to unstage the files.
  6. git log : shows all commits made in the repo along with commit id, author name, date, time, and commit message.
  7. git reset <(past commit id)> : this will unstage the commit, meaning taking the repo back to the commit id mentioned. Now, do git log, it shows the repo that is reset to mentioned commit. If you want to undo it, you can do git reset, it will reset the commit. Now again, do git log and check changes.
  8. git stash: with reset, we are moving all commits from the staged area to the unstaged area (opposite of add command). But let’s say, you don’t want to save changes right now, use git stash. Stash saves your uncommitted changes for later use(somewhere else), meanwhile you are free to make any new changes, then come back and re-apply stash. Note: With the reset, you are moving commits from the staged to the unstaged area. With stash, you are moving changes from staged to some area(working directory).
  9. git stash pop: to save the changes which we didn’t save(saved it in some other area) we use stash pop. Moves all unstaged files to the staged area(like add).
  10. git stash clear: sends out all unstaged files.
  11. git branch <(feature-name)>: This creates a new branch called feature-name.
  12. git branch -d <(feature-name)>: This deletes a specific branch called feature-name, provided it doesn’t have any unmerged changes.
  13. git branch -m <(feature-name)>: This renames the current branch to feature-name.
  14. git checkout <(feature-name)>: It switches to a branch called feature-name, and commits will be added to it.
  15. git checkout -b <(feature-name)>: It creates a new branch and switches to it.
  16. git merge <(feature-name)>: It merges the feature-name branch with the current branch. It happens via pull request.

GitHub:

GitHub is a cloud-based distributed version control service. GitHub allows us to host our local git repositories so that you can share them with other developers. After the local repository is created with git, the next step is to create a remote repository to which the local repository can connect, this is done using git-hub. Whenever you work on a new feature in your repository, you create a separate branch.

You can never push to the upstream URL(the URL from where you forked your project).

Pull Request: It is a request to accept changes made to the code of the remote repository. Whenever you want to make a pull request, create a new branch and make a pull request. Never commit to the main branch.

Merge Conflicts: When developers modify the same line in a different way in a file, git is confused, as it cannot decide which modification to keep so fails to merge. This is called merge conflict.

GitHub commands for CLI:

1_tjrF1ff5UjVNclwwe_GREg.png

  1. git remote add origin <(url)>: When you create a remote repository in git-hub, it presents an HTTP link, which is required as a part of the git remote add origin command. You will not get any feedback on the terminal window.
  2. git remote -V: gives all the URLs attached to the current folder.
  3. git push origin master: git push is used to send local repo content to the remote repo. It pushes the commits from the local master branch to the remote origin branch.
  4. git push origin –delete : to delete a remote branch called feature-name.
  5. git clone <(url)>: it downloads the remote repo (of the url) into your local repo so that you can make changes to it.
  6. git fetch --all: it downloads all the branches from the remote repo into your local repo but it does not update the local repo.
  7. git pull upstream main: does the same thing as fetch internally,( it downloads from a forked project into the main project) but it updates the local repo.
  8. git rebase -i <(commit no.)>: all the commits above the will be picked or squashed into one commit.
  9. git merge --abort: it cancels the merge that had merge conflicts.