-
I want to create a new branch that is based on the current branch.
-
I want to list the files that have been modified in the current working tree.
-
I want to view the changes that were made in a given commit.
-
I want to list the files that were changed in a given commit.
-
I want to view the changes that were made across multiple commits.
-
I want to open the contents of a file in a given commit in my editor.
-
I want to copy a file from a given commit into my current working tree.
-
I want to copy the last commit from another branch into my branch.
-
I want to copy an earlier commit from the current branch to the
head. -
I want to revert the merge of my feature branch into
master. -
I want to extract changes that I accidentally made to
master. -
I want to link an existing local repository to a remote repository.
The status command shows differences between the working tree, the index, and head commit.
git status
In general, you want to implement new features in short-lived "feature branches" so that changes can be isolated. You can use the checkout command to create a new branch based on the current branch:
git checkout master
# Creates a new branch, `my-feature`, based on `master`.
git checkout -b my-feature
In some of the git commands, the - token refers to the "last branch" that you had checked-out. This makes it very easy to jump back-and-forth between two branches:
git checkout master
git checkout my-feature
# At this point, the `-` refers to the `master` branch.
git checkout -
# At this point, the `-` refers to the `my-feature` branch.
git checkout -
The - token can also be used to merge-in the last branch that you had checked-out:
git checkout my-feature
# ... changes to the working tree (your file system).
git add .
git commit -m "Awesome updates."
git checkout master
# At this point, the `-` refers to the `my-feature` branch.
git merge -
The - token can also be used to cherry-pick the most recent commit of the last branch that you had checked-out:
git checkout my-feature
# ... changes to the working tree (your file system).
git add .
git commit -m "Minor tweaks."
git checkout master
# At this point, the `-` refers to the `my-feature` branch.
git cherry-pick -
By default, when you call git diff, you see all of the content that has been modified in the current working tree (and not yet staged). However, you can use the --stat modifier to simply list the files that have been modified:
git diff --stat
When show is given a branch name, it will default to head - the last or most-recent commit on the given branch:
git checkout master
# Outputs the changes made to the `head` commit of the current (`master`)
# branch.
git show
# Outputs the changes made to the `head` commit of the `my-feature` branch.
git show my-feature
You can also use the show command to target a specific commit that is not the head commit. This can be done with a specific commit hash; or, a relative commit operator like ~:
# Outputs the changes made in the commit with the given hash.
git show 19e771
# Outputs the changes made in in a previous commit of the current (`master`)
# branch.
git show head~ # Show changes in first parent.
git show head~~ # Show changes in first parent's first parent.
git show head~~~ # Show changes in first parent's first parent's first parent.
# Outputs the changes made in a previous commit of the `my-feature` branch.
git show my-feature~
git show my-feature~~
git show my-feature~~~
Just as with git diff, you can limit the output of the git show command using the --stat modifier. This will list the files that were changed in the given commit:
# Outputs the list of files changed in the commit with the given hash.
git show 19e771 --stat
While the show command can show you changes in a given commit, you can use the diff command to show changes across multiple commits:
git checkout master
# Outputs the changes between `head~` and `head` of the current branch. If
# only one commit is provided, other commit is assumed to be `head`.
git diff head~
# Outputs the changes between the first commit and the second commit.
git diff head~~~..head~~ And, since branch names are really just aliases for commits, you can use a branch name in order to show the changes between one branch and your branch:
git checkout my-feature
# At this point, the following are equivalent and output the changes between
# the `head` commit of the `master` branch and the `head` commit of the
# `my-feature` branch.
git diff master
git diff master..head
git diff master..my-feature
By default, the show command shows all of the changes in a given commit. You can limit the scope of the output by using the -- modifier and identifying a filepath:
# Outputs the changes made to the `README.md` file in the `head` commit of the
# `my-feature` branch.
git show my-feature -- README.md
# Outputs the changes made to the `README.md` file in the `19e771` commit.
git show 19e771 -- README.md
By default, the show command shows you the changes made to a file in a given commit. However, if you want to view the entire contents of a file as defined at that time of a given commit, regardless of the changes made in that particular commit, you can use the : modifier to identify a filepath:
# Outputs the contents of the `README.md` file as defined in the `head` commit
# of the `my-feature` branch.
git show my-feature:README.md
# Outputs the contents of the `README.md` file as defined in the `19e771`
# commit.
git show 19e771:README.md
Since you're working on the command-line, the output of any git-command can be piped into another command. As such, you can use the show command to open a previous commit's file-content in your editor or viewer of choice:
# Opens the `README.md` file from the `head` commit of the `my-feature` branch
# in the Sublime Text editor.
git show my-feature:README.md | subl
# Opens the `README.md` file from the `19e771` commit in the `less` viewer.
git show 19e771:README.md | less
Normally, the checkout command will update the entire working tree to point to a given commit. However, you can use the -- modifier to copy (or checkout) a single file from the given commit into your working tree:
git checkout my-feature
# While staying on the `my-feature` branch, copy the `README.md` file from
# the `master` branch into the current working tree. This will overwrite the
# current version of `README.md` in your working tree.
git checkout master -- README.md
When you don't want to merge a branch into your current working tree, you can use the cherry-pick command to copy specific commit-changes into your working tree. Doing so creates a new commit on top of the current branch:
git checkout master
# Copy the `head` commit-changes of the `my-feature` branch onto the `master`
# branch. This will create a new `head` commit on `master`.
git cherry-pick my-feature
Sometimes, after you understand why reverted code was breaking, you want to bring the reverted code back into play and then fix it. You could use the revert command in order to "revert the revert"; but, such terminology is unattractive. As such, you can cherry-pick the reverted commit to bring it back into the head where you can then fix it and commit it:
git checkout master
# Assuming that `head~~~` and `19e771` are the same commit, the following are
# equivalent and will copy the changes in `19e771` to the `head` of the
# current branch (as a new commit).
git cherry-pick head~~~
git cherry-pick 19e771If you want to make changes to a commit after you've already committed the changes in your current working tree, you can use the --amend modifier. This will add any staged changes to the existing commit.
git commit -m "Woot, finally finished!"
# Oops, you forgot a change. Edit the file and stage it.
# ... changes to the working tree (your file system).
git add oops.txt
# Adds the currently-staged changes (oops.txt) to the current commit, giving
# you a chance to update the commit message.
git commit --amendIn addition to adding files to the current commit, the --amend modifier can also be used to change the current commit message:
git add .
git commit -m "This is greet."
# Oh noes! You misspelled "great". You can edit the current commit message:
git commit --amend -m "This is great."Note that if you omit the -m message portion of this command, you will be able to edit the commit message in your configured editor.
At first, you may be tempted to simply merge your master branch into your feature branch, but doing so will create an unattactive, non-intuitive, Frankensteinian commit tree. Instead, you should rebase your feature branch on master. This will ensure that your feature commits are cleanly colocated in the commit tree and align more closely with a human mental model:
git checkout my-feature
# This will unwind the commits specific to the `my-feature` branch, pull in
# the missing `master` commits, and then replay your `my-feature` commits.
git rebase masterOnce your my-feature branch has been rebased on master, you could then, if you wanted to, perform a --ff-only merge ("fast forward only") of your feature branch back into master:
git checkout my-feature
git rebase master
# Fast-forward merge of `my-feature` changes into `master`, which means there
# is no creation of a "merge commit" - your `my-features` changes are simply
# added to the top of `master`.
git checkout master
git merge --ff-only my-featureThat said, when you're working on a team where everyone uses a different git workflow, you will definitely want a "merge commit". This way, multi-commit merges can be easily reverted. To force a "merge commit", you can use the --no-ff modifier ("no fast forward"):
# Get the `my-feature` branch ready for merge.
git checkout my-feature
git rebase master
# Merge the `my-feature` branch into `master` creating a merge-commit.
git checkout master
git merge --no-ff my-featureNow, if the merge needs to be reverted, you can simply revert the "merge commit" and all commits associated with the merge will be reverted.
If you performed a --ff-only merge of your feature branch into master, there's no "easy" solution. You either have to reset the branch to an earlier commit (rewriting history); or, you have to revert the individual commits in the merge.
If, however, you performed a --no-ff merge ("no fast forward") that created a "merge commit", all you have to do is revert the merge commit:
git checkout master
# Merge the feature branch in, creating a "merge commit".
git merge --no-ff my-feature
# On noes! You didn't mean to merge that in. Assuming that the "merge commit"
# is now the `head` of `master`, you can revert back to the commit's fist
# parent, the `master` branch: -m 1. And, since `head` and `master` are the
# same commit, the following are equivalent:
git revert -m 1 head
git revert -m 1 masterSometimes, after you've finished working on your feature branch, you execute git checkout master, only find that you've been accidentally working on master the whole time (error: "Already on 'master'"). To fix this, you can checkout a new branch and reset your master branch:
git checkout master
# > error: Already on 'master'
# While on the `master` branch, create the `my-feature` branch as a copy of
# the `master` branch. This way, your `my-feature` branch will contain all of # your recent changes.
git checkout -b my-feature
# Now that your changes are safely isolated, get back into your `master`
# branch and `reset` it with the `--hard` modifier so that your local index
# and file system will match the remote copy.
git checkout master
git reset --hard origin/masterIf you've edited some files and then change your mind about keeping those edits, you can reset the branch using the --hard modifier. This will update the working tree - your file structure - to match the structure of the last commit on the branch (head).
Caution: You will lose data when using the --hard option.
git checkout my-feature
# ... changes to the working tree (your file system).
git add .
# Remove the file from staging AND remove the changes from the file system.
git reset --hardIf you call git reset without the --hard option, it will reset the staging to match the head of the branch, but it will leave your file system in place. As such, you will be left with "unstaged changes" that can be modified and re-committed.
If you want to configure the username and email of you git. Then simply do:
git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"If you want to create a new git branch without switching. You can use :
git branch <branch_name>But if you want to create a new branch and switch to the created branch. You can use:
git checkout -b <branch_name>git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location.
git clone ssh://john@example.com/path/to/my-project.git When you clone a repository, Git automatically sets up a remote named "origin" that points to the repository you cloned from. However, if you create a new local repository or want to link an existing local repository to a remote repository, you need to use the git remote add origin command.
git remote add origin https://github.com/username/repository.gitTo add your local project to a GitHub repository, you can follow these general steps:
1- Initialize Git in your local project:
Open a terminal or command prompt. Navigate to the root directory of your local project. Run the following command to initialize a Git repository:
git init2- Add your project files to the Git repository:
Use the following command to add all files in the current directory and its subdirectories to the repository:
git add .3- Commit your changes:
After adding the files, create a commit to save the current state of your project using the following command:
git commit -m "initial commit"4- Link your local repository to the GitHub repository:
On the GitHub repository page, copy the remote repository URL. You can find it by clicking on the "Code" button and selecting the appropriate URL (HTTPS or SSH). In your terminal or command prompt, run the following command to add the remote repository:
git remote add origin <remote_repository_URL>5- Push your local repository to GitHub:
Use the following command to push your local commits to the remote repository on GitHub:
git push origin <branch_name>'i.e main'git@gitlab.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
- Open the terminal
- Type ssh-keygen
- Press enter.
- It will ask you to save the key to the specific directory.
- Press enter. It will prompt you to type password or enter without password.
- The public key will be created to the specific directory.
- Now go to the directory and open .ssh folder.
- You'll see a file id_rsa.pub. Open it on notepad. Copy all text from it.
- Go to https://gitlab.com/-/profile/keys or
- Paste here in the "key" textfield.
- Now click on the "Title" below. It will automatically get filled.
- Then click "Add key".
HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password
- Go to https://gitlab.com/-/profile/personal_access_tokens
- Add new token
- Now when you are prompted for password enter the token in input field.