jeet.Log


Git

Basic

Git is a version control system for software projects. In this post I will list down some of the important commands of git and their usage.

  • git config sets configuration variables. There are lot of variables. Please look it up in the documentation. Example: git config --global user.email "abcdef_email@example.com"

  • git init creates a new repository (repo).
  • git remote add origin git@github.com:username/new_repo adds a github repo to your local project

  • Cloning a private repo when you have the credentials: https://username:access_token@github.com/username/repo_name.git

  • Now after you have made some changes, you want to index those files into the staging area. The command to stage the modified files: git add -A .ignore src/

    Git adds your modified files to the queue to be committed later. Files are not committed. Git add adds files to the Git index, which is a staging area for objects prepared to be committed. Add tells git to start tracking a file.

  • After the files are staged, we will commit the files to our local repo. This will create new log, SHA1 hash and all others. The command to do this, git commit -a -m "Trial commit" “Trial commit” is the commit messsage. Ig you want to add any special character into the commit message, put a “" before the special character.

    You can combine both actions with git commit -a. Although I do NOT recommend using it.

  • git push will push your changes in the local repo to your remote repo.

Branching

  • git branch <branchname>] will create a new branch
  • git checkout <branchname> will enter into a particular branch of the project.
  • git branch -m <old branchname> <new branchname> will rename a local branch
  • git branch -d <branchname> will delete a branch
  • git push origin --delete <branchname> will delete a remote branch
  • git push origin <branchname> will push a branch to your remote repository

Merging

Git performs merging in two ways: 1st forward merge and 3 way merge.

When there is a linear path from feature to master branch, the master branch pointer just has to be translated down the straight path to the feature pointer. This kind of merging is called 1st forward merging. 1st forward merging do not create new auto-commits by git before merging.

In case the master and feature branch takes different path after a commit point in the past, Git looks at 3 different points when a merge command is executed. 1) The tip of the feature branch, 2) The tip of the master branch, 3) The commit point from where the feature and master branch diverged. Based on this 3 points, git combines the changes by creating new commit called the Merge Commit, then merges the two branches.

  • git merge <branchname> will merge a branch into the active branch
  • git merge <source branchname> <target branchname> will merge a branch into a target branch

Rebasing and Squashing

When you want to keep your history of your public branches very clean, you do not want to see all the merge commits which was made by git in the process of merging. So you can do “squash and merge” or “rebase and merge” In Squash and Merge, commits will be combined to one commit in the base branch. The final commit message in the master branch will contain the commit messages of all the commits which will get combined to one. In Rebase and Merge the commits will be rebased and added to the base branch. Rebasing is essential when you want to take only a subset of commits from a branch and merge it into another branch. Rebasing puts all commits from a specific feature branch and puts them on the top of main base branch. Doing these in local repo is a multistep process. First rebasing and squashing.

Take the SHA1 of last commit before creation of a feature branch, git rebase -i SHA1 will open rebase in an interactive mode. You need to choose options what you want. It will open a text file where you will have to choose options before your commits regarding how do you want to rebase them. Eg. s 2d458754 File 4 was created in the feature2 branch, or, pick 2d458754 File 4 was created in the feature2 branch. Then close the file and you will be taken into another text file which is the final commit message file. Once you edit and close it, git will rebase as per your mentioned options.

Sharing & Updating Projects

  • git push origin <branchname> will push a branch to your remote repository
  • git push -u origin <branchname> will push changes to remote repository (and remember the branch)
  • git push will push changes to remote repository (remembered branch)
  • git push origin --delete <branchname> will delete a remote branch
  • git pull will update local repository to the newest commit
  • git pull origin <branchname> will pull changes from remote repository
  • git remote add origin ssh://git@github.com/[username]/[repository-name].git will add a remote repository
  • git remote set-url origin ssh://git@github.com/[username]/[repository-name].git will set a repository’s origin branch to SSH

Logging

  • git log will display all the logs git has made during its course of action. It has got few important options: --oneline which gives a one line summary of the logs, --graph will give a tree like view of the various actions, --stat will display the quantity of change, -p will tell which files got change and what changes were made on those files.
  • git shortlog will diplay quantity of commits of a specific authors. Options are: -n, -s, -e
  • git log --author="abcd" will list out all the commits made by author abcd in the particular branch.
  • git log --grep="sometext" will grep out all the lines in the log file which has the instance of sometext in it.
  • git log --pretty=format:"Hey %cn is the commiter, with full SHA1 as: %H, short SHA1 as: %h, and on the commit date: %cd" will output into specific format
  • git log --merge --oneline will show all merge commits, wherease git log --no-merges --oneline will show ONLY actual commits made BY people and not commits that are generated by merge operation.

Reset and Revert [DESTRUCTIVE, CAUTION!]

  • git reset <SHA1> will reset back to previous commit. --soft option will keep your files, and stage all changes back automatically, --hard which will completely destroy any changes and remove them from the local directory. This option is highly destructive, and only should be performed when you know what you are doing. There is another mode too ‘–mixed’
  • git revert <SHA1> will revert back to an old commit and create a brand new commit.

Amend

git commit --amend command is a convenient way to modify info into the very last commit.

  • git commit --amend -m "New commit message" changes the message of the last commit with the neew message.
  • git commit --amend --author="New Name author@gmail.com" changes the author info of the last commit with the new author info.

Cherry-pick

git cherry-pick copy one commit from one branch to another branch

  • git cherry-pick <SHA1> will copy commit with SHA1 from another branch to this branch.
  • git cherry-pick --no-commit <SHA1> will stage in this branch but not commit.

Reflog

git reflog can be useful to log all operations made in your LOCAL repo. It outputs every operation with its SHA1 You can copy this SHA1 and use it to checkout a specific commit. Eg, git checkout 0a681442

Stashing

  • git stash allows you to save some staged but uncommitted work and checkout other branch so that those uncommitted changes remains in the memory once you get back to this branch.
  • To see the temporary informations (SHA1) check this: ls .git/refs/stash
  • got cat-file -p <SHA1> to check stash with a SHA1
  • To bring back all the uncommitted works when you are back to this branch, execute, git stash pop

Garbage collections (gc)

This is very well explained here: Atlassian website :

Git repositories accumulate various types of garbage. One type of Git garbage is orphaned or inaccessible commits. Git commits can become inaccessible when performing history altering commands like git resets or git rebase. In an effort to preserve history and avoid data loss Git will not delete detached commits. A detached commit can still be checked out, cherry picked, and examined through the git log. In addition to detached commit clean up, git gc will also perform compression on stored Git Objects, freeing up precious disk space. When Git identifies a group of similar objects it will compress them into a ‘pack’. Packs are like zip files of Git bjects and live in the ./git/objects/pack directory within a repository.