GIT and Github

How to direct connect to github repo without cloning ?

  • Initialize a Local Repository (if you haven’t already): git init
  • Add the Remote Repository: git remote add origin <repo-url>
  • Verify the Remote Repository: git remote -v

To get configuration of git:- git config –list

Create git repo: git init <repo-name>

Local area–> stage area(git add)–> commit area (git commit)

git add . –> it will add all the untrack file and add at a time

To check the file which is already committed –> git ls –files

To check the last or recent commit: git show

To check details of commit : git show <commit-id>:filename

To check recent 10 commit: git log

To check all the commit : git log –all

Delete the branch in local: git branch -d <branch-name>

Delete remote branch: git push origin –delete <branch name>

git reset

We make changes in the file the add the file and the file is in staging area, now we want to make some more changes in that file: git reset HEAD <file-name>, now the file is untrack

We make changes in the file the add the file and the file is in staging area and commited, now we want to make some more changes in that file: git reset HEAD ~1, now the file is untrack

When we make the changes in the file and we are added now we want to undo the changes: git checkout <file-name> or if we want to Revert to a Specific Commit: git checkout <commit-hash>

When we committed the file and want to revert the changes: git revert <commit-id>

When we are adding new file in status it show untracked but if we modify existing file it will show modified

Delete a file in git: git rm <file-name>, if want back the delete file we can revert it by commit id

Discard Local Changes in a File: git restore <file-name>

Unstage Changes in the Staging Area: git restore --staged <file-name>

Git Ignore

In Git, .gitignore is a file where you can specify patterns for files or directories that you want Git to ignore. This helps keep unwanted files out of your repository, such as temporary files, build artifacts, or configuration files specific to your local environment.

How to overcome conflicts in Git?

  • Conflicts occur when multiple changes are made to the same line(s) or section(s) of a file.
  • Edit the file to keep the desired code. Remove the conflict markers (<<<<<<<, =======, and >>>>>>>).

If there is suddenly the file is deleted in git how do you get it back?

If you want to restore deleted files and directories without overwriting the working tree, then you can use the git restore command. This command restores files and directories deleted from the staging area or the working tree.

What are git merge and git stash Memory?

git merge is used to combine changes from one branch into another. It allows you to integrate the histories of different branches, usually by merging a feature branch back into the main branch (like main or master) after development is complete. There are two common types of merges:

git stash temporarily saves changes in your working directory without committing them. This is useful if you need to switch branches or work on something else but don’t want to commit your in-progress changes yet. git stash allows you to save it in “stash memory” these changes, clearing your working directory, and later “apply” or “pop” them back when you’re ready.

Git pull and git fetch

  • Use git fetch when you want to review changes before integrating them.
  • git pull when you are confident and ready to update your branch with remote changes.

GIt Marge and git rebase

  • GIt Marge: Creates a new merge commit that combines the histories of the branches. it will create a new commit id for the marge
  • git rebase: Git Rebase is a command used to move or combine a sequence of commits to a new base commit. It helps you put your changes on top of a different commit, making the history cleaner and more straightforward without creating merge commits. Unlike git merge, which combines branches and adds a merge commit, rebase rewrites history by applying your changes directly to the target branch (usually master or main). This keeps the history neat and easier to understand.

Git stash

The git stash command in Git is used to temporarily save changes in your working directory that you do not want to commit immediately but also do not want to lose. It allows you to “stash” these changes away and revert to a clean working directory, so you can work on something else or switch branches.

Suppose i write some code and currently don’t want to commit that code or also delete that code, for that we can use git stash so it will save it in another folder and working directory will clean and we can use it in future

git cherry-pick

The git cherry-pick command is used to apply a specific commit from one branch to another. This is particularly helpful when you want to selectively integrate changes without merging entire branches.

git cherry-pick <commit_hash>

How do you undo the last commit in Git?

Undo the Last Commit but Keep Changes in the Working Directory: git reset –soft HEAD~1

Lets say your organization has github and bitbucket to store code, you have cloned a repo onto your local and changed directory name. after some days one of your team members asks you to share clone link, how would you provide the same?

Find the Remote URL: You can check the remote repository’s URL by running the following command in your local repository:

git remote -v

2. We can check the .git folder, there all the details are there about the repo

How to delete branch in git ?

git branch -d <branch_name>

What are hooks in git?

  • Client-Side Hooks: These run on the user’s machine and are triggered by operations such as committing, merging, or checking out code.
  • Server-Side Hooks: These run on the remote repository server and are triggered by events like receiving a push or creating a branch.

How to stop direct commits to GitHub ?

How to Stop Direct Commits to GitHub? 🚫

To prevent direct commits to GitHub branches, especially main or develop, follow these methods:


1. Use Branch Protection Rules (Best Practice)

GitHub provides Branch Protection Rules to enforce policies.

🔹 Steps to Enable Protection:
1️⃣ Go to your GitHub repository.
2️⃣ Click on SettingsBranches.
3️⃣ Under “Branch protection rules”, click Add rule.
4️⃣ Enter the branch name (e.g., main or develop).
5️⃣ Enable:

  • Require pull request before merging
  • Restrict who can push to matching branches (select only maintainers)
  • Require status checks to pass before merging
    6️⃣ Click Save changes.

Result: No one can commit directly. All changes must go through a Pull Request (PR).


2. Remove Direct Push Access for Users

If you’re in a team, you can restrict push access.

🔹 Steps:

  • Go to SettingsManage Access.
  • Set write permissions only for specific users.
  • Remove push access for developers.

Leave a Comment