Friday, October 13, 2017

Git - Basic Commands

Understanding

The Git repository is a tree of commits each of which keeps a pointer to its parent (for merge commits: two parents) and all of the files modified in this commit.  HEAD, tags and branches are all labels to a node of that tree.

Branches

  • git branch NAME   --- create a branch of the current branch
  • git branch --all  --- list all branches local and remote
  • git checkout -b release-4.16.0 origin/develop   --- create branch release-4.16.0 from the current branch and set tracking to origin/develop
  • git branch -u origin/develop  --- set tracking on an existing branch
  • git branch -d NAME  - delete a local branch but only if it's safe (i.e. we won't loose access to any commit)
  • git branch -D NAME  - force delete a local branch (requires extra permissions; commits to which we may loose access will be available through its ID for next N days (default is 30 days) and it will be garbage-collected after that)
  • git push origin --delete NAME  - force delete a remote branch

Commit

  • git commit  --- committing the changes to local git repo
  • git commit --amend  --- allows to edit the last commit, to edit its message and correct the files you committed
  • git reset --soft HEAD~1 --- undoes the most recent commit without reverting the changes in the files
  • git fetch origin && git reset --hard origin/master --- reset your branch to what it was before any changes and commits done locally.  To save the local commits just in case, do git branch saved-my-commits-here.

Pushing and Pull

  • git add  --- staging files for commit
  • git pull  --- if the current branch is behind the branch it tracks, this command will update it
  • git push origin local-branch:remote-branch  --- push local branch to remote repository origin

Rebase

  •  git rebase origin/develop  --- rebases, which means if the current branch is behind on some commits from origin/develop branch, it will incorporate all of them and place as if they were made earlier than those commits that the current branch is ahead of the origin/develop.
  • git rebase -i HEAD~2  --- interactive rebase, that is a tool to modify commit history.  In most common scenario it is used to squash two or more recent commits into one (though commit --amend can do it, too) but it can also remove commits from the history as well as change their order.  The tool can do lasting damage if you make mistake - use with caution!

Tags

  • git tag v4.23.0  --- adds a tag v4.23.0 to the current node (there is also another, lighter type of tag - I haven't been using it)
  • git push origin v4.23.0  --- pushing a tag to the remote repo

Other

  • git fetch  --- updates the local git repo from the remote repos
  • git status  --- shows the commits that the current branch is ahead and behind with respect to the branch it tracks as well as unversioned and modified files that aren't added to the change set.
  • git log  --- shows a paged list of commits with the most recent first
  • cherrypicking  --- allows to copy a commit from into another branch (important: it's a copy
  • git merge my-other-branch  --- merges my-other-branch into the current branch; use --no-ff if you always want a merge and not just a git pull if the last commit on current branch happens to be an ancestor of the commits on my-other-branch.
  • git mv --- rename or move a file or folder without losing its history (to see its full history afterwards use git log --follow).