36

Version Control System: Get up to speed with GIT

 5 years ago
source link: https://www.tuicool.com/articles/hit/iQni6rI
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1*NJF7lpGTkoxYYCPwuXGuOA@2x.png
Git Workflow

What is Git?

Git is an open source distributed version control system used primarily for Source code management (or any digital body of work) and keeping track of the changes in any set of files.

1*u4dlEq4sqIT6iHL_Usvwnw.png
Version Control

Basic Git Terminology

Commit

To commit changes means recording the changes to the repository. A commit (Commit Object) represents a version of all files tracked in the repository and is addressable via a hash ( SHA-1 checksum ).

Branch

Movable pointer pointing to one of your commits. Every time you commit, your branch moves forward automatically to the last commit you made.

HEAD

Reference to the last commit in the currently checked-out branch.

Repository

A repository is a .git directory for your project. It contains all of your project’s files and stores each file’s revision history.

Working Tree

Directory on your file system linked to a Git Repository. You can make changes here and commit these changes to be pushed to the Repository.

Staging Area (or Index)

The staging area is a file that stores information about what will go into your next commit.

Tag

Named pointer pointing to a commit which uniquely identifies a version of the Git repository. Tags are usually used to mark a release. Tags make it easier to revert to a particular named point of the Git repository.

Basic Git Commands

Some of the basic and most used commands have been briefed below along with their usages. These commands will allow you to start using and interacting with a Git repository:

  1. Installing Git

    Before you start using Git, you have to make it available on your computer. Even if it’s already installed, it is a good practice to update it to the latest version. You can visit these links for the latest versions.
Installing on Linux: http://git-scm.com/download/linux
Installing on Windows: http://git-scm.com/download/win
Installing on Mac: http://git-scm.com/download/mac

2. Setup and Configuration

Once you have installed Git, you’ll need to identify yourself with a username and email to Git.
git config — global user.name “example”
git config — global user.email “[email protected]

3. Creating Project

git init

Git init is used to create an empty Git repository or reinitialize an existing one.

Else to initialize a repo within a particular directory, use:

git init [project directory]

git clone : Used to clone a repository into a new directory (Download a project and its entire version history).

git clone [repo url]

4. Adding, Committing and Resetting Changes

git add : Adds a change in the working directory to the staging area.

git add [file]

git status : Lists all new or modified files to be committed.

git status

git commit : Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files.

git commit -m “commit message”

git reset : Erase mistakes and craft replacement history

git reset [file]
git reset [commit]
git reset — — hard [commit]

5. Refactoring Filenames

git rm : Deletes the file from the working directory and stages the deletion.

git rm [file]

git mv : Changes the file name and prepares it to commit.

git mv [original-file] [renamed-file]

6. Branching

git branch : List, create or delete branches. Lists all local branches in the current repository.

git branch [branch-name] : Create a new branch

git branch -d [branch-name] : Delete the specified branch

git checkout : Create and switch branches or checkout remote branches.

git checkout [branch-name] : Switches to the specified branch and updates the working directory.

git checkout -b [new-branch-name] : To create a new branch and checkout the newly created branch.

git checkout [remote-branch]: To checkout a remote branch

7. Synchronizing Changes

Fetch : To retrieve remote changes without merging them into your local branches

git fetch [remote-name] : To fetch all the branches from the repository.

git fetch [remote-name] [branch-name] : To fetch the specified branch from the repository.

git fetch —-all : To fetch all registered remotes and their branches

Merge: Join two or more development histories together. Before executing git merge , it is required to ensure that you are currently checked out into the receiving branch and both the receiving branch and the branch to be merged are up-to-date with the latest remote changes.

git merge [to-be-merged-branch-name] : Merges the specified branch into the currently checked out branch.

Pull: Combination of Fetch and Merge. Used to get changes from a remote repository into the current branch

git pull : Gets changes of the currently checked-out branch from the remote.

git pull [remote-name] [branch-name] : fetches commits from the branch of the original remote, and then it merges the pulled branch into the branch you currently have checked out.

Push: Used to upload local repository content to a remote repository.

git push [remote name] [branch name]

Stash: Temporarily stashes the changes you’ve made to your working tree so you can work on something else, and then come back and re-apply them later.

git stash : stash the changes to go to a clean working directory

git stash pop  : remove the most recent stash from the list and apply it to the current working directory

git stash list  : Show all existing stashes

git stash drop : In case you don’t need a stash, use this command to remove it from the list of stashes.

8. Reviewing History

git log : List version history of the current branch

git log

git diff : To see the changes you have made to the working directory. To view the changes you made relative to the staging area for the next commit

git diff HEAD  : everything unstaged and staged did to the last commit

git show : Outputs metadata and content changes of the specified commit

git show [commit-hash]

9. Patching :Patching is a way to share one branch commit with other developers or to another branch.

Cherry-Pick : Given one or more existing commits, apply the change each one introduces, recording a new commit for each.

git cherry-pick [existing commit hash to be picked]

Rebase   : Rebasing is the process of moving or combining a sequence of commits to a new base commit. From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you’d created your branch from a different commit.

git rebase [branch-name to be rebased with]
git rebase -i [branch name to be rebased with] //To be able to pick                   commits to be moved
git rebase --continue      // To continue with the rest of rebase after fixing conflicts
git rebase --abort         // Completely undo the rebase
git rebase --skip          // Completely skip the commit

Revert  : A revert operation will take the specified commit, inverse the changes from that commit, and create a new “revert commit” .

git revert [insert bad commit hash here]

10. Debugging

Blame : Annotate each line and tell about the revision which last modified the file.

git blame [file-name]

grep : Print lines matching a pattern

git grep [regexp]

Useful Resources:

  1. https://git-scm.com/docs
  2. https://git-scm.com/docs/gittutorial
  3. http://devdocs.io/git/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK