Introduction
In this post, I will share to you some of the basic commands and concepts revolving around GIT.
If you have experience in git and you want to skip, you can go directly to "Examples" section.
Workflow
Here are the trees maintained by git:
Working Directory -> Index (Add) -> Head (Commit)
working dir = holds actual files
index = staging area
head = contains last commit you madeCommands
# repository commands
git clone https://github.com/username/hello-world.git # clones a remote repo
git clone username@host:/path/to/repository # same as above but using ssh keys
git remote -v # prints remote branches
# configs
cat .git/config
# adding/removing files/directories to index
git add filename # adds a single file
git add fileA fileB ... fileN # adds multiple files
git add * # adds all files and directories (except empty directories)
git add dir/* # adds dir and its contents (dir must be non-empty)
git reset file # unstages a file
# commiting changes
git commit -m "description of change you've done" # short way
git commit # long-way (that will bring you to a VI editor)
# pushing changes
git push # push all changes to repo (regardless on what branch you are)
# branching
git branch # lists all branches
git branch branch_name # creates a branch*
git checkout branch_name # switches to another branch
git push origin branch_name # push branch to repo**
* when you create a branch, that new branch will also contain all files and directories on the
the current branch where you are located. So if you create a branch from a branch with no files,
expect that the new branch will also have no contents!
** you cna push a branch even you are not currently located at that branch
# history and logging
git blame <path to file> # prints all changes on a file line by line
git log # prints all commitsExamples
Here are practical examples to get you going instead of blowing your mind with lots of
commands.
Cloning a remote repository w/o SSH keys:
git clone https://github.com/batman31/myproject
* cloning a remote remote clones also the branches associated with it
Cloning a respository w/ SSH keys:
* first, generate SSH key pair *
* second, upload the public key to github (usually its in the settings menu where you can add one) *
git clone git@github.com:zeon31/hello-world.git
Deleting a file from localdisk and remote git repo:
git rm octopus.txt # to delete multiples files, you can ise wildcards like `git rm file*`
git commit -m "deleted octopus.txt"
git push
How to delete a directory and its contents?
git rm pets/*
git commit -m "pesky pets"
git push
Creating a branch and submitting a pull/merge request:
* given that your local repo is up-to-date *
git branch fix-bug
git checkout fix-bug
* do you code change on the file you want to change ... *
* go to git hub: project > merge requests > create one > merge*
I have so many untracked files and I want to remove all of them
git clean -fd
* -f = force
* -d = include directories
How to delete a branch in 2 steps
1. delete local branch: git branch -d <branch name>
2. delete remote branch: git push origin --delete <branch name>
Restoring a deleted file
git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"Sources
A simple nice guide with illustrations
http://rogerdudler.github.io/git-guide/
Another nice tutorial allowing users to enter commands on the web page
https://try.github.io/
Tuesday, April 13, 2021
Git Tutorial
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment