Avatar
Fernando Vásquez is an electronics engineer and software developer currently based in the world. He occasionally blogs about Python and Android programming.

Git Basic Commands

  • First, create a SSH key with ssh-keygen
ssh-keygen -t ed25519 -C "[email protected]"
  • Start using git with the current directory
git init
git config --global user.name "User Name"
git config --global user.email "[email protected]"
git config --global user.signingkey __KEYID__

git config --local -e
git config --local core.editor "nvim"
git config --local commit.gpgsign true
  • Push changes to remote repo “origin” from local branch “master”
git push -u origin master
  • Pull (checkout) to local branch “master” from remote repo “origin”
git pull origin master
  • Cloning a git repo, remote or local
git clone [email protected]:__user__/__repoName__.git
git clone __REPO__ __DIR__
  • Do things… changes new files…
git add ejemplo.txt         #- Adding to the local repo
git commit -m "now in local repo" #  - Push changes into the remote repo
git push __repo__
  • You do not need to specify the local branch if you are already sitting in it.
git pull
  • Change remote origin
git remote set-url origin __repo__
git remote set-url origin __repo__
  • Clone specific branch
git clone <url> --branch <branch> --single-branch [<folder>]
git clone user@git-server:project_name.git -b branch_name /some/folder
git clone user@git-server:project_name.git -b branch_name --single-branch /some/folder
  • Once you have created of pulled a git repository, the basic things you can do:
git status       # Checking the status of your repository
git ls-files     # Seeing what files have been committed
git rm <file>    # Scheduling deletion of a file ``````
git log          # Viewing a log of your commits ( -v: pagination )
git log --stat
git changes      # Visualizing
git --all
  • Creating a new tag and pushing it to the remote branch
git tag "v1.3"
git push --tags
  • revert to specific commit (commits you did after will no longer be in the history)
git log -10                 # search the hash where you want to go
git reset --hard __HASH__   # hash sample: 85102eac830990afa60136419bd09ffeea7eb646
git reset --hard HEAD       # remove all you uncommited changes
git reset --soft HEAD       # stage your changes
git reset --mixed HEAD      # keep changes as modified
  • other
git clone -b __branch__ __repo__
git clone -b __branch__ --single-branch __repo__
  • small change to last commit
    git add .
    git commit --amend
    
  • merge different remotes
    git remote add openshift __repo__
    git fetch openshift
    git merge openshift/master
    git commit
    
  • Config Kdiff for git
git config --global --add merge.tool kdiff3
git config --global --add mergetool.kdiff3.path "D:/bin/kdiff3/kdiff3.exe"
git config --global --add mergetool.kdiff3.trustExitCode false
git config --global --add difftool.prompt false

Reverting changes 🤦

  • revert a commit
git commit -m "Something terribly"
git reset --soft HEAD~           # HEAD~ is the parent of HEAD
git add
git commit -c ORIG_HEAD
  • While working on “feature” branch, suddenly need to go work on a hotfix

git commit --all --message "Backup my feature work"
git checkout -b hotfix master   # create hotfix branch from master
git checkout feature            # go back to feature
git reset head^
  • save your changes without creating a commit

git stash               # save your changes
git checkout branch-B   # Then switch to your other branch
git commit --message "finished with branch-B"
git checkout branch-A   # When you're ready, go back to your original branch
git stash pop           # and unstash your changes:
  • git reset
git reset --hard
git checkout HEAD -- <path>

https://ohshitgit.com/

Ignoring files .gitignore

  • Create a file in the root directory called .gitignore
  • then add ignore restrictions like: *.log db/schema.rb
  • If you want a log/ directory, but want to ignore all the files in it: log/*
  • Then add an empty .gitignore in the empty directory: touch log/.gitignore
  • Lines beginning with ! are exceptions !.gitignore

Git Branching Cheat Sheet

git branch          # show current brannch
git show-branch -a  # list all branches
  • Create and switch to a branch
git branch iss53        # create new branch
git checkout iss53      # move to new branch
git checkout -b iss53   # in one step
  • Merging branch into master
git checkout master
git pull origin master  # in the case you want to update master
git merge iss53

all tags