Here are a few resources and useful cheat sheets I found to learn Git:
- Git docs
- github-git-cheat-sheet
- joshnh/Git-Commands/README.md (<- this one is very handy)
- Git Cheat Sheet by GitHub
- Step by step guide by GitHowTo
This command creates a file in the directory you are in.
touch test.pyIf you want to create and edit a new file or simple edit an existing file in the terminal, you can use vim:
vim test.pyThis opens vim where you can edit the file), press i to enter the edit mode, type code, press esc to enter command mode, type : x to save and exit the file.
mkdir [dir]cd /Users/USERNAME/FOLDER/FOLDER/Easiest solution is find your folder path by right-clicking folder > press alt > //Copy "file.md" as pathname//
Type cd, paste pathname and press enter
git clone urlThis creates a folder with the repo, so no need to mkdir beforehand.
git pullgit statusgit add [file-name.file-extension]git add [folder]git add -A(to add all changes to files).
It's useful to know that Git works with the content of files and not files themselves. So when we //add// changes to a file we deleted, we are not //adding// the file to the repo but //adding// the changes to the file (i.e. deletions) to Git.
git commit -m "[commit message]"If you staged a change where the file was removed the resulting message after committing will look something like this:
1 file changed, 28 deletions(-)
delete mode 100644 <FILENAME>.<EXTENSION>Don’t push your work until you’re happy with it One of the cardinal rules of Git is that, since so much work is local within your clone, you have a great deal of freedom to rewrite your history locally. However, once you push your work, it is a different story entirely, and you should consider pushed work as final unless you have good reason to change it. In short, you should avoid pushing your work until you’re happy with it and ready to share it with the rest of the world.
Source: git-scm.com
git push -u origin maingit push -u origin my-branch-nameThe syntax is:
git push <remote> <branch>where remote is origin by default and you current branch is used. If your current branch is main, the command
git pushwill supply the two default parameters—effectively running
git push origin mainSource: How to push a local Git branch to Origin
Because my repositories are hosted on GitHub, the terminal will ask for your login credentials to push the changes:
Username for 'https://github.com': <USERNAME>
Password for 'https://<USERNAME>@github.com': <personal_access_token>Once entered, you can see the changes being pushed to the repo.
About personal access tokens:
In Dec 2020, GitHub announced their intent to require the use of token-based authentication (for example, a personal access, OAuth, or GitHub App installation token) for all authenticated Git operations. Beginning August 13, 2021, we will no longer accept account passwords when authenticating Git operations on GitHub.com.
In short, that means you cannot use your account password to authenticate yourself when pushing changes to GitHub from the command line. Instead you will have to use a personal access token that you can generate in Settings > Developer settings > Personal access tokens
Source: Creating a personal access token
I like to check the git status (git status) again to see what changes are left if I haven't committed all changes.
Source: Renaming a repository
Go into 'settings' on GitHub and change the name there manually.
In addition to redirecting web traffic, all git clone, git fetch, or git push operations targeting the previous location will continue to function as if made on the new location.
However, to reduce confusion, we strongly recommend updating any existing local clones to point to the new repository URL. You can do this by using git remote on the command line:
git remote set-url origin new_url
Source: Stack Overflow
git add -p <filename>
# or
git add --patch <filename>Git will break down your file into what it thinks are sensible "hunks" (portions of the file). It will then prompt you with this question:
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?Here is a description of each option:
ystage this hunk for the next commitndo not stage this hunk for the next commitqquit; do not stage this hunk or any of the remaining hunksastage this hunk and all later hunks in the fileddo not stage this hunk or any of the later hunks in the filegselect a hunk to go to/search for a hunk matching the given regexjleave this hunk undecided, see next undecided hunkJleave this hunk undecided, see next hunkkleave this hunk undecided, see previous undecided hunkKleave this hunk undecided, see previous hunkssplit the current hunk into smaller hunksemanually edit the current hunk. You can then edit the hunk manually by replacing +/- by #?print hunk help
If the file is not in the repository yet, you can first do git add -N <filename>.
Afterwards you can go on with git add -p <filename>.
Afterwards, you can use:
git diff --stagedto check that you staged the correct changesgit reset -pto unstage mistakenly added hunksgit commit -vto view your commit while you edit the commit message.
git branchgit branch -rgit branch -aSource: Git Branches: List, Create, Switch to, Merge, Push, & Delete
git checkout -b my-branch-namewhich is short for
git branch my-branch-name
git checkout my-branch-namegit checkout my-branch-nameExample
git checkout mainTo get a list of all branches from the remote, run this command:
git pullRun this command to switch to the branch:
git checkout --track origin/my-branch-nameSource: Git Branches: List, Create, Switch to, Merge, Push, & Delete
git checkout main
git branch --delete branch_nameYou can’t delete the branch you’re currently on. First, switch to another branch and then delete
Source: How To Delete a Local and Remote Git Branch
git push origin --delete branch_nameIn Git, local and remote branches are separate objects. Deleting a local branch doesn’t remove the remote branch.
To delete a remote branch, use the git push command with the
-d(--delete) option:
Source: How To Delete a Local and Remote Git Branch
Rename master branch to newname (in this case 'main')
git branch -m master mainPush newname branch to origin and track origin/newname instead of origin/master
git push -u origin mainChange "Default branch" in Settings / Branches in Github
Settings > Branches > click Switch sign > select new default. Double check it worked on the code page under branches (should show default main).
Delete origin/master
git push origin :masterSource: The easy way to rename "master" to "main" in git and GitHub and GitHub Gist by Comevius
Replacing all branch history/contents (undo commit):
git reset --hard HEAD^Source: Seth Robertson
Basically:
- go onto main branch
- reset that branch to a commit locally
- force push that commit to remote (origin)
git checkout master
git reset --hard e3f1e37
git push --force origin masterSource: Stack Overflow
git branch topic/wip (1)
git reset --hard HEAD~3 (2)
git switch topic/wip (3)
You have made some commits, but realize they were premature to be in the
masterbranch. You want to continue polishing them in a > topic branch, so createtopic/wipbranch off of the currentHEAD.Rewind the
masterbranch to get rid of those three commits.Switch to
topic/wipbranch and keep working.
Source: git-scm.com
git checkout <your-branch> # gets you on your branch
git fetch origin # gets you up to date with origin
git merge origin/main # merges changes from main with <your-branch>Source: Stack Overflow
For example, after feedback or PR Review:
git commit -m "These changes are in response to PR comments"
git push -f origin HEADSource: Stack Overflow
$ git tag
v1.0
v2.0Source: Git documentation
[A lightweight tag] is basically the commit checksum stored in a file — no other information is kept. To create a lightweight tag, don’t supply any of the -a, -s, or -m options, just provide a tag name:
git tag <tagname>This time, if you run git show on the tag, you don’t see the extra tag information. The command just shows the commit:
$ git show v1.4-lw
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
Change version numberSource: Git documentation
You can also tag commits after you’ve moved past them. Suppose your commit history looks like this:
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 Create write support
0d52aaab4479697da7686c15f77a3d64d9165190 One more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc Add commit function
4682c3261057305bdd616e23b64b0857d832627b Add todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a Create write support
9fceb02d0ae598e95dc970b74767f19372d61af8 Update rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc Commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a Update readmeNow, suppose you forgot to tag the project at v1.2, which was at the “Update rakefile” commit. You can add it after the fact. To tag that commit, you specify the commit checksum (or part of it) at the end of the command:
git tag -a v1.2 9fceb02You can see that you’ve tagged the commit:
$ git tag
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5
$ git show v1.2
tag v1.2
Tagger: Scott Chacon <[email protected]>
Date: Mon Feb 9 15:32:16 2009 -0800
version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon <[email protected]>
Date: Sun Apr 27 20:43:35 2008 -0700
Update rakefile
...Source: Git documentation
By default, the
git pushcommand doesn’t transfer tags to remote servers.You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run
git push origin <tagname>.
$ git push origin v1.5
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
Total 14 (delta 3), reused 0 (delta 0)
To [email protected]:schacon/simplegit.git
* [new tag] v1.5 -> v1.5Source: Git documentation
To delete a tag on your local repository, you can use
git tag -d <tagname>. For example, we could remove our lightweight tag above as follows:
$ git tag -d v1.4-lw
Deleted tag 'v1.4-lw' (was e7d5add)Note that this does not remove the tag from any remote servers. There are two common variations for deleting a tag from a remote server.
The second (and more intuitive) way to delete a remote tag is with:
git push origin --delete <tagname>Source: Git documentation
If you want to view the versions of files a tag is pointing to, you can do a
git checkoutof that tag, although this puts your repository in “detached HEAD” state, which has some ill side effects:
$ git checkout v2.0.0
Note: switching to 'v2.0.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final
$ git checkout v2.0-beta-0.1
Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final
HEAD is now at df3f601... Add atlas.json and cover imageIn “detached HEAD” state, if you make changes and then create a commit, the tag will stay the same, but your new commit won’t belong to any branch and will be unreachable, except by the exact commit hash. Thus, if you need to make changes — say you’re fixing a bug on an older version, for instance — you will generally want to create a branch:
$ git checkout -b version2 v2.0.0
Switched to a new branch 'version2'If you do this and make a commit, your
version2branch will be slightly different than yourv2.0.0tag since it will move forward with your new changes, so do be careful.
Source: Git documentation
git tag <tag_name_to_update> <hash_code_new_commit>
git push --force origin <tag_name_to_update>--force because the commit already exists at remote. You can also delete the tag and recreate it to avoid --force.
Source: ToolsQA
git submodule add <URL>- this command removes the reference to the submodule in .gitmodules.
- you might need to remove it from .git/config (if it still shows up)
git rm <path_to_module>Source: Stack Overflow (linked from Atlassian)
To configure your Git client to sign commits by default for a local repository, in Git versions 2.0.0 and above, run
git config commit.gpgsign true. To sign all commits by default in any local repository on your computer, rungit config --global commit.gpgsign true.To store your GPG key passphrase so you don't have to enter it every time you sign a commit, we recommend using the following tools:
Source: Github docs
Further instructions: