Git CheatSheet

Table of Contents


Git is a distributed version control system that is widely used for software development and collaboration. It allows us to keep track of the changes we make to our code and collaborate with others on a project. Git maintains a history of the changes made to a project, allowing us to revert to previous versions if needed.

Here are some basic Git commands and concepts:

  • git init: Initialize a Git repository in the current directory.
  • git clone: Clone an existing Git repository from a remote location to your local machine.
  • git add: Stage changes for commit.
  • git commit: Commit the staged changes to the repository’s history.
  • git push: Push the committed changes to a remote repository.
  • git pull: Fetch and merge changes from a remote repository.
  • git branch: List, create, or delete branches.
  • git checkout: Switch to a different branch or revert to a specific commit.
  • git merge: Merge changes from one branch into another.
  • git stash: Temporarily save changes that have not been committed.
  • git log: View the history of commits in a repository.

These are just a few of the basic Git commands. Git is a very powerful tool, and there are many more commands and features available.

I am jotting down some notes primarily for my own reference, but I hope they may prove beneficial to others as well.

Some important notes

  1. We can run everytime the git add <fileName> command without commiting to github. So, that in case if file deletes or something bad happends then we can checkout latest verison of file using git checkout <fileName>.

Grab a file from different branch

  • We are in master branch and want to commit a file app.js from experiment branch. For this we have to do following:
git checkout master # Just to make sure we are on the correct branch master.
git checkout experiment -- app.js # then copy the version of app.js
                                  # from branch "experiment"
                                  # you should give the relative path of app.js
                                  # now, its expecting that app.js should be in the
                                  # same path where you are now.

Reference: https://stackoverflow.com/a/2364223/2302094

Check difference between two commits

  • Using Sublime Merge one can check the difference using CMD/CTRL + click on the two commits.

GIT: Readme tricks

GitHub readme follows the Markdown Language. Apart from markdown formate there is some nice tricks for adding github link and others. Below I am summarizing some of them.

  1. Auto Linking References & URLs in GitHub: link

GIT reflog

Reference:

  1. https://help.github.com/en/articles/using-git-rebase-on-the-command-line
  2. https://dev.to/blakedeboer/beginners-guide-to-interactive-rebasing-1ob

GIT amend

Git Stash

  1. To create stach

    git stash
    
  2. To list stash

    git stash list
    
  3. To see stash

    git stash show
    
  4. To apply stash

    git stash apply
    

    or

    git stash pop
    

    The above command will apply the latest stash. But, if there are many stash and you want to get stash number 3 which is named stash@{3}, then do:

    git stash apply stash@{3}
    
  5. To remove a stash

    git stash drop stash@{0}
    
  6. Un-apply a stash:

    git stash show -p stash@{0} | git apply -R
    

Check difference between two branches

git diff <branch-1> <branch-2>

git rebase

git rebase -i HEAD~3  # to squash last three commit
git push <remote> <branch> -f.
# use -f to force push. If you committed last three
# commits then you can't push directly. And if you
# surely know what you are doing then you can use `-f`.

Undoing git rebase

  • Undoing a git rebase: link

Git branching model

Best reference that I found is in one of blog by Vincent Driessen: https://nvie.com/posts/a-successful-git-branching-model/

Need to read

  1. https://www.freecodecamp.org/news/learn-the-basics-of-git-in-under-10-minutes-da548267cc91/
  2. https://www.freecodecamp.org/news/how-not-to-be-afraid-of-git-anymore-fe1da7415286/

References

  1. http://git.github.io/git-reference/index.html

Git Patch

  1. Create a patch:

    git diff > mypatch.patch
    
  2. Apply a patch:

    git apply mypatch.patch
    

    or,

    To see whats going on behind the scene use option -v

    git apply -v mypatch.patch
    
  3. Alternative way to git apply:

    patch -p1 < mypatch.patch
    

4.

Important Links

  1. Git summary of commands: my ppt link, main ppt link

  2. Git stash: link

  3. Debugging with git: link
  4. Git tutorial: http://rogerdudler.github.io/git-guide/

GitHub Issues

Git: cannot checkout branch

Error msg: error: pathspec '…' did not match any file(s) known to git

git remote update
git fetch
git checkout --track origin/<BRANCH-NAME>

Reference: https://stackoverflow.com/a/32578285/2302094

Patch does not apply

$ git apply example.patch
error: patch failed: includes/common.inc:626
error: includes/common.inc: patch does not apply

Git couldn’t apply the changes in the patch because it wasn’t able to find the line(s) of code in question; they must have been changed or removed by another commit. Try these things:

  • Make sure the patch hasn’t already been applied. Look for it in git log or simply examine the code to see if the change(s) are already present. If they are, you’re done. If they aren’t or if only some of them are, try something else:

  • Use patch -p1 < filename.patch. Whereas git apply altogether rejects a patch with any errors, patch -p1 works hunk by hunk, applying as many individual changes as it can. It backs up each file as filename.ext.orig before modifying it and saves rejected hunks in filename.ext.rej. Discard the .orig files and manually apply the changes left in the .rej. This is an easy strategy for small patches.

Reference: https://www.drupal.org/node/1129120




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • GitLab workflow for CMS-AN
  • Vi-Editor
  • Condor Jobs
  • XDAQ Basics
  • EOS uses
  • ROOT CheatSheet
  • sed command
  • Mac Settings
  • awk command
  • Terminal multiplexer (screen/tmux)
  • PPT with plots in one click