Git: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| Line 1: | Line 1: | ||
== Quick Use Guide == | ==Quick Use Guide== | ||
As someone who used SVN for the last decade, switching over to git was slightly confusing since the terminology is different. Hopefully, this usage guide will help. | As someone who used SVN for the last decade, switching over to git was slightly confusing since the terminology is different. Hopefully, this usage guide will help. | ||
=== Committing Files === | ===Committing Files=== | ||
A commit is like a snapshot of the code at a particular time in the repository. It is possible to recover code to a previous commit if something were to happen to the code (such as accidental bugs being introduced, or accidentally deleting or modifying some file). | A commit is like a snapshot of the code at a particular time in the repository. It is possible to recover code to a previous commit if something were to happen to the code (such as accidental bugs being introduced, or accidentally deleting or modifying some file). | ||
| Line 21: | Line 21: | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
$ git commit -m 'message' | $ git commit -m 'message' | ||
}}If you need to commit your changes to a separate branch, such as if you started working on a new feature but it isn't quite ready yet, create a new branch and then commit.{{highlight|lang=terminal|code= | |||
$ git checkout -b wip_feature | |||
$ git commit -a | |||
}} | }} | ||
=== Reverting Changes === | ===Reverting Changes=== | ||
To revert all changes in the current repository, use: | To revert all changes in the current repository, use: | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 37: | Line 40: | ||
}} | }} | ||
=== Remote Origins === | ===Remote Origins=== | ||
Git allows you to push/pull changes from other remote git repositories. Remote repositories can be in another path or hosted somewhere on the internet. | Git allows you to push/pull changes from other remote git repositories. Remote repositories can be in another path or hosted somewhere on the internet. | ||
| Line 64: | Line 67: | ||
}} | }} | ||
==== Pushing / Pulling ==== | ====Pushing / Pulling==== | ||
After making a new change in your repository, you may want to push your changes out to other repositories. Do so by running: | After making a new change in your repository, you may want to push your changes out to other repositories. Do so by running: | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 77: | Line 80: | ||
}} | }} | ||
=== Merging === | ===Merging=== | ||
After creating a new branch {{code|development}} and making changes to this branch, you may want to merge the changes on the {{code|development}} branch back into {{code|master}}. | After creating a new branch {{code|development}} and making changes to this branch, you may want to merge the changes on the {{code|development}} branch back into {{code|master}}. | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 88: | Line 91: | ||
The {{code|--no-ff}} flag prevents the merge from executing a fast-forward. This ensures that a new node will be constructed that records the merge event instead of a fast-forwarding on the branch. | The {{code|--no-ff}} flag prevents the merge from executing a fast-forward. This ensures that a new node will be constructed that records the merge event instead of a fast-forwarding on the branch. | ||
=== Restore Deleted File === | ===Restore Deleted File=== | ||
If you notice files are missing somewhere and aren't sure which commit triggered its deletion, run the following to determine the last commit that altered this path. | If you notice files are missing somewhere and aren't sure which commit triggered its deletion, run the following to determine the last commit that altered this path. | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 101: | Line 104: | ||
See also: https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository | See also: https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository | ||
== Branches == | ==Branches== | ||
=== Create a new branch === | ===Create a new branch=== | ||
To create a new branch from the master branch | To create a new branch from the master branch | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 113: | Line 116: | ||
}} | }} | ||
=== Delete a branch === | ===Delete a branch=== | ||
To delete a branch | To delete a branch | ||
{{highlight|lang=terminal|code= | {{highlight|lang=terminal|code= | ||
| Line 119: | Line 122: | ||
}} | }} | ||
=== Pulling & Merging === | ===Pulling & Merging=== | ||
When implementing a feature, it's ideal to separate each feature into its own branch. To test multiple features together, you will need to merge these branches together. Do this using the {{code|git pull}} command. | When implementing a feature, it's ideal to separate each feature into its own branch. To test multiple features together, you will need to merge these branches together. Do this using the {{code|git pull}} command. | ||
| Line 144: | Line 147: | ||
}} | }} | ||
== Identities == | === Rebasing === | ||
If for some reason your master has diverged from the upstream repository (such as if changes were made to master simultaneously while you were working on your local copy), you will notice that the history between both repositories are different. A git pull will result in either a message about a diverged branch, or conflicts between the local and remote changes. | |||
The fix is to either: | |||
# Merge your work upstream (<code>git merge origin/master</code>), thereby integrating your changes with the upstream repo and then commit your changes, or | |||
# Rebase your work on the upstream master using the <code>git rebase origin/master</code> command. Alternatively, you can also git pull with the rebase option (<code>git pull --rebase origin/master</code>) to rebase with the local work as part of the pull. | |||
# If you don't care about your changes, <code>git reset --hard origin/master</code> will bring your local repo up to speed with the upstream, discarding any changes you've already made. | |||
==Identities== | |||
Users typically use the global identity when using Git. This is great if you use the same identity across all repos. | Users typically use the global identity when using Git. This is great if you use the same identity across all repos. | ||
| Line 158: | Line 170: | ||
See: | See: | ||
== Examples == | *www.micah.soy/posts/setting-up-git-identities/ | ||
* http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git | |||
* http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull | ==Examples== | ||
*http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git | |||
*http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull | |||
{{Navbox Linux}}[[Category:Linux]] | {{Navbox Linux}} | ||
[[Category:Linux]] | |||
[[Category:LinuxUtilities]] | [[Category:LinuxUtilities]] | ||
Revision as of 23:23, 22 October 2020
Quick Use Guide
As someone who used SVN for the last decade, switching over to git was slightly confusing since the terminology is different. Hopefully, this usage guide will help.
Committing Files
A commit is like a snapshot of the code at a particular time in the repository. It is possible to recover code to a previous commit if something were to happen to the code (such as accidental bugs being introduced, or accidentally deleting or modifying some file).
Similar to SVN where files are included/excluded to a commit, git allows you to stage/unstage files to commit. A file that is staged will be added to the next commit that is made.
Typically, you will want to stage all files for commit which can be done by running:
$ git add -A .
Run git status to see all files that are staged as well as any changed but unstaged files.
$ git status
To commit all the staged files with a message:
$ git commit -m 'message'
If you need to commit your changes to a separate branch, such as if you started working on a new feature but it isn't quite ready yet, create a new branch and then commit.
$ git checkout -b wip_feature
$ git commit -a
Reverting Changes
To revert all changes in the current repository, use:
## Fetch latest from remote
$ git fetch --all
## Reset all changes fetched previously.
$ git reset --hard HEAD
To revert a specific file from a specific revision, use git checkout.
$ git checkout <revision> <filename>
Remote Origins
Git allows you to push/pull changes from other remote git repositories. Remote repositories can be in another path or hosted somewhere on the internet.
To see your repository's remotes:
$ git remote -v
To add a new remote:
$ git remote add origin git@gitlab-blah:user/repo.git
To remove a remote:
$ git remote rm origin
If your remote uses SSH, you will most likely need to configure SSH in order to make use of SSH key based authentication. You can read more about this at SSH Configuration File, but the basic idea is to create a SSH configuration file at ~/.ssh/config defining the remote's host.
Host gitlab-remote
User git
HostName git.example.com
IdentityFile ~/.ssh/gitlab-remote
Pushing / Pulling
After making a new change in your repository, you may want to push your changes out to other repositories. Do so by running:
$ git push
You may also specify a specific remote and branch as well. Eg: git push remote-name branch-name.
Conversely, to retrieve changes from another repository, run
$ git pull
Merging
After creating a new branch development and making changes to this branch, you may want to merge the changes on the development branch back into master.
development$ git merge master
## Resolve any conflicts
development$ git checkout master
master$ git merge --no-ff development
The --no-ff flag prevents the merge from executing a fast-forward. This ensures that a new node will be constructed that records the merge event instead of a fast-forwarding on the branch.
Restore Deleted File
If you notice files are missing somewhere and aren't sure which commit triggered its deletion, run the following to determine the last commit that altered this path.
## Missing 'scripts' directory. Find the commit that deleted it.
$ git rev-list -n 1 HEAD -- scripts
2709b067bfc836c471ffa02ba569537e459e5e01
## To restore 'scripts', checkout that path from the previous (^) commit.
$ git checkout 2709b067bfc836c471ffa02ba569537e459e5e01^ -- scripts
See also: https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository
Branches
Create a new branch
To create a new branch from the master branch
$ git checkout -b new-branch-name
To create a branch from an existing branch
$ git checkout -b new-branch-name existing-branch
Delete a branch
To delete a branch
$ git branch -d existing-branch
Pulling & Merging
When implementing a feature, it's ideal to separate each feature into its own branch. To test multiple features together, you will need to merge these branches together. Do this using the git pull command.
## Work on a feature
$ git checkout -b feature-A master
## Do your work and commit
$ git commit -a
$ git push origin feature-A
## Work on another feature
$ git checkout -b feature-B master
$ git commit -a
$ git push origin feature-B
## Then, to test both feature-A and feature-B together:
$ git checkout -b testing master
## Pull one, or multiple branches at once with the pull command.
## You will be asked to merge these commits to your branch.
$ git pull origin feature-A
$ git pull origin feature-B
## At this point, your code now should have the changes from both branches.
Rebasing
If for some reason your master has diverged from the upstream repository (such as if changes were made to master simultaneously while you were working on your local copy), you will notice that the history between both repositories are different. A git pull will result in either a message about a diverged branch, or conflicts between the local and remote changes.
The fix is to either:
- Merge your work upstream (
git merge origin/master), thereby integrating your changes with the upstream repo and then commit your changes, or - Rebase your work on the upstream master using the
git rebase origin/mastercommand. Alternatively, you can also git pull with the rebase option (git pull --rebase origin/master) to rebase with the local work as part of the pull. - If you don't care about your changes,
git reset --hard origin/masterwill bring your local repo up to speed with the upstream, discarding any changes you've already made.
Identities
Users typically use the global identity when using Git. This is great if you use the same identity across all repos.
In order to use different identities, unset the global identity and require that identities are specified from each repository's configuration. Without specifying any identities, git commits will use the system's hostname and username which is not desired.
# git config --global --unset user.name
# git config --global --unset user.email
# git config --global --unset user.signingkey
# git config --global user.useConfigOnly true
Create each new identity.
See:
- www.micah.soy/posts/setting-up-git-identities/
Examples
- http://stackoverflow.com/questions/215718/reset-or-revert-a-specific-file-to-a-specific-revision-using-git
- http://stackoverflow.com/questions/1125968/force-git-to-overwrite-local-files-on-pull