Git for a Developer¶
It's a review and supplementary material for Git as a programmer.
bxhu saying
Once I thought I had mastered the practical usage of Git, until I saw this tutorial from Berkeley.
I believe going through this content will be super helpful for large project development especially in synced workflow.
- bxhu 2024-10-01, at moffit library, UCB
Basic Skills¶
Here we follow the tutorial reference - UCB.
Please follow this book and jump back here after finishing it.
We will ignore most part of the book (as it's super easy) and focus on some important concepts and their usage.
restore¶
What if we want to restore changes from a previous version of our program? We can use git restore! There are a couple ways to do this.
If we want to restore files to the versions in the most recent commit, then we can run git restore without specifying a commit id:
Bash | |
---|---|
1 |
|
If we want to restore to a specific commit, we can identify that commit’s id and restore the files from that commit.
Bash | |
---|---|
1 |
|
Actually, it's a very flexible command and we can use git restore
towards 3 types of files:
- Already
add
andcommit
(git add
+git commit
)git restore <file>
can restore the file to the latest commit. (back tounstage
state)- will discard all the changes after the latest commit.
- Already
add
but notcommit
yet (justgit add
)git restore --staged <file>
remove thestaged
files fromstaging area
but keep the changes in theworking directory
.
- Neither
add
norcommit
(None🤡)git restore <file>
can also be used for this situation.- actually, not really necessary😄
reference
Important: restore does not change the commit history! Or, in other words, the safe containing our panoramic photos is entirely unaffected by the restore command.
The entire point of git is to create a log of everything that has EVER happened to our files.
In other words, if you took a panoramic photo of your room in 2014 and in 2015 and put them in a safe, then decided in 2016 to put it exactly back like it was in 2014, you would not set the panoramic photo from the year 2015 on fire. Nor would you a picture of it in 2016 magically appear inside your safe.
If you wanted to record what it looked like in 2016, you’d need to take another photo (with the appropriate -m message to remember what you just did).
remote¶
Format
Bash | |
---|---|
1 |
|
example:
Bash | |
---|---|
1 |
|
- The
remote-name
is a local nickname for the remote repository. - The
remote-url
can be seen in github remote repository.
After adding a remote, all other commands use its associated short name (nickname)
Check Remote from a local view
Bash | |
---|---|
1 |
|
example:
Bash | |
---|---|
1 2 3 4 5 |
|
High-Level Workflow¶
Now we jump to this book, Using Git - UCB.
As is mentioned above, we will go on focusing on important concepts and their usage instead of 1-to-1 following.
One more thing, pro git is strongly recommended for further understanding.
branch¶
create a branch off of your current branch
Bash | |
---|---|
1 |
|
switch from one branch to another by changing which branch your HEAD pointer references
Bash | |
---|---|
1 |
|
combine the previous two commands which create a new branch and then switch to it with this single command
Bash | |
---|---|
1 |
|
delete branches
Bash | |
---|---|
1 |
|
easily figure out which branch you are on (locally)
Bash | |
---|---|
1 |
|
example
Bash | |
---|---|
1 2 3 4 |
|
figure out all branches you are on (local + remote)
Bash | |
---|---|
1 |
|
Bash | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
merge¶
This merge
command will create a new commit that joins the two branches together and change each branch’s pointer to reference this new commit.
While most commits have only one parent commit, this new merge commit has two parent commits. The commit on the main
branch is called its first parent and the commit on the fixing-ai-heuristics
branch is called its second parent.
rebase¶
Rebasing changes the parent commit of a specific commit. In doing this, it changes the commits so that it is no longer the same.
Rebase
can be used as an alternative to merge
for integrating changes from one branch to another. It is quite different from merge
in that merge
creates a new commit that has both parent branch commits as parents. Rebasing takes one set of commits from a branch and places them all at the end of the other branch.
why rebase
versus merge
There are different reasons why you would want to use rebase
versus merge
. One of these reasons is that rebase leads to a cleaner history when working with many different branches and team members.
fetch and pull¶
fetch
: This is analogous to downloading the commits, but it does not incorporate them into your own code.
Fetching the changes will only update your local copy of the remote code but not merge the changes into your own code.
just get it, not merge
a great example here
pull
: This is equivalent to a fetch
+ merge
. Not only will pull fetch the most recent changes, it will also merge the changes into your HEAD
branch.
get it and merge automatically
fetch example:
Bash | |
---|---|
1 2 3 |
|
pull example:
Bash | |
---|---|
1 2 |
|
Git WTFS¶
WTFS
This part is intended to help you through frequently encountered Weird Technical Failure Scenarios (WTFS) in Git
reference here
pull then push¶
Bash | |
---|---|
1 2 3 4 5 6 7 8 |
|
What has happened here is that your remote (i.e. your online Github repository) contains commits that your local repository does not have.
Luckily, Git is very good about telling you how to fix these errors: if you read the error message carefully, you’ll see that is suggests that you git pull. Do that, fix any merge conflicts, and push. Done!
fix conflicts then pull¶
Bash | |
---|---|
1 2 3 4 5 6 |
|
Java | |
---|---|
1 2 3 4 5 6 7 8 |
|
to be specific
Everything between <<<<<<< HEAD
and =======
is what was on your computer, and everything between =======
and 27ddd0c71515e5cfc7f58a43bcf0e2144c127aed
is what was on the remote server.
just manually adjust the contents above :)
enter a commit message to explain why this merge is necessary¶
Text Only | |
---|---|
1 2 3 4 5 6 |
|
Git has opened a terminal text editor for you to enter a commit message. You can leave the default commit message and exit the text editor.
exit for nano / vim
- vim:
:wq
- nano:
ctrl + x
(simultaneously)
does not appear to be a git repository¶
Bash | |
---|---|
1 2 3 4 |
|
Try running git remote -v
. If your repo is set up correctly, you should see:
Bash | |
---|---|
1 2 3 4 |
|
If you only see the two lines corresponding to origin
, and not the two lines corresponding to skeleton, then Git doesn’t know where to find the skeleton repo.
To fix this, run:
Bash | |
---|---|
1 |
|
Then, run git remote -v
again and ensure that you see two lines corresponding to origin
and two lines corresponding to skeleton
.