$ git config --global user.name "Sungjae Cho"
$ git config --global user.email "sungjae.cho.1118@gmail.com"
These name and email are not related to the GitHub account.
Set the present working directory as the GitHub home folder
git init
I made a new repository named as 'git-practice'. Then, I got the repository on 'https://github.com/sungjae-cho/git-practice'.
git clone https://github.com/sungjae-cho/git-practice
First, change the working directory to the local directory.
cd <folder name>
cd git-practice
We should make a connection between the repositry in GitHub and the local directory.
git remote add <connection name> <repository url in github>
git remote add git-practice https://github.com/sungjae-cho/git-practice
We can check what connections exist by typing as follows.
git remote -v
After you added some files in the local directory, you would like to add the new files into the repository in the GitHub server. First, add all new files to the list to commit. '.' means all files in the present working directory. The following command is needed only if there are newly added files.
git add <file>
git add .
Second, make a commit of all files with a message.
git commit -a -m <string message>
git commit -a -m "Something has been updated."
Third, push the commit to the remote directory in GitHub.
git push <connection name> <user name>
git push git-practice master
Once you enter the command, you should type GitHub ID and password to push. Then, you can find that the new files have been added in 'https://github.com/sungjae-cho/git-practice'.
Change the working directory to the local directory.
cd <folder name>
cd git-practice
Check what connections exist by typing as follows.
git remote -v
You can see connection names with their URL.
Make a commit of all files with a message.
git commit -a -m <string message>
git commit -a -m "Something has been updated."
Then, push the commit to the remote directory in GitHub.
git push <connection name> <user name>
git push git-practice master
Once you enter the command, you should type GitHub ID and password to push. Then, you can find that the updated files have been updated in 'https://github.com/sungjae-cho/git-practice'.
Update the git directory to the recent one.
git pull <remote-name> <branch name>
git pull origin master
View current remotes.
$ git remote -v
origin https://github.com/OWNER/REPOSITORY.git (fetch)
origin https://github.com/OWNER/REPOSITORY.git (push)
destination https://github.com/FORKER/REPOSITORY.git (fetch)
destination https://github.com/FORKER/REPOSITORY.git (push)
Delete the remote 'destination'.
$ git remote rm destination
Check current remotes.
$ git remote -v
origin https://github.com/OWNER/REPOSITORY.git (fetch)
origin https://github.com/OWNER/REPOSITORY.git (push)
$ git reset --hard
This throws away all your uncommitted changes. For safety, you should always check that the output of git status
. Reference.
To get just file names, type the following.
$ git diff --cached --name-only
To get detailed information, type the following.
$ git diff --cached
The following command returns the recent commits. j
is for down scolloing and k
for up scolling.
$ git log
To see more detailed information about the commit, type the following.
$ git log --stat
Search a particular commit stage where to move over the recent commits.
Type the following to move on to the commit stage. The following command resets the commit ID as the head, say, the last commit ID.
$ git reset <commit-id>
See files added by the git ls-files
command.
git ls-files
git status -u
git ls-files -m
How to commit a change with both “message” and “description” from the command line? [duplicate]
The first way
git commit -m "Title" -m "Description ..........";
The second way
git commit
git checkout <commit_id> <file_path_to_restore>
git fetch --all
git reset --hard origin/master
- Reference: How do I force “git pull” to overwrite local files?
- Explanation:
git fetch
downloads the latest from remote without trying to merge or rebase anything. Then thegit reset
resets the master branch to what you just fetched. The--hard
option changes all the files in your working tree to match the files inorigin/master
.
Open Terminal.
Create a bare clone of the repository.
git clone --bare https://github.com/exampleuser/old-repository.git
Mirror-push to the new repository.
cd old-repository.git
git push --mirror https://github.com/exampleuser/new-repository.git
Remove the temporary local repository you created in step 1.
cd ..
rm -rf old-repository.git
git clone --single-branch --branch <branch_name> <github_repo_url>
cd <repo_dir>
git remote add <new_remote> <your_repo_url>
git push -u <new_remote>; git push --tags -u <new_remote>
"This will gather the entire history leading up to branch_name, but no commits that are not ancestral to it."
git reset --hard <commit-hash>
git push -f origin master
Add the forked original repo to the remote and call it upstream
.
git remote add upstream https://github.com/<original-repo>.git
Fetch all branches of remote upstream
.
git fetch upstream
Rewrite your master
with upstream’s master
using git rebase
.
git rebase upstream/master
Push your updates to your master
. You may need to force the push with --force
.
git push origin master --force
See what is added and deleted for compared with <old-commit-id>
.
git diff <old-commit-id> <new-commit-id>
See what is added and deleted for the current state compared with <old-commit-id>
.
git diff <old-commit-id>
git checkout HEAD -- my-file.txt
--
basically means: treat every argument after this point as a file name.
git add -p # If you do not need to specify some files to commit
git add -p <file_name> # If you want to commit some lines with a particular file
git add -p <file_name> ... <file_name> # If you want to commit some lines with particular files
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Key controllers: y, n, q, s, and ?.
Create a new branch in the local machine.
git checkout -b <new_branch>
Now, a new brach named <new_branch>
has been created.
Push the new branch, <new_branch>
to the GitHub remote repository, origin
.
git push origin <new_branch>
Now, you can find the new branch in your GitHub remote repository.
Reference link
// delete branch locally
git branch -d localBranchName
// delete branch remotely
git push origin --delete remoteBranchName
git checkout
is the Git command to switch a branch and update tracking files in the local machine.
See this official document of git checkout
to understand this command.
According to the document, git checkout
"updates files in the working tree to match the version in the index or the specified tree".
The "working tree" means the current branch, and the "files in the working tree" means the files tracked by the current branch.
Thus, untracking files is not changed after git checkout
.
git checkout <dest_branch>
Reference: git rebase | Atlassian Git Tutorial
- Merging results in an extraneous merge commit every time you need to incorporate upstream changes.
- The major benefit of rebasing is that you get a much cleaner project history.
- Rebasing eliminates the unnecessary merge commits required by git merge.
- Rebasing also results in a perfectly linear project history.
- There are two trade-offs for rebasing pristine commit history: safety and traceability.
- Never use rebasing on public branches. Is anyone else looking at this branch?” If the answer is yes, take your hands off the keyboard and start thinking about a non-destructive way to make your changes (e.g., the git revert command)
- Otherwise, you’re safe to re-write history as much as you like.
You can rebase the feature
branch onto master
branch using the following commands:
git checkout feature git rebase master
git clone -b <branch_name> --single-branch <repo_url>
Clone a root repository.
git clone https://github.com/<GITHUB_ID>/<project_name>.git
Add a submodule.
cd <project_name>
git submodule add -b <branch> https://github.com/<GITHUB_ID>/<project_name>.git <submodule_directory_name>
You can check the <submodule_directory_name>
folder and .gitmodules
file have been created.
Then, commit and push the submodule file that you can check with git status -u
.
Remove a submodule
# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule
# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule
# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule
A. My repo is preserved, and a new parent repo appears.
- Deleting a private repository will delete all of its forks.
- Deleting a public repository will not delete its forks.
- If public repo is converted to private, and deleted the original: Forked repo will not be deleted. a public repository's forks will remain public in their own separate repository network even after the parent repository is made private. Reference
Type git commit --amend
. Then, a text editor is open and you just edit the old commit message.
- On the command line, navigate (with
git log
) to the repository that contains the commit you want to amend. - Use the
git rebase -i HEAD~n
command to display a list of the lastn
commits in your default text editor. - Replace
pick
withreword
before each commit message you want to change. - Save and close the commit list file.
- In each resulting commit file, type the new commit message, save the file, and close it.
- When you're ready to push your changes to GitHub, use the push --force command to force push over the old commit.
When you add changes in your files, you may obstruct the following change.
old mode 100755
new mode 100644
These different modes are changed depending on the system you use. It is not crucial to commit this change to your commit history. Then, use the folloing command not to see the message above.
git config core.filemode false
The wiki document of Git LFS properly illustrates how to install Git LFS, and this documents expalin how to use Git LFS. I proceeded the following procedure on Ubuntu 20.x.
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
Then, move to the repository where Git LFS will be used. Then, type the following.
git lfs install
Then, designate the file to be tracked by Git LFS using the following.
git lfs track <file-path-to-track>
Then, .gitattributes
has been created in the current location.
Commit and push the file.
git add .gitattributes
git commit -m "Create gitattributes for LFS."
git push <remote> <branch>
Then, commit the file to be tracked by Git LFS.
git add <file-path-to-track>
git commit -m <message-string>
git push <remote> <branch>