Finding out what is going on
git help explantion (in depth) on command; or just general help without
git status shows status of the files in the working directory and staging area compared to the local repo (changed, deleted, untracked). Also shows if a repo is out of sync from its remote repo.
git log
log shows commits ids with log messages. When no specifier is given, shows the whole commit history of the current branch. When a commit is given, it shows commits leading up to and including it. When a range symbol (..) is given, it instead show commits since then. If a range is given, shows commits from after the first, up to and including the second.
[path/file] only for this file
--since="1 week ago" limit by date
--until="yesterday" limit by date
-p with patch details
--author="Name" only by this author
git shortlog
show grouped by author commit messages for all commits
git show
show runs git log -p, for a single commit, by default the most recent one. if run on a file instead, will list its contents.
[
] run for another commit.
[] run for a file in this commit.
git blame file
for each line gives information (author etc) from the revision which last modified it
does not tell you anything about lines which have been deleted or replaced
git diff show differences between two revisions. If you add a filepath, only for that file. By default, compares your:
git diff working copy vs
staging area
git diff --staged staging area vs
HEAD
git diff ... between working copy and commit
git diff --- between two branches
git diff ... between two commits
git ls-files
-d show deleted files
git ls-tree commit-id
lists all objects that are part of this commit (which are technically managed in a tree data structure, so the commit may contain blobs which correspond to files, and trees which correspond to directories).
git reflog
This shows all actions that have been done in your repository.
Working with branches (locally)
All changes apply by default to your current branch.
git branch
lists branches, showing the current one
-r known remote branches
-a all branches, remote or local
git branch create a branch pointing to the current HEAD, but do not switch to it
git checkout switch to branch. Git resets your working directory to look like the snapshot of the commit you check out when you switch branches, changing, adding and removing files as needed. If you would lose uncommitted changes this way, git warns you.
git checkout -b to create and check out a new branch based on branch, in one go. Leaving out creates it based on the current branch
git checkout -f drop all changes in branch
git checkout drops changes in file
git checkout -- drop all changes in working directory
git checkout -- drop all changes in file
git merge merge the named branch into the current branch. So to merge back a side branch, checkout the master, then merge in your branch. You can also merge in changes from the master to your checked-out branch.When you merge successfully, git automatic creates a new commit for the merge.
--squash take all the work and squash it into one no-commit on top of current branch
--no-commit do not automatically make a commit at the end (useful if you want to apply further changes to sqashed merge before committing)
Dealing with merge conflicts: if both branches changed a file, you will find markes of the alernate versions in it. You need to edit, and add the changed files to resolve them.
git branch -d delete a no longer needed branch after you merged it in
git rebase rebase your changes on the named commit. git figures out all the changes you made in your current branch compared to last common ancestor you had with it, then applying them to it. Typically you rebase onto a mothership. The advantage is that now adding your commit back into the mothership does not need a merge with multiple ancestors, as it is already based on the head of the mothership, and can be fast-forwarded.
Rebase will stop on each conflict. After resolving this conflict (editing files and running "git update-index" on them), "git rebase --continue" will continue the rebase. If you want to skip the conflicting commit, use "git rebase --skip". The whole rebase can be stopped and your branch restored to how it was by using "git rebase --abort".
Do not rebase commits that you have pushed to a public repository.
For each branch, you can define default remotes and branches on them to push/pull in the config file. Each branch has an own section there.
Working with remote branches and repositories
Remote branches point to a remote repo. To work with them, you first have to fetch into the local repo, where the branch name will be changed to the form
remote/branch. You cannot change the remote branches, and? if the remote repo changes in since you fetched, they may be outdated. The local copies of the remote you can then merge.
git clone source creates local copy of a remote repo and set "origin" repo to it
git remote add add a new remote to your remotes
git remote -v show what remote repos are defined (-v: with details on fetch/push) show details about the remote
git fetch update local repo with new commits from remote.
git pull fetch changes from remote repo branch and merges them into your local one. Without branch into your current branch.
--rebase: instead of merging changes copy everything, then apply your local changes
git push pushes changes push to remote branch (create if needed), merging them there. The branch on the remote itself is just named branch, like the branch on the local repo. Only the local pointer to the remote branch is named remote/branch.
defaults to the current branch's remote, or origin if none configured.
defaults to all branches with matching names (this behaviour can be configured).
"
:" as branch explicitly pushes all matching branches
HEAD uses the
current branch
localname
:remotename to use a different remote branch(name) than the local
--all push all changes in the source repo, even if unkown in target
-f force overwrite in case of rebase, as new version of branch is not a child of old in remote
git fetch
fetch fetches changes from a remote repo, and updates the commit FETCH_HEAD in the local repo, without merging them into your local repo history, like pull would. This way you can examine them with git log master..FETCH_HEAD, and if they are fine merge them in with "git merge".
Working with remote branches
Some commands automatically
git fetch origin # Updates origin/master
git rebase origin/master # Rebases current branch onto origin/master
git pull --rebase pull from remote repo and update local , then rebase against it. More explicit
git rebase --abort ... roll back the rebase in case it is too messy
git merge
merge merges two commits, by default the currently active branch and the one specified. If that is FETCH_HEAD, it will merge in the changes pulled with fetch.
Working with your local repository
git add file
introduces the file to git for tracking.
git commit -m "Message"
commit writes changes in files that are under management of git to the repository. A commit can record changes in several files. Each commit has an informational message, and gets assigned a 40 character unique id called "git log". Changes can be newly added files, deleted ones, or edited ones.
[path/file] commit changes in file
-a commit all open changes
--amend change the message on the last commit. Use with care.
-a --amend change the message and the last committed changes to the current
git commit --amend as long as your working directory is not changed, you can add, and remove files to the index, then recommit with amend (even changing the message, if you had a typo), and it all will still just be the same commit.
git reset HEAD unstage a file you erronouslay added to the index
git checkout -- undo changes done in working dir since last commit
git reset --hard reset all uncommitted changes
git reset --hard HEAD@{headnumber} undo that latest pull
git rebase -i HEAD^3, then pick one, squash the others, to squash several local commits into a single one before pushing. Useful if you make intermediary commits to save your work.
git revert undo a commit
Useful Aliases
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'
No comments:
Post a Comment