Post

Git Commands - CMDsheet

Git Common Commands

git --versionCheck the git version
git initTo initialize a directory
git remote add origin "https://<path-to-git-repository>"To add a remote directory to the newly initialized folder
git statusTo check if there are any uncommitted changes
git pull masterTo pull all the files from the remote repository to the local repository
git add filename OR git add .To add all or selected uncommitted changes
git commit -m “commit msg”To commit with uncommitted changes
git push -u “url” master OR git push -u origin masterTo push the changes to the master branch on the repo
git clone https://<path-to-git-repository>To clone a directory from git providers
git clone https://username:password@github.com/username/repository.gitTo clone the directory with username/password
git fetchTo fetch the git repository to local

Branches

git branchTo get the current git branch
git branch -rTo list all branches
git checkout -b branch_pathTo create to a new branch
git branch -D <local-branch>To remove branch from local directory
git push origin [name_of_your_new_branch]To push the newly created branch to origin
git checkout -b branch_pathTo create to a new branch
git checkout -b new_branch existing_branchTo create to a branch from an existing branch

Git Global Config

git config --global --listList all settings in the global Git configuration
git config --global user.name "Your Name"Set your name in the global Git configuration
git config --global user.email "your.email@example.com"Set your email in the global Git configuration
git config --global core.editor "code --wait"Set Visual Studio Code as your default editor for Git
git config --global --editOpen the global configuration file in a text editor for manual editing
git config --global --unset user.nameRemove the user.name setting from the global Git configuration
git config --global --unset-all user.nameRemove all user.name settings from the global Git configuration
git config --global --get user.nameGet the user.name setting from the global Git configuration
git config --global --get-all user.nameGet all user.name settings from the global Git configuration
git config --global --get-regexp userGet all settings in the global Git configuration that match the regular expression user
git config --global --rename-section user.email user.mailRename the section user.email to user.mail in the global Git configuration
git config --global --remove-section user.emailRemove the section user.email from the global Git configuration
git config --global pager.branch falseTo disable the displaying output using pager

Git Push

git push <target-remote>To push local changes to a specific remote repo
git push <target-remote> <source-remote>/<branch>:<branch>To push code from source repo to target repo

Cloning a Git Repository:

  • To pull contents from a repository(github) and upload to another(gitlab):
  1. Clone the repository to your local workspace and verify the files are download
    1
    
    git clone https://github.com/in28minutes/devops-master-class.git
    
  2. To clone the directory with username/password:
    1
    2
    
    git clone https://username@github.com/username/repository.git
    git clone https://username:password@github.com/username/repository.git
    
  3. Change the directory to the folder and remove the origin
    • To check the origin
      1
      
      git remote -v
      
    • To remove the origin
      1
      
      git remote rm origin
      
  4. Add the new repository as origin to the cloned folder and push it to the master branch
    1
    
    git remote add origin https://gitlab.com/sakharamgit/devops.git
    
  5. Push the changes to the destination repository:
    1
    
    git push origin master  
    
This post is licensed under CC BY 4.0 by the author.