Skip to content

Git Introduction

  • Helps a team manage changes to a code base, jump back and forth in the code-base's timeline and so on.
  • Git is the most widely used modern VCS by far!
    • Developed and maintained by Linus Torvalds (Father of Linux Kernel) in 2005
Workflow
  • Your Local Repository consists of three trees maintained by Git!
    1. your Working Directory
    2. your Index - i.e. the staging area, the files you have added for the next commit
    3. the HEAD which points to the last commit you have made
Add and Commit
  • Changes can be proposed and added to Index using git add <filename> or git add *
  • To commit the proposed files, we use git commit -m "Commit Message"
    • The above commit command makes the changes to the HEAD, but still not on your remote repository
Pushing Changes
  • To send the changes from the 'HEAD of local repo' to the remote repository, we use
    git push origin master
  • master here can be replaced with the branch_name of your choice, to which you want to push
  • git push -u origin new-feature
    • pushes new-feature to the central repository (origin), and the -u flag adds it as a remote tracking branch
    • -u flag: This flag stands for "upstream." When you use it, Git sets up the local branch to track the remote branch. It establishes a connection between the local branch and its counterpart on the remote repository.
    • After this command, you can simply use git push and git pull without specifying the remote branch name, and Git will know which branch to push to or pull from based on the tracking setup.
Connecting to a remote Repo
  • If you haven't cloned the repo, and want to connect the local repo to a remote repo, we use the following
    • git remote add origin <server>
  • After the above command, you will be able to push changes to the specified remote server
Branches
  • Used to develop features isolated from each other
  • Master is the default branch when creating a repository
  • Creating a new branch
    • git checkout -b branch_name
  • Switching back to master
    • git checkout master
  • Deleting a branch
    • git branch -d branch_name
  • A branch will not be available to others unless it is pushed to remote repository
    • git push origin <branch>
Updating and Merging
  • to update your local repository to the newest commit, execute
    • git pull or git fetch
    • git pull puts the files in your local repository as well as the working directory
    • git fetch puts the file in your local repository but not in the working directory
      • You have to use git merge to add the fetched files to the working repositorygitPullVSgitFetch.png
  • to merge another branch into your active branch
    • git merge <branch>
  • Sometimes, auto merge may fail due to conflicts, it's your responsibility to merge those conflicts manually by editing the files shown by git.
  • After making the required changes , you need to mark the files as merged manually with git add <filename>
  • Files can be compared before merging using git diff src_branch target_branch
  • git merge --abort -> go back to the before-merge state in your local repo
Tagging
  • Recommended to create tags for software releases (Why?)
  • Create new tag by : git tag 1.0.0 1b2e1d63ff
    • 1b2e1d63ff -> first 10 characters of the commit id you want to reference with tags
  • How to get the commit id? using the log
log
  • study repository history using git log
  • There are a ton of parameters -> git log --help
  • Cool features
    1. ASCII art tree of all branches, with names and tags of branches
      git log --graph --oneline --decorate --all
    2. Only the files which have changed - git log --name-status
    3. Commits by only a specific author - git log --author=<author_name>

Okay, you messed up doing git, it happens! Let's get over it

Replacing Local Changes
  • git checkout -- <filename>
    • The above command will replace the specified file in working directory to the last content in the HEAD
    • changes already staged(added to the INDEX), as well as new files will be kept.
  • git fetch origin -> git reset --hard origin/master
    • Drop all the local changes and commits , fetch latest history from the server and point your local master branch
Special Mentions
  • built-in git GUI -> gitk