Git cheat sheet

Git: distributed repository is the new centralize.

Common used git command. I keep it brief with no/minimum explanation of each command by hoping that those commands are self explanatory. I also organize by git trifecta of Add/Modify – Branch – Remote workflow.

Add, check and publish. The basic

  1. Starting
    • git init
    • git clone <repository-url>
  2. Adding/making changes
    • git add .
    • git add <path/file.name>
    • git commit -am “”
  3. Checking status
    • git status
    • git log —-all —-graph —-oneline
    • git log branch1..branch2
  4. Getting updates
    • git fetch Fetches latest changes from origin
    • git pull Fetches latest changes from origin and merge
  5. Publishing
    • git push Push changes to origin from current
    • git push <origin> <branch> Push changes to origin from branch

 

Modify, fixing mistakes

  1. Revert commits
    • git revert HEAD
    • git revert <commit-ref>
  2. Revert/undo all changes
    • git checkout - <file>
  3. Fix last commits
    • git commit –amend [-m “updated message”]
  4. Reset modifications
    • git reset HEAD <file>
  5. Diff-erence
    • git diff
    • git diff $id1 $id2 Diff between 2 commits
  6. Resolving changes
    • git checkout –ours <path/file>
    • git checkout –theirs <path/file>
  7. Combine commits
    • git rebase -i HEAD~4

 

Branching

  1. Creating branch
    • git branch <branch name>
  2. Listing branch
    • git branch
    • git branch -a
    • git branch -v
  3. Switching branch
    • git checkout <branch-name>
    • git checkout -b <branch-name>
  4. Merging branch
    • git merge <branch-name>
    • git merge --squash <branch-name>
  5. Deleting branch
    • git branch -d <branch-name>

 

Remote

  1. Adding remote
    • git remote <remote-name> <remote-url>
  2. Listing remote
    • git remote -v
  3. Fetching/Pushing to/from remote
    • git fetch <remote-name>
    • git push <remote-name> <branch-name>
  4. Getting update without removing local
    • git stash
    • git pull
    • git stash pop

 

Also, use .gitignore file for ignore files or directories. For further reference for more detailed git usage, my go to is gitref.org.

Advertisements

Updating GitHub Fork

Let say you want to contribute to an interesting project on GitHub neovim/neovim, first we need to fork it to our GitHub accout ninovsnino/neovim. All this we need to do that in our own GitHub account.

I’m a vim guy and I really exited on the initiative to make a modern and extendable version of vim, it’s a good example on new and hot open source project on GitHub.

Register and list git alias

Now let’s open our Git Bash console to clone it in our local machine
git clone git@github.com:ninovsnino/neovim
cd neovim
git remote add upstream git@github.com:neovim/neovim

the last command will set the original project with an alias upstream.

We can verify where are origin and upstream pointing it to
$ git remote -v
# origin https://github.com/ninovsnino/neovim (fetch)
# origin https://github.com/ninovsnino/neovim (push)
# upstream https://github.com/neovim/neovim (fetch)
# upstream https://github.com/neovim/neovim (push)

Update local master

If your original public repo is a very popular project there’ll be a lot of updates until a point we couldn’t keep up with, one day you skip monitoring it will be one or more changes will get in.
Now it’s time to sync it,

$ git fetch upstream
remote: Counting objects: 462, done.
remote: Compressing objects: 100% (272/272), done.
remote: Total 462 (delta 259), reused 286 (delta 170)R
Receiving objects: 100% (462/462), 1.53 MiB | 72.00 KiB/s, done.
Resolving deltas: 100% (259/259), done.
From https://github.com/neovim/neovim
bbf6442..a86b1a1 master -> upstream/master

Now we get our master updated in local machine

$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Fast-forwarded master to upstream/master.

It will make our git history cleaner

Update our GitHub repository

What we have so far is our local machine, master is keep up with upstream, but we still didn’t touch origin, so we’ll update it as well.

$ git push origin master
Username for 'https://github.com': ninovsnino
Password for 'https://ninovsnino@github.com':
Counting objects: 629, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (183/183), done.
Writing objects: 100% (462/462), 191.74 KiB | 0 bytes/s, done.
Total 462 (delta 334), reused 387 (delta 259)
To https://github.com/ninovsnino/neovim
bbf6442..a86b1a1 master -> master

Happy contributing.