Git CLI reference

A work in progress intended for my own reference, but if this proves helpful to others as well that’s great.

Create a repo

Clone a repo

# clone the repo as a new folder in the current directory
$ git clone <url of repo>Code language: PHP (php)

Create a repo

# create git repo
$ git initCode language: PHP (php)

Don’t forget to setup your .gitignore file before adding all the files.

# add all files to the repo
$ git add .
# add a single file
$ git add <file name>Code language: PHP (php)

Working with a repo

Commit, push and pull

# commit local changes
$ git commit -m "comment goes here"
# push changes to remote repo
$ git push
# pull changes from remote repo
$ git pullCode language: PHP (php)

Roll back commits

# HEAD~number of commits
# soft keeps changes but drops commits
git reset --soft HEAD~1
# hard drops commits and any changes
git reset —hard HEAD~1Code language: PHP (php)

Remote Repo

List remote branches

list remote
$ git branch -r
list local and remote
$ git branch -aCode language: PHP (php)

Pull remote branch

if the branch exist on server but not locally
$ git switch <NAME OF BRANCH>Code language: HTML, XML (xml)
# or
git fetch origin; git checkout --track origin/branchNameCode language: PHP (php)

Push remote branch

git push -u origin branchName

Delete remote branch

git push origin --delete branchNameCode language: JavaScript (javascript)

Other helpful stuff

Configure profile

Setting this up will tell people that you did your work.

# omit your name/email and git will return the current setting
$ git config --global "Clint Vidler"
$ git config --global address@example.comCode language: PHP (php)

Get help

# learn about a command
$ git help
$ git help [command]
# enter 'q' to quit the help documentCode language: PHP (php)

Get info about repo

# obtain the status of the local repo
$ git status
# for just a summary
$ git status -sCode language: PHP (php)
# obtain a log of commits
$ git logCode language: PHP (php)
# show a list of all tracked files and folders
git ls-tree -r main --name-onlyCode language: PHP (php)
# show files that were changed in the last commit
git diff --name-only HEAD~0 HEAD~1Code language: PHP (php)

GUI displaying changes

$ gitk

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.