Advanced Git usage: Difference between revisions
No edit summary |
No edit summary |
||
(16 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
'''''[[Using patches to share your updates with others|Previous]] | [[Git and Github tutorials|Next]] | [[Guide to using Git with GEOS-Chem | '''''[[Using patches to share your updates with others|Previous]] | [[Git and Github tutorials|Next]] | [[Guide to using Git with GEOS-Chem]]''''' | ||
#[[Introduction to Git]] | #[[Introduction to Git]] | ||
Line 15: | Line 15: | ||
#[[Receiving updates (aka pulling)]] | #[[Receiving updates (aka pulling)]] | ||
#[[Using patches to share your updates with others]] | #[[Using patches to share your updates with others]] | ||
#<span style="color:blue">'''Advanced Git Usage''' | #<span style="color:blue">'''Advanced Git Usage'''</span> | ||
#[[Git and Github tutorials]] | #[[Git and Github tutorials]] | ||
== Overview == | == Overview == | ||
On this page we discuss how some of the more advanced things you can do with Git. | On this page we discuss how some of the more advanced things you can do with Git. | ||
== Resetting a branch of your local source code folder to an earlier state == | |||
If you would like to abandon work on a branch and restore that branch to an earlier commit, you can use Git's '''hard reset''' option. This is best done with the [[Viewing_the_revision_history#Use_Gitk_to_see_a_graphical_display|Gitk browser]]. | |||
<span style="color:red'>'''''NOTE: You should do a hard reset on branches of your local source code folder, and not on the remote repository.'''''</span> | |||
(1) '''Change''' to your GEOS-Chem source code directory. | |||
cd /path/to/your/GEOS-Chem/Code | |||
(2) '''Start Gitk'''. | |||
gitk --all & | |||
(3) [[Viewing the revision history|'''View the revision history''']] in the top-left Gitk window. | |||
[[Image:GitResetStep1.png]] | |||
In this example above, the '''feature/MyFeature''' branch is checked out, and has some '''unstaged changes'''. These are updates that have not been committed yet. | |||
(4) Let's say that you want to abandon the work that you have done in '''feature/MyFeature''' branch. This may be because you have added a bunch of debugging print statements into the code, but no longer need them. Let's also assume that you want to reset '''feature/MyFeature''' to the same commit as the '''master''' branch. | |||
'''Right-click''' on the commit corresponding to the '''master''' branch. A context menu will appear. | |||
(5) '''Click''' on the menu item '''Reset the feature/MyFeature branch to here'''. | |||
[[Image:GitResetStep2.png]] | |||
(6) A dialog box will appear with 3 radio buttons. '''Click''' on '''Hard: reset working tree and index (discard all changes)'''. | |||
[[Image:GitResetStep3.png]] | |||
(7) '''Click''' on '''OK'''. | |||
(8) [[Viewing the revision history|'''View the revision history''']] again and '''Refresh''' Gitk by either '''pressing F5''' or selecting '''File/Update''' from the dropdown menu. | |||
You should now see this: | |||
[[Image:GitResetStep4.png]] | |||
Note that the '''unstaged''' changes have now disappeared, and that '''feature/MyFeature''' is now at the same commit as '''master'''. | |||
--[[User:Bmy|Bob Yantosca]] ([[User talk:Bmy|talk]]) 19:15, 21 June 2019 (UTC) | |||
== Reverting to an older state of the code == | == Reverting to an older state of the code == | ||
Line 89: | Line 134: | ||
--[[User:Bmy|Bob Yantosca]] ([[User talk:Bmy|talk]]) 17:03, 21 June 2019 (UTC) | --[[User:Bmy|Bob Yantosca]] ([[User talk:Bmy|talk]]) 17:03, 21 June 2019 (UTC) | ||
== Removing references to remote branches == | |||
In a previous section, [[Receiving_updates_(aka_pulling)#Get_all_updated_branches_and_tags_from_the_GEOS-Chem_remote_repository|we learned how to pull references for branches at the remote repository into your local source code folder]]. But when branches are deleted from the remote repository, you will likewise want to delete the references to those branches as well. Here's how to do it. | |||
(1) '''Start Gitk'''. | |||
gitk --all & | |||
(2) [[Viewing_the_revision_history#Use_Gitk_to_see_a_graphical_display|'''View the revision history]] in the top-left GitK window. | |||
[[Image:GitRemoteRemove1.png]] | |||
(3) Let's assume that you want to '''remove the reference to the remote branch remotes/origin/feature/N2O5_hetchem''' branch, as you don't need this anymore. '''Type:''' | |||
git branch -r -d origin/feature/N2O5_hetchem | |||
(4) '''Refresh''' the [[Viewing_the_revision_history#Use_Gitk_to_see_a_graphical_display|'''revision history display in GitK]] by pressing '''F5''', or by selecting '''File/Update''' from the menu. | |||
[[Image:GitRemoteRemove2.png]] | |||
As you can see, the '''remotes/origin/feature/N2O5_hetchem''' branch has now been deleted. | |||
== Further reading == | |||
#[https://www.atlassian.com/git/tutorials/advanced-overview ''Advanced Git tutorials overview'' (Atlassian Git tutorial)] | |||
#[https://www.toptal.com/git/the-advanced-git-guide ''The Advanced Git Guide'' (Toptal)] | |||
#[https://stosb.com/blog/advanced-git-commands-you-will-actually-use/ ''Advanced Git commands you will actually use'' (Stosb)] | |||
#[http://rkd.zgib.net/scicomp/git-advanced/git-advanced.html ''Advanced Git usage'' (Richard K. Darst)] | |||
#[https://www.learnenough.com/git-tutorial/getting_started ''Learn enough Git to be dangerous'' (Michael Hartl)] | |||
---- | ---- | ||
'''''[[Using patches to share your updates with others|Previous]] | [[Git and Github tutorials|Next]] | [[Guide to using Git with GEOS-Chem | '''''[[Using patches to share your updates with others|Previous]] | [[Git and Github tutorials|Next]] | [[Guide to using Git with GEOS-Chem]]''''' |
Latest revision as of 20:30, 5 August 2019
Previous | Next | Guide to using Git with GEOS-Chem
- Introduction to Git
- Installing Git
- First-time Git setup
- Cloning (i.e. downloading for the first time)
- Ignoring files
- Viewing the revision history
- Using Git GUI
- Branching
- Committing
- Tagging
- Merging
- Receiving updates (aka pulling)
- Using patches to share your updates with others
- Advanced Git Usage
- Git and Github tutorials
Overview
On this page we discuss how some of the more advanced things you can do with Git.
Resetting a branch of your local source code folder to an earlier state
If you would like to abandon work on a branch and restore that branch to an earlier commit, you can use Git's hard reset option. This is best done with the Gitk browser.
NOTE: You should do a hard reset on branches of your local source code folder, and not on the remote repository.
(1) Change to your GEOS-Chem source code directory.
cd /path/to/your/GEOS-Chem/Code
(2) Start Gitk.
gitk --all &
(3) View the revision history in the top-left Gitk window.
In this example above, the feature/MyFeature branch is checked out, and has some unstaged changes. These are updates that have not been committed yet.
(4) Let's say that you want to abandon the work that you have done in feature/MyFeature branch. This may be because you have added a bunch of debugging print statements into the code, but no longer need them. Let's also assume that you want to reset feature/MyFeature to the same commit as the master branch.
Right-click on the commit corresponding to the master branch. A context menu will appear.
(5) Click on the menu item Reset the feature/MyFeature branch to here.
(6) A dialog box will appear with 3 radio buttons. Click on Hard: reset working tree and index (discard all changes).
(7) Click on OK.
(8) View the revision history again and Refresh Gitk by either pressing F5 or selecting File/Update from the dropdown menu.
You should now see this:
Note that the unstaged changes have now disappeared, and that feature/MyFeature is now at the same commit as master.
--Bob Yantosca (talk) 19:15, 21 June 2019 (UTC)
Reverting to an older state of the code
When you clone GEOS-Chem from the remote repository to your local disk space (with the git clone command), the master branch will point to current stable GEOS-Chem version. However, you may want to revert to an older state of the code. Git allows you to do this very easily.
Let's assume that the latest version of the code is 12.3.2 but that you want to use version 12.0.0. The procedure is as follows:
(1) Change to your local GEOS-Chem source code folder.
cd /path/to/your/GEOS-Chem/Code
(2) Start Gitk.
gitk --all &
(3) View the revision history with Gitk.
(4) Locate the commit in the revision history window corresponding to version 12.0.0. This should be marked with a yellow tag.
(5) Right-click with the mouse on the name of the commit. A context menu will appear.
(6) Select the Create new branch menu item. A dialog box will appear.
(7) Type the name of the new branch. For this example, use the name dev/12.0.0
That's it. You now have two branches:
- master represents the current stable version (in this example, 12.3.2)
- dev/12.0.0 represents GEOS-Chem version 12.0.0
Also note: Using this method, you can check out a new branch from any commit, not just those commits that correspond to GEOS-Chem releases.
Cherry-picking individual commits from another branch
Git allows you to cherry-pick commits, that is to pull a single commit from a different branch into the branch you are currently working on. This can prove useful in many situations. For example, a colleague may have committed a critical bug fix into his or her development version of GEOS-Chem. You may want to only grab that particular bug fix into your current branch without also getting all the other changes that your colleague made.
The best way to cherry-pick commits is via the gitk browser Here is a simple example. Let's assume the following:
- You're working in branch my_branch of your local GEOS-Chem code directory.
- You've pulled the branch containing your colleague's bug fix update into a new branch of your local code directory called Bug-fix-branch.
- You are only interested in merging the commit labeled "Remove obsolete LAVHRRLAI and LMODISLAI from input.geos" into my_branch.
(1) Start Gitk.
gitk --all &
(2) View the revision history in the upper-left Gitk window:
As you can see, my_branch is displayed in boldface, which indicates that is the current checked out branch.
(3) Locate the commit entitled "Remove obsolete LAVHRRLAI and LMODISLAI from input.geos" in the revision history window.
(4) Right-click with them mouse on the commit entitled "Remove obsolete LAVHRRLAI and LMODISLAI from input.geos".
(5) A context menu will appear. Select the Cherry-pick this commit option:
(6) View the revision history in the upper-left Gitk window again:
Note that my_branch now displays higher than Bug-fix-branch in the gitk browser revision history window, because it now has the most recent commit.
If the commit that you brought into my_branch comes from a base version of GEOS-Chem that is significantly different than the your version, he cherry-pick operation may generate conflicts that you will have to manually resolve.
--Bob Yantosca (talk) 17:03, 21 June 2019 (UTC)
Removing references to remote branches
In a previous section, we learned how to pull references for branches at the remote repository into your local source code folder. But when branches are deleted from the remote repository, you will likewise want to delete the references to those branches as well. Here's how to do it.
(1) Start Gitk.
gitk --all &
(2) View the revision history in the top-left GitK window.
(3) Let's assume that you want to remove the reference to the remote branch remotes/origin/feature/N2O5_hetchem branch, as you don't need this anymore. Type:
git branch -r -d origin/feature/N2O5_hetchem
(4) Refresh the revision history display in GitK by pressing F5, or by selecting File/Update from the menu.
As you can see, the remotes/origin/feature/N2O5_hetchem branch has now been deleted.
Further reading
- Advanced Git tutorials overview (Atlassian Git tutorial)
- The Advanced Git Guide (Toptal)
- Advanced Git commands you will actually use (Stosb)
- Advanced Git usage (Richard K. Darst)
- Learn enough Git to be dangerous (Michael Hartl)