Skip to content

Using Git

0x0 edited this page Mar 5, 2018 · 5 revisions

What is git? Basically something to track your file changes. O fcourse it gives you much more than that, that is why nearly every software organisation large or small e.g. Google, Atlassian, Microsoft, Apple etc use it. Learn more here

There exists GUIs for using git e.g. Github desktop app, Git Kraken and Source tree however you will find that the command line is actually easier to use for basic commands and gives you more power. That doesn't mean the CLI and GUIs are mutually exclusive. I use both in my daily work.

Common git commands

  • Get the remote repo and set up remote tracking on origin/branch-name: git clone https://github.com/githubusername/repo-name.git
  • Add your changes to staging, this prepares yours changes to be saved to "history": git add file-name
  • Save your stages changes into "history": git commit -m "Helpful commit message that describes your changes"
  • Push your changes up to remote repo (The remote repo must be tracked): git push origin branch-name
  • Get changes from a remote tracking branch: git pull origin remote-branch-name

Life saving git commands also dangerous

  • Remove all changes in local branch and use remote branch instead git reset --hard origin/branch-name
  • Go back to a specific commit git reset --hard <sha1-commit-id>
  • Remove all changes from one branch and become another branch while maintaining history of old branch so other people who have that branch don't get messed up
git checkout good-branch
git merge --strategy=ours bad-branch # Creates a merge commit to store history of old branch
git checkout bad-branch
git merge good-branch
  • To remove commits even when you pushed remotely git push origin <your_branch_name>/HEAD --force you will need to be checked out on the specific commit or correct branch state you want to reset to. Only do this if others have not pulled/branched from the commit you need to change otherwise they will have to do some history rewriting.

References: https://stackoverflow.com/questions/2763006/make-the-current-git-branch-a-master-branch, https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head

The git cheat sheet

Git cheat sheet

Clone this wiki locally