Links        1

Terms        1

Config Command        1

Repo Commands        1

Branch Commands        3

Stash Command        4

Flow        9

Links

Terms

Repository: Project or the folder/place where your project is kept

Github: a website to host your repositories online

Clone: Bring a repository that is posted somewhere like it up into a folder on your local machine

Add: check your files and changes and git

Commit: save your files in git

Push: upload git commits to a remote repository like Github

Pull: download changes from a remote repository to your local machine, opposite of push

Config Command

Commands

Description

git config --global --edit

To see the git config file

git config --global user.name “ENTER NAME”

To set git user name

git config --global user.email “ENTER EMAIL”

To set git user email

Repo Commands

Commands

Description

git init

Initialize git in the project directory

git status

Gives a list of files to be tracked

git add .

All the file changes needs to be tracked

  • Changes have been staged

git add index.html

Track the changes only in index.html file // track individual file

git commit -m “MESSAGE” -m “DESCRIPTION MESSAGE”

git commit -am “MESSEGE”

Files ready to be committed

-m = Add a message to your commit title

-m = add a message for your commit description

Store code locally // not live yet

-a = add files that are previously added

will not work for adding a newly created file

git branch -l

git branch -a

Show a list of branches in your working directory

git clone https://LINK

Cloning a repo

git push

git push origin master

Pushing code to your remote repo where your project is hosted

When you push changes for the first time

Origin = location of git repository

Master = branch that we want to push to

git ssh

Generating a new SSH key and adding it to the ssh-agent - GitHub Docs

git init

git remote add origin HTTP_LINK

git push origin master

git push -u origin master

To initialize a git for a new repository

  • Use it without having repo first
  • Create remote repo

Add origin of your 2nd repo

To push changes to the origin master

-u = set upstream // so you don’t have to write origin master everytime you need to push

git pull

To pull changes from your branch you are currently in

git merge master

Get latest from master in your branch

git reset

  • undo your add // When you want to reset your tracked changes(git add)
  • When you git add . → but now you don’t want to add those changes anymore // undo add changes

git reset HEAD~1

git revert head~2

Undo the commit // When you want to reset your last commit changes(git commit -m)

  • Changes will still be there
  • But they are not saved in git or staged with git any longer                        

git reset --hard COMMIT_HASH

Remove all the changes not just be unstaged but completely removed

git fetch 

git pull

tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository.

Pull will brings the copy of the remote directory changes into the local repository.

git cherry-pick COMMIT_ID

Cherry pick commit

  • Let’s say you have a bug in master branch file and you want to only target that file and bring down that file to your local branch to fix issue
  • In you’re local branch use this command to get that file and fix the bug                                

git rebase -i HEAD~{N}

Squash last N commits into a single commit

Branch Commands

git branch

To see which branch you’re currently in

git branch BRANCH_NAME

git switch BRANCH_NAME = git checkout BRANCH_NAME

Create a new branch

Switch to newly created branch

git checkout NEW_BRANCH_NAME

git checkout -b NEW_BRANCH_NAME

Switch between branches

-b = create a new branch from which branch you are in

It will create a new branch and let you in the new branch

git diff BRANCH_NAME

Shows the file changes/difference between master and your working branch

git branch -d BRANCH_NAME

Delete a branch

  • Use this command outside the branch you’re trying to delete                        

Stash Command

Git Tutorial: Using the Stash Command

git stash save “MESSAGE”

Save changes in a temporarily place. You’re changes are temporarily gone

Usecase

  1. When you have changes you’re not ready to commit
  2. You need to switch branches
  3. You want to revert back temporarily to where you stared
  4. You don’t know what to do with your changes        

git stash list

To list your stashes

  • List stash ID

git stash apply STASH_ID

To bring back your stashed changes

git stash pop

It gives our changes back and deletes the stash

Usecase:

When you add changes on your master branch instead of your working branch. And you want to have all those changes in your working branch

  • Git stash save “M” // stash can carry over branch to branch
  • Git checkout YOUR_BRANCH
  • Git stash pop
  • Git add, git commit

git stash clear

Remove all the stashes

Agenda

Repository

Branch

Merge

Commit: is to track your changes. When you make changes and commit, it detects the changes you make, when you make another commit it tracks that commit too and when you want to know your 1st commit change you will get the details with the commit id  

Collaborate remote repository

Github hosts remote repository

 

Commands  

  1. Git init // to initialize local git repo
  1. Git status // to check if there has been any commit to our local repo/find untracked files
  2. Git config --global user.name “Gautam”   // to register you name email with your local GIT repo
  1. Git config --globle user.email “gautampambhar@gmail.com

Now you have a folder and your application files in your folder. You have initialized git repo.

You need to add files to init repo.

  1. Git add -A //add all the file to init repo

If you change a file content and you directly commit to the repo it won’t work

You need to add file first and then commit it when you make any change

  1. Git commit -m “first commit”  //snapshot stores into git repo
  1. Git commit -a -m “2nd commit”  // it adds the files and commit it both

  1. Git log // you get all the details about all commit you make
  1. git log --format=fuller
  2. git log --oneline
  3. git log --oneline --graph // it addes * to left

Branch & log

  1. Git branch newbranch  // add new branch
  2. Git branch // to see which branch are you on
  3. Git checkout newbranch // to go to whichever branch you want
  4. Git log // it only going to show commit or file in which branch you are in( suppose you have 2 branch, and currently you’re in master branch it only going to show you files and commit that are associated with master branch)
  5. Git branch --all  // to see all the branch
  6. Git log --decorate --graph --all // it shows all the git files and commit associated with all the branch

  1. Git reset --hard // to make changes deleted and revert to the old version of the file
  2. Git diff // it tells what content you have removed and shows the new content that you have replaced with  → to check the previous commit

Merge branch  

Merging newbranch to the master branch

  1. Git diff master newbranch // see the difference between 2 branch
  2. Git merge newbranch // (when you are at master branch and you want to merge all the newbranch file into master = change you will ses is in master branch you will have all the file that is associated with new branch )

GIT Stashing

  1. Git stash

working with remote

  1. git remote add origin “link-http”

In order to connect to SSH you have to generate public SSH key and add that key into your github account and then you can start making pushing changes

  1. Git clone link-http
  2. Git origin // will give you origin (what is origin = )

Flow 

  1. Install Git: First, you'll need to ensure you have Git installed on your local machine. You can download it from Git's official site.
  2. Set up an Azure Repo: Go to Azure DevOps and log in with your account. Create a new project, then navigate to ‘Repos’. Create a new repository.
  3. Initialize a Git repository on your local machine:
  1. mkdir my-git-project     # Creates a new folder
  2. cd my-git-project         # Navigates into the newly created folder
  3. git init                        # This will create a new Git repository in your my-git-project folder.
  1. Create a new file and make some changes:
  1. echo "Hello World" >> README.md     # Creates a new file named README.md with the content "Hello World"
  1. Stage and commit your changes:
  1. git add .   # Adds all changes to the staging area
  2. git commit -m "Initial commit"   # Commits changes to the repository with a message
  1. Link your local repository to your Azure Repo:
  1. In Azure DevOps, within your repo, select 'Clone' at the top-right corner. A window should pop up with the URL you need to copy.
  2. You can link your local repository to your Azure Repo with the following command (replace <URL> with the URL you just copied):
  3. git remote add origin <URL>   # Set a new remote repository // After running this command, your local repository has a reference to your remote repository called "origin".
  4. This "origin" is a naming convention in Git that refers to the default remote repository your local repository is connected to. Once this is set up, pushing your changes to the remote repository is as simple as running git push origin master
  1. Push commits to the remote repo on Azure:
  1. git push -u origin master   // This means "push the commits in the local repository's master branch to the remote repository known as origin".

Error solved: https://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to

git reset --mixed origin/master

git add .

git commit -m "This is a new commit for what I originally planned to be amended"

git push origin master