This chapter describes Git’s local operation instructions, which may be used without connecting to a network.
Git Basic Operations
Git is decentralized, which means you don’t require a server and can keep your whole repository on your machine.
GIT BASICS
git clone <repo>
- Clone repo located at
$~/KodaCamp> git clone https://github.com/xxxxxxx/KodaCamp.git
Cloning into 'KodaCamp'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 1), reused 9 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), done.
Resolving deltas: 100% (1/1), done.
Create a hello_world.rb
file inside the KodaCamp projects and follow the following git commands.
git init
- Create an empty Git repository in the specified directory. Run with no parameters to set up the current directory as a git repository.
$~/KodaCamp> git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /Users/KodaCamp/.git/
git status
- List the files that are staged, unstaged, or untracked.
$~/KodaCamp> git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello_world.rb
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello_world.rb
git add [file or .]
- Add a file as it now appears to your next commit (stage)
$~/KodaCamp> git add .
$~/KodaCamp> git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello_world.rb
git commit -m "message"
- Commit your staged content as a new commit snapshot
$~/KodaCamp> git commit -m 'add hello world'
[master (root-commit) d53b9b9] add hello world
1 file changed, 1 insertion(+)
create mode 100644 hello_world.rb
REWRITING GIT HISTORY
git commit --amend
- Replace the previous commit with the staged changes and the last commit combined. Use with nothing staged to change the message of the most recent commit.
Type git commit --amend
to update your commit message.
$~/KodaCamp> git commit --amend
add hello world
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: xxx xxx x xx:xx:xx xxxx +0800
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: hello_world.rb
"~/KodaCamp/.git/COMMIT_EDITMSG" 14L, 297B
Next, type shift + I
to edit the console. After that, you can now change your commit message
add hello world file
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: xxx xxx x xx:xx:xx xxxx +0800
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: hello_world.rb
-- INSERT --
After you edit your commit message, you can save it by typing (esc + esc + :wq!)
and hit enter. You can see that your commit message is already updated.
$~/KodaCamp> git commit --amend
[master 2aa0663] add hello world file
Date: xxx xxx x xx:xx:xx xxxx +0800
1 file changed, 1 insertion(+)
create mode 100644 hello_world.rb
git rebase <base>
- Rebase the current branch onto [base]. [base] can be a commit ID, branch name, a tag, or a relative reference to HEAD.
git reflog
- Display a history of modifications to the HEAD of the local repository. Add the —relative-date flag to display date information or —all to display all references.
GIT BRANCHES
git checkout -b <branch-name>
- Create and check out a new branch called [branch]. To checkout, an existing branch, use the -b flag.
$~/KodaCamp> git checkout -b Feature/sample_script
Switched to a new branch 'Feature/sample_script'
git merge <branch>
- Merge [branch] into the current branch.
git checkout
- Change to a different branch and check it out in your working directory.
$~/KodaCamp> git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
git branch
- List every branch in your repository. Insert a [branch] parameter to create a new branch called
$~/KodaCamp> git branch
* Feature/sample_script
master
(END)
UNDOING CHANGES
git revert <commit>
- Create a new commit that undoes all of the modifications made in the previous [commit], then apply it to the current branch.
git reset <file>
- Remove [file] from the staging area, but leave the working directory unchanged. It removes a file from staging without overwriting any changes.
git clean -n
- Displays which files would be removed from the working directory. Use the -f flag in place of the -n flag to execute the clean.
REMOTE REPOSITORIES
git remote add <alias> <url>
- Create a new connection to a remote repository. In other commands, after you’ve added a remote, you can use [alias] as a shorthand for [url].
git remote add origin https://github.com/xxxxxxxx/KodaCamp.git
git fetch <remote> <branch>
- Get a specific [branch] from the repository. Remove the [branch] to get all remote references.
git pull <remote>
- Fetch a copy of the current branch from the given remote and merge it into the file system immediately.
$~/KodaCamp> git pull
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), 643 bytes | 321.00 KiB/s, done.
From https://github.com/cdrk811/KodaCamp
1bb0f3c..1913bc8 master -> origin/master
Updating 1bb0f3c..1913bc8
Fast-forward
sample_script.rb | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 sample_script.rb
git push <remote> <branch>
- Push the branch to [remote], along with additional commits and objects. Create a named branch in the remote repository if it doesn’t exist.
$~/KodaCamp> git push --set-upstream origin Feature/sample_script
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 331 bytes | 331.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'Feature/sample_script' on GitHub by visiting:
remote: https://github.com/cdrk811/KodaCamp/pull/new/Feature/sample_script
remote:
To https://github.com/cdrk811/KodaCamp.git
* [new branch] Feature/sample_script -> Feature/sample_script
Branch 'Feature/sample_script' set up to track remote branch 'Feature/sample_script' from 'origin'.
GIT CONFIG
Define the author name for all commits made by the current user.
git config --global user.name <name>
To check the author’s name, just type:
git config --global user.name
Define the author email for all commits made by the current user.
git config --global user.email <email>
To check the author’s email, just type:
git config --global user.email
GIT RESET
git reset
- Reset the staging area to reflect the most recent commit, but leave the working directory unchanged.
git reset --hard
- Reset the staging area and working directory to the most recent commit and overwrites all changes in the working directory.
$~/KodaCamp> git reset --hard
HEAD is now at 83782d7 adjust code style
git reset <commit>
- Revert the current branch tip backward to
git reset --hard <commit>
- The same as before, but reset both the staging area and the working directory to match. Delete all uncommitted changes and commits after
GIT REBASE
git rebase -i <base>
- Rebase the current branch onto [base] interactively. Launches editor to enter commands for how each commit will transferred to the new base.
GIT PULL
git pull --rebase <remote>
- Fetch the remote branch’s copy and rebase it into the local copy. To integrate the branches, git rebase is used instead of merge.
GIT PUSH
git push <remote> --force
- Forces the git push even if it results in a non-fast-forward merge. Do not use the –force flag until you’re perfectly sure you know what you’ve done
git push <remote> --all
- Push all of your local branches to the remote selected.
git push <remote> --tags
- Tags are not automatically pushed when you push a branch or utilize the –all flag. All your local tags are sent to the remote repository when you use the –tags flag.
TEMPORARY COMMITS
git stash
- Save modified and staged changes.
If you want to moveout to your current branch and save your modified files, you need to stash your branch. But before you stash your changes, you must first git add
any modifications.
$~/KodaCamp> git add .
$~/KodaCamp> git stash
Saved working directory and index state WIP on sample_script: 5af1f0e Feature: add sample script
git stash list
- List stack-order of stashed file changes.
$~/KodaCamp> git stash list
stash@{0}: WIP on sample_script: 5af1f0e Feature: add sample script
(END)
git stash pop
- Write working from top of stash stack.
$~/KodaCamp> git stash pop
On branch Feature/sample_script
Your branch is up to date with 'origin/Feature/sample_script'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: sample_script.rb
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (de63e771fa0cf1eec7de030afe0f15ddeba26606)
git stash drop
- Discard the changes from top of stash stack.
$~/KodaCamp> git stash drop
Dropped refs/stash@{0} (cdcdda06ed51ba503c4aacfb50eb368841157df2)