git

Git is a distributed version control software system that is capable of managing versions of source code or data. It is often used to control source code by programmers who are developing software collaboratively.

git commands

  • command to add a modified file or directory

    git add filename-or-directry-name
    
  • Command to add all modified file

    git add -u
    
  • Command to commit

    git commit -s
    
  • Command to checkout multiple modified file

    git status | grep modified | sed 's/^.*modified:   //' | xargs git checkout
    

    Note: check space between modified: and //

  • Command to remove white space and cleanfile in all modified file

    git status | grep modified | sed 's/^.*modified:   //' | xargs ./cleanfile
    

    Note: cleanfile is a script present in current folder.

  • Command to push in specific brance with review

    git push origin HEAD:refs/for/dev1.0
    

    Note: origin means head(main) branch and dev1.0 is current brench where we want to push

  • Command to checkout specific commit

    git checkout commit-hash
    
  • Command to create new branch

    git checkout  -b new_branch_name
    
  • To delete a git branch

    git branch -d branch_name
    
  • create git patch from commit

    git format-patch -n
    

    Note: where n is number of commit, each commit will generete a patch file

  • Apply a git patch generated by git format-patch

    git apply --stat a_file.patch
    git apply --check a_file.patch
    git am --signoff < a_file.patch
    
  • configure user information

    git config --global user.name "Your name"
    git config --global user.email "Your email id"
    git config --global branch.autosetuprebase always
    git config --global color.ui true
    git config --global color.status auto
    git config --global color.branch auto
    git config --global core.editor vim
    git config --global merge.tool vimdiff
    git config --list
    
  • To clone git repositery first time use

    git clone https://github.com/user/Repo-name
    
  • add new file

    • add new file

      git add <filename>
      
    • now check status of newly added file

      git status
      
    • then use commit to update added file using command

      git commit -m "your comment"
      

    Now the new file is ready to push, use above push command

  • delate file

    git rm filename
    
  • to undo the delate file

    • to find a deleted file and its commit

      git log --diff-filter=D --summary                  # all deleted files ever
      git log --diff-filter=D --summary ./               # all deleted files in cwd 
      git log --diff-filter=D --author=Batman --summary  # all files deleted by Batman
      
    • for undo

      git checkout <commit>~1 <filename>
      
  • to checkout only one file

    git checkout -- filename
    
  • to create patch or current changes

    git status > filename.patch
    
  • to apply patch

    git apply filename.patch
    
  • To modify the chanages in last commit

    git commit --amend
    

©2023-2024 rculock.com