Git For Scribd
Git For Scribd
Me
There are many environments for using Git, the base being a command
line. Its original command line is Bash, but if you download “Git for
Windows”, the same exact commands can be called through cmd. There
are also many Git GUIs, like GitGUI, GitHub Desktop, and GitKraken, which I
will cover later.
*Gui list:https://git-scm.com/downloads/guis
recommend
changing
“master” to
“main
So, to add a new snapshot: make changes -> stage them (or some of them) -> commit staged.
Basic Commands
Untracked add
Text editor - there are cases where git opens a text editor as a part of command. For example, when writing
commits without “-m <message>”, a text editor will open to write the commit message.
Set text editor: - git config core.editor <configuration-command>
For example, git config --global core.editor "code --wait“
list of text editor commands: https://git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config
Every config can have --local (default, on repo) --global (on user) --system (on machine) added to it.
Git Status Examples
git status after adding new files git status after git add .
A repository starts with a default branch, usually named master or main, pointing at the latest
commit. New branches can be added with
git branch <branch-name>
this image draws a repository with a master branch, and a testing branch that was added later.
Changing to a new branch, committing a change, and coming back to the branch you were originally
on, will not show the committed changes in the files - because the files shown are from a different
commit.
Time Travel?
What happens if you commit a change on branch, then move to a second branch, and commit
another change? You have now created divergent history:
Which commands
were used to get
from one branch
to this state?
branch ->
checkout ->
commit ->
checkout ->
commit
Generally, if the merged changes are additions of files or lines of code, git merge will work cleanly.
However, changes or removals might cause merge conflicts.
Merge Conflicts
Two branches currently on the same commit,
with the python file containing:
“<<<<<<< HEAD” shows the part of the file in the current branch, and “>>>>>>> merge_test”
shows the part git tried to merge but failed. To complete the merge, you need to resolve the conflict
manually and make a commit.
reset HEAD~1
Where are: (1) HEAD~2, (2) dev, (3) 73102, (4) merge_test~1, (5) 7fd50~2 ?
)2(
)4(
)1(
)5( )3(
Reverting
In order to undo a change made in a commit, without changing the commit history like git reset, there is the command:
git revert <commit>
This creates a new commit on the branch, without the changes made in <commit>. Notice this can be any commit,
even one multiple commits ago.
For example: Then committed another:
Multiple people connect to the same centralized remote, downloading its history, making changes on their local repository,
then uploading them back to the remote. Everyone works together - committing, making branches, merging etc. It’s
important to update the remote any time you commit to the main branch, so other people can start working from the latest
stable version.
It’s possible to create a custom remote repository using a dedicated server, but people usually use services that provide
them, such as GitHub, GitLab or BitBucket.
Running your own server gives you a lot of control and allows you to run the server within your own firewall, but it requires
more time to set up and maintain. If you use a hosted server it’s easy to set up and maintain, but you have to keep your code
on someone else’s servers, and some organizations don’t allow that.
Remote Repo Commands
• Add a remote repository: git remote add <name> <link>
<name> is how the branch containing snapshots from the remote will be called on your local machine, unrelated to the
name of the remote. It is usually called “origin”.
• Change repository branch name: git remote rename <old-name> <new-name>
• Change repository link: git remote set-url <name> <link>
• list remotes: git remote -v
• Update remote with snapshots from current branch: git push (--all) <remote> (-u) <local-branch>
-u will make <remote> the default remote branch when fetching and pulling with the same branch, making it so you don’t
need to write the names every time.
• Update current branch with snapshots from remote branch: git fetch <remote> (<remote-branch>)
• Update current branch and merge: git pull <remote> (<remote-branch>)
not specifying a remote branch will get all branches.
after
pushing it’s just not the same without u
after making a
commit on local
Fetch, Pull and Clone
Fetch will add the branch changes to the new <remote>/<branch> “branch” but will not merge them with the local
one. To do that you need to manually type:
git merge <remote>/<branch>
from the local branch.
The command:
git pull (-u) <remote> <remote-branch>
on the other hand, will do both fetch and merge automatically.
If you want to download a remote repository you don’t have on your machine at all, use:
git clone <link>
and it will get the full repository history in a folder.
Fetch Example
If someone else pushed to the same remote branch
Then merge.
pull would have done the fetch and the merge at the
same time
Now... What is GitHub?
GitHub
Github is an online platform for Git, that provides hosting for online remote repositories. Published in 2007
by Tom Preston-Werner, Chris Wanstrath, P. J. Hyett and Scott Chacon, and bought by Microsoft in 2012.
It has all the functionality of Git and remote repositories as described, as well as:
• Online backup
• Collaboration with anyone that wants to contribute, even people outside of the dev team. This is very
apparent in open-source projects.
Setting up GitHub
After creating an account and signing in, you will have the option to open a new repository.
HTTPS/SSH
The HTTPS/SSH button gives you a choice between two ways for GitHub to ensure the machine you
are working on is connected to this account. Only machines connected to an account can directly
push to its remote repositories.
The manual steps, in case you want to switch accounts or if something breaks, are:
going to Profile -> Settings -> Developer settings -> Personal Access Tokens. Generate a new token and
save it, then search “Manage Windows Credentials” in windows, find this:
For SSH: you will need to generate a private and public key (woah). First go to C:\Users\USER\.ssh to
check if you already have a key. If not, run ssh-keygen -t ed25519 -C <email>
which will generate the keys in a folder (.ssh folder by default). Now copy the public key and paste it
in Profile -> Settings -> SSH and GPG keys -> New SSH key.
Pick the corresponding button and use the link on the side as the <link> in git remote add.
HTTPS Example
paste
Sharing a GitHub Repository
Access to a GitHub repository can be given to other GitHub accounts by adding collaborators. They will be
able to push to the repo, but not change settings.
With forking!
When someone forks your GitHub repository, they get a complete copy of it, on their own account. From
here they can manipulate it as if it was their own. If the repository is public, anyone can fork your project.
This workflow is in general a good way to cleanly merge branches and keep main consistently stable.
Divergent history Deal with conflicts with (from feature) git merge main
https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-
SSH-Public-Key
StackOverflow