Git

From Things and Stuff Wiki
Revision as of 08:16, 1 April 2024 by Milk (talk | contribs) (→‎Articles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


General

See also Vim#Git

  • Git - a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.


  • https://en.wikipedia.org/wiki/Git - a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows (thousands of parallel branches running on different computers).


  • Git - Book - The entire Pro Git book, written by Scott Chacon and Ben Straub and published by Apress, is available here. All content is licensed under the Creative Commons Attribution Non Commercial Share Alike 3.0 license. Print versions of the book are available on Amazon.com.




Guides



Articles

Consider how a common Git workflow falls apart.

Create a branch off Master, do work, and merge it back into Master when you’re done

Most of the time this behaves as you expect because Master changed since you branched. Then one day you merge a feature branch into Master, but Master hasn’t diverged. Instead of creating a merge commit, Git points Master to the latest commit on the feature branch, or “fast forwards.” (Diagram)

Unfortunately, your feature branch contained checkpoint commits, frequent commits that back up your work but captures the code in an unstable state. Now these commits are indistinguishable from Master’s stable commits. You could easily roll back into a disaster.







Config

git config --global --edit
  open config file in editor
git config --global user.name "Your Name Here"
git config --global user.email "your_email@youremail.com"
git config color.ui true
  turns on colour for everything
git config format.pretty oneline
git config --global push.default matching
  (will be default soon)


git config --global help.autocorrect 10
git config --global credential.helper 'cache --timeout=3600'

The credential helper only works when you clone an HTTPS repo URL. If you use the SSH repo URL instead, SSH keys are used for authentication.

Aliases


Bare repo

To convert repo to bare repo; [13]

mv repo/.git repo.git
git --git-dir=repo.git config core.bare true
rm -rf repo


Commands

See Vim#Git

Clone

git clone git://github.com/SomeUser/SomeRepo.git
  # clone read-only repo

git clone git@github.com:UserName/OtherRepo.git
  # clone with SSH key

git clone https://github.com/UserName/OtherRepo
  # clone with github username

git clone --reference git git://github.com/git/git git2
  # clone local

Repo config

  • git config receive.denyCurrentBranch warn - allow pushing to a non-base repo
git repo-config core.sharedRepository true - allow access by multiple users


Status

git status
  # show repo status
git status -uno
  # show status without untracked file list

Describe

git describe
  # last tag plus number of releases from that tag plus abbreviated sha

git describe --abbrev=0 --tags
  # gets tag from current branch

Listing

git ls-files --modified
git ls-tree -r HEAD --name-only

Diff

See also Vim#Git

git diff
  # diff uncommitted changes
git diff --cached
  # diff changes in the staging area
git diff master <branch>
  # show difference between master and another branch
git diff shaOfCheckIn
  # show diff between sha and current
git show shaOfCheckIn
  # show diff of sha from previous
  # or
git diff shaOfCheckIn^ shaOfCheckIn
git diff shaOfCheckIn{^,}
git diff <revision_1>:<file_1> <revision_2>:<file_2>
  # diff between two files
git diff --dirstat=files --no-index dir1/ dir2/
  # diff between any two directory contents



Patches

git diff > change.patch
# create a patch

git diff > [description]-[issue-number]-[comment-number].patch
# create a Drupal patch

git apply -v <patch>
# apply patch. also;

curl https://github.com/github/jobs/pull/25.patch | git am

Adding files

git add .
  # add file to Index (staging)
git add *
  # add all (except those in .gitignore) files to Index


git add -i
  # interactive shell
git add -p
  # add hunks seperatly
### commonly used commands
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
### more advanced commands
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
git ls-files --modified | xargs git add
  # list modified (not untracked) and add [14]

Committing

  • Spend effort on your Git commits - Version control systems like Git are widely appreciated for their ability to provide a centralised location for source code, for helping people work together on the same code base, and for allowing you to scrap the crap you just wrote and get your code back to the state it was in before you started your misguided refactoring.However, there's one thing it can do for you that is overlooked too often: help you document your code. In fact, it can prove to be at least as useful as comments.

Committing moves files from Index into HEAD.

git commit -m Commit message goes here.
git commit -a
  git adds at the same time
git commit --amend
  without file change, alter last commit msg
git commit --verbose
  # or -v, add diff to commit message comments (not the commit message)
git commit --allow-empty -m 'Empty commit so I can open a PR'


git commit -eF .git/filename


Remotes

git remote -v
  # list all remotes
git remote add origin <server>

git remote show origin
git remote set-url origin git://github.com/chief/global.git

git remote set-url --push origin git@github.com:User/forked.git

git remote set-url --push origin DISABLE
git branch --set-upstream master origin/master
  # branch master set up to track remote branch master from origin.


Sending/receiving

git fetch [origin] [master]
  get files from origin master remote

git pull [origin] [master]
  fetch from and merge with another repository or a local branch

git push otherrepo <branch>
  stick branch on otherrepo remote

Reset

git reset --hard HEAD
  reset any uncommitted changes in Index
git reset --hard origin/master
  reset from remote master

Rebase



Branches

git branch
  # list branches with current starred
git branch -a
  # list branches (including remote)

git branch <branch>
  # create branch
git branch -d <branch>
  # delete branch

Staging

git checkout <branch>
  # switch to working on <branch>
git checkout -b <branch>
  # same as "git branch <branch> | git checkout <branch>"

git checkout -
  # check out previous branch

git checkout HEAD@{1}
  # checkout next commit on current branch

git checkout HEAD~
  # checkout previous commit on current branch
git checkout -- <file>
  # checkout file from HEAD

git checkout SHA <file>
  # checkout file from the point of a certain commit

Merging

git merge <branch>
  merge branch, keep branch in commit log
cd path/to/project-b
git remote add project-a path/to/project-a
git fetch project-a
git merge project-a/master # or whichever branch you want to merge
git remote remove project-a [17]
git rebase <branch> - fast-forward merge current to <branch> (?)


Stash

Log

git log
  list commits with message and sha
git log .
  log for current directory

git log --since="today"
git log --since="6am"

git log -p
  show diffs
git log -p -2
   show second two last diffs
lol = log --graph --decorate --pretty=oneline --abbrev-commit
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

Bisect

Merging

git mergetool

git mergetool -t opendiff


Meld GUI is worth checking.

Fugitive

:ls
  list window numbers
[c
  jump to previous changeset

]c
  jump to next changeset
:diffget [window]
   get change from active window to working
:diffupdate
   redraw diff highlighting

Tags

git tag
  list repo tags

tags point to a sha. use git checkout [tag] to change to the commit.

git tag v1.3
  # add a tag

git tag -a v1.4 -m "my version 1.4"
  # add an annotated tag with message
git push origin --tags
  # push changed tags

Submodules

git submodule add git@mygithost:project [--branch x.y] [directory/path]
  # add submodule and checkout specific branch
git submodule init
git submodule update

or

git submodule update --init
  # bootstrap submodules listed in .gitmodules after cloning somewhere
git submodule foreach git pull origin master
  # update all submodules


git submodule deinit directory/path
  # disable a submodule
git rm --cached path/to/submodule
  # remove a submodule

Subtrees

git-subtrac

.gitignore

  • man gitignore - Specifies intentionally untracked files to ignore

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
*
!.gitignore
!sxhkd/sxhkdrc
!bspwm/*
!compton.conf
!gtk-2.0/gtkrc-2.0
!/myrepos

Archive to tar

git archive [20]
  default output is tar
git archive master | tar -x -C /somewhere/else
  pipe to extraction
git archive --format=tar --remote=git@server:repo.git master | tar -xf -

also, Legit.

Bundle

  • https://git-scm.com/docs/git-bundle - provides support for git fetch and git pull to operate by packaging objects and references in an archive at the originating machine, then importing those into another repository using git fetch and git pull after moving the archive by some means (e.g., by sneakernet). As no direct connection between the repositories exists, the user must specify a basis for the bundle that is held by the destination repository: the bundle assumes that all objects in the basis are already in the destination repository.


Filter branch

Plumbing


Attributes


Hooks

  • pre-commit - A framework for managing and maintaining multi-language pre-commit hooks.

CLI tools

Zsh prompt symbols

Status:
✔:	repository clean
●n:	there are n staged files
✖n:	there are n unmerged files
✚n:	there are n changed but unstaged files
…:	there are some untracked files

Branch:
↑n:	ahead of remote by n commits
↓n:	behind remote by n commits
↓m↑n:	branches diverged, other by m commits, yours by n commits
::	hash, not a branch

Ncurses

tig

gita

  • https://github.com/nosarthur/gita - a command-line tool to manage multiple git reposThis tool does two thingsdisplay the status of multiple git repos such as branch, modification, commit message side by sidedelegate git commands/aliases from any working directory


to sort

  • https://github.com/icefox/git-achievements - records all of the achievements you acquire while using Git. There are over 40 achievements, most with different levels to be achieved. After a git command is executed if a new achievement is unlocked git-achievements will display a message on the console for your enjoyment:





BFG Repo-Cleaner

Clients







  • https://github.com/jesseduffield/lazygit - A simple terminal UI for git commands, written in Go with the gocui library.Are YOU tired of typing every git command directly into the terminal, but you're too stubborn to use Sourcetree because you'll never forgive Atlassian for making Jira? This is the app for you!



Handy



  • https://github.com/ndbroadbent/scm_breeze - a set of shell scripts (for bash and zsh) that enhance your interaction with git. It integrates with your shell to give you numbered file shortcuts, a repository index with tab completion, and many other useful features. [22]









Large files

git-annex

GitMedia

LFS

  • Git Large File Storage - replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.



Repository management




Articles

Git used for private repo, with Gitweb for easy overview.

git-daemon

Gitolite

  • create git user
  • ~/.ssh/authorized_keys is empty or non-existent
  • put pub key in $HOME/YourName.pub
git clone git://github.com/sitaramc/gitolite
mkdir -p $HOME/bin
gitolite/install -to $HOME/bin
gitolite setup -pk YourName.pub

gitolite-admin is only accessible with the public key named after the git account used for the gitolite

GitLab

recommends 1Gb ram!

gerrit

RhodeCode

GitBucket

Gitblit

Gogs

Gitea

Stagit

GitGrep

Pagure

  • Pagure - git-centered forge, python based using pygit2.

Apache Allura

  • Apache Allura - an open source implementation of a software forge, a web site that manages source code repositories, bug reports, discussions, wiki pages, blogs, and more for any number of individual projects. Read about all of Allura's features or compare features to other OSS forge software.

GitPrep

Services

GitHub




Tools

  • github-cli - command-line interface to GitHub's Issues API (v2)


  • hub - a command-line wrapper for git that makes you better at GitHub.




Gist



Related

  • GitTorrent - a peer-to-peer network of Git repositories being shared over BitTorrent. You can read more about the project at this blog post. The design of GitTorrent has five components: A "git transport helper" that knows how to download and unpack git objects, and can be used by Git itself to perform a fetch/clone/push. A distributed hash table that advertises which git commits a node is willing to serve. A BitTorrent protocol extension that negotiates sending a packfile with needed objects to a peer. A key/value store on the distributed hash table, used as a "user profile" describing a user's repositories and their latest git hashes. A method for registering friendly usernames on Bitcoin's blockchain, so that a written username can be used to find a user instead of an ugly hex string.
  • http://prose.io/ - static site management
  • Ost.io ("open-source talks") is a forum for open-source projects and the best place for discussing project stuff with other users. It is tightly integrated with GitHub.

Bitbucket

N.b. https clone url includes username@, which will require password. remove this for straight-through access.

curl -k -X POST --user user:pass "https://api.bitbucket.org/1.0/repositories" -d "name=project_name"
https://<user>:<pass>@bitbucket.org/<user>/<project>.git

Other



to sort