Tuesday, March 23, 2021

What is Git | git comands

What is Git


Execution Security Flexibility Version control with Git



By a wide margin, the most generally utilized present day form control framework on the planet today is Git. Git is a full grown, effectively kept up with open source project initially created in 2005 by Linus Torvalds, the well known maker of the Linux working framework piece. A stunning number of programming projects depend on Git for form control, including business projects just as open source. Designers who have worked with Git are all around addressed in the pool of accessible programming improvement ability and it functions admirably on a wide scope of working frameworks and IDEs (Integrated Development Environments).

Git Advantages


  • Fast.
  • Does not lose data (has proper fsck, checksums, every developer has full copy of the repository, well written code).
  • Distributed (meaning, handles branching and merging very well).
  • Ability to create commits that do not match the collection of all modified files (“index” or “staging area”).
  • Gives you lots of freedom.


Update your last pushed commit "firstly check last commit"

 

$ git commit --amend -m "New commit message."

$ git push origin --force branch_name


Update vim editor for git (need to add below syntax)

$ vim ~/.gitconfig

[user]
    name = Alon
    email = alonmusk@teslamoters.com
[core]
    editor = vim
    excludesfile = /home/mypath/.gitignore_global
[color]
  ui = auto
  # other settings here


Changing an Older or Multiple Commits

 

Note :- To change the most recent commit message, use the git commit --amend command.
        To change an older or multiple commit messages, use git rebase -i HEAD~N 

Step 1)  Type git rebase -i HEAD~N, where N is the number of commits to perform a rebase on. For example,
         if you want to change the 4th and the 5th latest commits you would type:

$ git rebase -i HEAD~5

The command will display the latest X commits in your default text editor :

pick 43f8707f9 fix: update dependency json5 to ^2.1.1
pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2

Step 2) Move to the lines of the commit message you want to change and replace pick with reword:

reword 43f8707f9 fix: update dependency json5 to ^2.1.1
reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2

Step 3) Save the changes and close the editor.

Step 4) Force push the changes to the remote repository:

git push origin --force branch_name

 

why .gitignore is not working

 Note :- Remember to commit everything you've changed before you do this!

$ git rm -rf --cached .
$ git add .
$ git commit -m "update .gitignore"
$ git push origin branch_name

Renaming Git Branch 

Step 1) Checkout the old branch 

$ git checkout old_name

Step 2) Rename the local branch by typing

$ git branch -m new_name

Step 3) Push the <new_name> local branch

$ git push origin -u new_name

Step 4) Delete old branch 

$ git push origin --delete old_name 

 

Delete Last Commit and last updated files

$ git reset --hard HEAD~1

$ git push -f origin master

 

 Scenario - 

  • If you forget to checkout the respected branch before adding and commiting the files.
  • and now wants to push the code on respected branch but unfortunately appearing below output.

Output be like :

Demo-User:~/Demo-Folder$ git push origin branch1

error: src refspec branch1 does not match any.
error: failed to push some refs to 'git@ssh.dev.azure.com:v3/tech2towards2021/Demo/wordpress'


Delete the most recent commit by the soft way, it will keep the work you've done:

$ git reset --soft HEAD~1

Delete the most recent commit by the hard way, it will destroy all the work you've done:

Don't use this Danger command in production, if you use than make backup before doing anything.  

$ git reset --hard HEAD~1



Command line instructions



=======================
Create An Empty Branch:
=======================


In Git, an "orphan" branch is a branch that has no parent commit. This means it doesn't share history with any other branch. In other words, it's a branch that starts completely fresh, with no commit history from any existing branch.

git switch --orphan new_branch
git commit --allow-empty -m "Initial commit on orphan branch"
git push -u origin new_branch


=======================
Git global setup
=======================


git config --global user.name "Administrator"
git config --global user.email "admin@example.com"
git config -l


=======================
Create a new repository
=======================


git clone git@github.com:demo32112/demo.git
cd flexsin-sys-inventory
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

=======================
Existing folder
=======================


cd existing_folder
git init
git remote add origin git@github.com:demo32112/demo.git
git add .
git commit -m "Initial commit"
git push -u origin master

=======================
Existing Git repository
=======================


cd existing_repo
git remote rename origin old-origin
git remote add origin git@github.com:demo32112/demo.git
git push -u origin --all
git push -u origin --tags


What is the difference between a Git pull and a Git fetch?


A Git pull is a command that fetches changes from a remote repository and merges them into the local repository with single command, It is a combination of the Git fetch and Git merge commands.

On the other hand A Git fetch only downloads changes from a remote repository and does not merge them into the local repository, after that you have to check the version status of between your local files & Remote files via diff command and then need to merge it.

Note: The best practice is used git fetch because here you can see the version change before downloading the files and In git pull you directly download the files without seeing the version status.

Example of git pull

git pull origin Branch_Name


Example of git fetch

git fetch origin Branch_Name
git diff origin/Branch_Name
git merge origin/Branch_Name





No comments:

Post a Comment

testing