Git: Practice

How to Checkout/Clone From a Specific Git Commit Id (SHA)

Important Note: Always keep a backup of your code with all the commit histories before performing any git activity that may change or reverse the code.

Checkout From Specific Git Commit ID

Shows all commits that are in any of local branches but not in any of remote-tracking branches for origin :

$  git log --branches --not --remotes=origin
Get the commit ID (SHA) that you want to checkout.

$ git log
Copy the commit (SHA) id and checkout :
  • The complete SHA ID
  • 
    $ git checkout 28cd74decb47d8a5294eb59e793cb24a0f242e9e
    	
  • short form of the SHA ID from the start
  • 
    $ git checkout 28cd74de
    	
Afer this checkout, it will be in the detached HEAD mode.
Meaning, you can play around with making the changes without impacting any branches.

If you want to make changes from the commit ID checkout, you need to create a new branch,


$ git checkout -b <new-branch-name> <commit-id-sha>
	
This will retain everything from the commit ID to the <new-branch-name> .

Set Git HEAD to Specific Commit ID

If you want to revert your HEAD to a specific commit, perform a hard reset with the commit SHA after pulling the changes,

$ git pull
$ git reset --hard  <COMMIT-SHA-ID>

Apply changes from one Git branch to another

In Git, there are several ways to integrate changes from one branch into another:
  • Merge branches
  • Rebase branches
  • Apply separate commits from one branch to another (cherry-pick)
  • Apply separate changes from a commit
  • Apply specific file to a branch

Merge branches

Merging your branch into master is the most common way to do this.

Suppose you have created a feature branch to work on a specific task, and want to integrate the results of your work into the main code base after you have completed and tested your feature.

your teammates continue to commit their work to master:
If the current branch is master, when you run

$ git merge feature
the changes from your feature branch are integrated into the HEAD of the master branch. Then "git merge feature" will replay the changes made on the feature branch since it diverged from master (i.e., D) until its current commit (3) on top of master:
If your working tree is clean (which means you have no uncommitted changes), and no conflicts occur between your feature branch and the target branch: Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Rebase branches

When you rebase a branch onto another upstream branch, you apply the commits from the first branch on top of the HEAD commit in the upstream branch.

git rebase [ <upstream> [<branch>]]
Assume the following history exists and the current branch is "feature":

          A---B---C feature
         /
    D---E---F---G master
Either of the following commands can do the rebase operation :

$ git rebase master
$ git rebase master feature
and results in the following:

                  A'--B'--C' feature
                 /
    D---E---F---G master

Apply separate commits from one branch to another (cherry-pick)

Sometimes you only need to apply a single commit to a different branch instead of rebasing or merging an entire branch.
This may be useful, for example,
  • if you are working in a feature branch and want to integrate a hotfix from master that was committed after the two branches have diverged.
  • if you may want to backport a fix to a previous release branch
"git cherry-pick" is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.

Assume we have a repository with the following branch state:


    a - b - c - d   main
         \
           e - f - g feature
If we wanted to use commit `f` in main:
  1. checko out to the target branch main
  2. 
    $ git checkout main
      	
  3. pic up the desired commit from another branch feature
  4. 
    $ git cherry-pick f-hash
      	
Git history will look like:

    a - b - c - d - f   main
         \
           e - f - g feature  

How to merge only specific commits from a pull request with git cherry-pick

How to use git cherry-pick

Git's cherry-pick command allows you to "cherry pick" only the commits you want from another branch.
  • Pull down the code locally.
  • Get back into the branch you're going to merge into your branch.
  • In general, you'll likely do merge commits from the master :
      
    $ git checkout master
    	
  • Find the commits you want to pull into your branch.
  • Grab the unique commit hashes for each of the commits that you want via git log.
  • Change back to your branch, then, "cherry pick" the commits you want into this branch.
  • 
    $ git cherry-pick commit-long-hash  
      	
  • Push up your branch

Android: Source Control Tools

Working with Android code requires using both Git (an open-source version-control system) and Repo (a Google-built repository-management tool that runs on top of Git).
Repo doesn’t replace Git, it only makes it easier to work with Git in the context of Android.
Repo uses manifest files to aggregate Git projects into the Android superproject.
In most situations, you can use Git instead of Repo, or mix Repo and Git commands to form complex commands.
However, using Repo for basic across-network operations makes your work much simpler. For more details on Repo, see the Repo Command Reference, Repo README

Gerrit is a web-based code review system for projects that use Git.
Gerrit encourages a more centralized use of Git by allowing all authorized users to submit changes, which are automatically merged if they pass code review.

Repo Command Reference

init


$ repo init -u url [options]
This creates a .repo/ directory with Git repositories for the Repo source code and the standard Android manifest files.
  • -u
  • Specify a URL from which to retrieve a manifest repository.
    The common manifest is found at https://android.googlesource.com/platform/manifest.
  • -m
  • Select a manifest file within the repository.
    If no manifest name is selected, the default is default.xml.
  • -b
  • Specify a revision, that is, a particular manifest-branch.

留言

熱門文章