Difference between revisions of "Short Notes on git"

From PaskvilWiki
Jump to: navigation, search
(Basic Commands)
(Skip Retyping Your Password)
Line 103: Line 103:
=== Skip Retyping Your Password ===
=== Skip Retyping Your Password ===
<pre>$ git config --global credential.helper cache</pre>
<pre>$ git config --global credential.helper store</pre>
Run <tt>git pull</tt> and enter your username and password. These will be stored in <tt>~/.git-credentials</tt> file and reused each time.
=== Ignoring Files and Folders ===
=== Ignoring Files and Folders ===

Revision as of 18:34, 27 December 2019

Basic Commands

The git has 3 "trees" maintained automatically within your local repo - "working copy" is the files you're actually working on, "index" is kind of staging area, and "head" is your last actual commit. You're working on your working copy, then add changes to the index, then push those to head locally, and finally push those to (remote) repo.

# two ways to check out a repo; 1) first local, then connect
$ git init                                   # init local copy
$ git remote add origin <server>             # connect it to remote
# or, 2) checkout a remote repo straight away
$ git clone /path/to/repo                    # create working copy of local repo
$ git clone user@host:/path/to/repo          # create working copy of remote repo

$ git status                                 # get info about local copy, incl. branch ID, changes, etc.

$ git fetch                                  # download changes but do not integrate in the HEAD
$ git pull                                   # update the local copy to the latest revision
$ git pull <remote> <branch>                 # pull specific branch
$ git diff <source> <dest>                   # view differences between "source" and "dest" branches
$ git merge <branch>                         # merge changes from another branch (e.g. "origin/master")
                                             # you should merge from remote ("origin") as your local copy may differ

# set meld as diff and merge tool; use --global to apply globally, not just per-repo
$ git config difftool meld
$ git config difftool.prompt false           # don't ask each time whether to run meld
$ git config mergetool meld                  # don't ask each time whether to run meld
$ git config mergetool.prompt false
$ git difftool                               # you need to use "difftool" to run meld for diff, not just `git diff`
$ git mergetool                              # run this only after `git merge` if there are any conflicts to be resolved

# getting the changes to the remote repo
$ git add [-i] <file>                        # add changes from file(s) to index (-i = interactive)
$ git commit -m 'message'                    # commit changes from index to head
$ git push origin master                     # push changes from head to master branch, or
$ git push <remote> <branch>                 # push changes to <branch>

$ git checkout -- <file>                     # revert file and replace with current HEAD version, exc. of changes added to Index
$ git fetch origin                           # fetch latest history from server
$ git reset --hard origin/master             # drop all local changes, incl. those in Index

# branching
$ git branch -av                             # list existing branches
$ git checkout -b <branch>                   # create a new branch, and switch to it, or
$ git checkout <branch>                      # switch to branch (make it your HEAD)
$ git push origin <branch> --set-upstream    # make the branch available to others, and set the upstream
$ git branch -d <branch>                     # delete local branch
$ git branch -dr <remote/branch>             # delete remote branch

# other stuff
$ git tag <tag name> <commit id>             # create tag from a commit
$ git log --pretty=oneline [--author=<name>] # one-line per commit listing
$ git log --graph --oneline --decorate --all # ascii tree of branches, tags, ...
$ git log --name-status                      # list changed files

# "svn:externals" like inclusion of another repo; need to commit this as well
$ git submodule add -b <branch> <repo> [<folder>]
$ git add <folder> .gitmodules ; + commit ; + push

$ git submodule update --remote              # update the submodules from their respective repo's

Typical Flow

$ git clone [remote repo]                     # get a local copy of the repo
$ git checkout -b <branch>                    # start a new branch

# do your changes...
$ git add <files>                             # register changes for commit
$ git commit -m 'message'
$ git push origin <branch>                    # commit the branch with changes to repo
# repeat as needed...

# once done with the branch
$ git merge master                            # pull all changes from "master"
$ git mergetool                               # resolve conflicts; commit again to branch if needed - `add`, `commit`, `push`
$ git checkout master                         # switch to master
$ git merge <branch>                          # this should pass ok; commit to "master" afterwards
$ git branch -d <branch>                      # clean up - remove the branch
$ git tag <tag> <commit id>                   # optionally, also tag the commit after merging

Resolve Conflict

If on git pull you get a message:

error: Your local changes to the following files would be overwritten by merge:
	[list of files]
Please, commit your changes or stash them before you can merge.

You need to resolve the conflict manually.

git fetch
git add [file]
git commit -m 'resolving'
git merge

At which point you'll get message:

Auto-merging [files]
CONFLICT (content): Merge conflict in [files]
Automatic merge failed; fix conflicts and then commit the result.

Now you need to manually edit the file(s) to resolve the conflict, and then add/commit the resolved file(s).

Skip Retyping Your Password

$ git config --global credential.helper store

Run git pull and enter your username and password. These will be stored in ~/.git-credentials file and reused each time.

Ignoring Files and Folders

$ touch .gitignore
# edit the file with your fave editor (folders should end with '/'):

# it's good practice to commit the file as well
$ git add .gitignore
$ git commit -m "Added .gitignore file"
$ git push origin master

Using Git with gnome-keyring

sudo apt-get install libgnome-keyring-dev
cd /usr/share/doc/git/contrib/credential/gnome-keyring
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring