Skip to content

Instantly share code, notes, and snippets.

@noelmathewisaac
Last active April 12, 2022 04:28
Show Gist options
  • Save noelmathewisaac/aa3977a823b1147555be2dcc69de8a79 to your computer and use it in GitHub Desktop.
Save noelmathewisaac/aa3977a823b1147555be2dcc69de8a79 to your computer and use it in GitHub Desktop.
Git Commands

Git Commands

Reference for some common git commands

Initial Commit

  1. Initialise a new repo (only done once at the beginning of the project)
    git init
  2. Stage changes
    git add .
  3. Commit changes
    git commit -m "Initial commit"
  4. Add remote for remote repository
    git remote add origin <github_repo_url>
  5. Push to remote repo
    git push origin master

Make changes to code/Add new feature...

  1. Create new Branch and make changes
    git checkout -b <new-branch-name>
  2. Stage changes
    git add .
  3. Commit changes
    git commit -m "Initial commit"
  4. Pull code from github and merge with local code
    git pull origin master
  5. Merge new-branch to master
    git merge <new-branch-name> git merge --no-ff branch-name # Merge with no fast-forward
  6. Add remote for remote repository
    git remote add origin <github_repo_url>
  7. Push new-brach to github git push origin <new-branch-name> git push origin HEAD #Shortcut to push current branch wiithout specifying name
  8. Go to github and open a new pull request to merge with the master branch.
    • Even while working on an individual project, it is useful to open a PR, as github can perform automated checks on the code before approving the PR.

Branching

  • Switch to existing branch
    git checkout branch_name
  • Create new branch (relative to current branch) and switch to it
    git checkout -b branch_name
  • Rename current branch to new_name
    git branch -m new_name
  • Delete current branch
    git branch -d branch_name

Tagging

  • Create a tag for the current commit a tag number and message
    git tag -a v1.4 -m "my version 1.4"
  • Create a tag for an old commit/Add additional tag for old tag
    git tag -a v1.2 <old_tag/hash> -m "Message here"
  • Push tags to remote repo
    git push origin --tags
  • Delete local tag
    git tag -d <tag_name>
  • Delete remote tag
    git push --delete origin <tag_name>

Misc

  • Make git automatically convert line endings correctly for your platform. This can be used to prevent uncessary whitespace additons to commits when working on different OSs.
    git config --global core.autocrlf true

Git LFS

When using git lfs, if there is a previously failed commit it should be reset, if there is an error even after tracking the file. Sometimes a history of a failed push can cause issues"

Git LFS is used to track and commit large files. The steps are:

  1. Track a File
  • Track a single file git lfs track "filename"

  • Track a whole folder git lfs track "Folder/**"

  1. Add the .gitattributes file
    git add .gitattributes

  2. Add the files, commit and push

  • View tracked files git lfs ls-files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment