-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the git-cheat-sheet wiki!
Git as course:
- What is Git?
- Theoretical Git.
- How to install Git on your machine?
- Start using Git: Make a new project, create on file, then init a git repo. Then add it and commit it.
Creating Branches and switch between them.
Git log: to see and check all commits and Heads
Head: is the last commit in a branch.
Detached Head: is when you switch to one commit. this commit will be the detached head
Git ls-files: show all files in staging area
When you delete a file from working area, it still in staging area, you have also to delete it from the staging area.
To delete a file from the staging area, you have two ways:
-
git add
that will add all existing files to the staging area (under the hood will delete the deleted file) -
git rm <file_name>
that will remove the delete file from the staging area
To delete the unstaged changes: git checkout/restore <file_name> // to delete unstaged changes in certain file git checkout/restore . to go back to last head.
To delete unstaged files: git clean -dn
to see what would deleted. git clean -df
to delete this file
Removing added chenges: there is two ways:
- old one: with two commands:
git reset <added_file_name>
and thengit checkout <added_file_name>
- new way: with two commands:
git restore --staged <added_file_name>
and thengit checkout <added_file_name>
Removing commited changes:
- Soft remove: by running:
git reset --soft HEAD~<NUM OF steps back>
// Soft means that all files will stay in staging area.
Example: If we have a release Branch, ad you forget to create a new feature branch, and committed your changes to release branch, what will you do in this case?
First you go back to the last commit in release branch by ranning: git reset --soft HEAD~1/<last_commit_id>
. Now the HEAD is pointed to the last commit, and your changes are in the staging area. So next to do is creating a new feature branch and then commit the changes to this branch and then merge it to release branch.
Delete Branch:
-
git branch -d <Branch_name>
: delete just merged branches. -
git branch -D <Branch_name>
: Force to delete any branch.
Adding changes to an old commit (Detached HEAD): When you checkout to another old commit and then do changes th this commit and commit all changes, you have then to create a new branch and connect this changes to this branch by running: git branch <new-branch-name> <commit_id>
-
git --version
: Check installed Git Version -
git init
: create empty repository -
git status
: Check working directory & staging area status -
git add <file_name> / .
: Add single file or all files to staging area -
git commit -m "message"
: Create a new commit -
git checkout <commit_id>
: Checkout commit (detached head) -
git branch / switch <branch_name>
: Create a new branch without go to the new branch -
git checkout -b / switch -c <branch_name>
: Create new branch and direct go to it -
git checkout <branch_name>
: Go to branch -
git merge <other_branch>
: merge branch with the other branch -
git rm <file_name>
: delete a file from staging area -
git branch -D <branch_name>
: Delete branch
-
git checkout .
: Revert changes to the last state (Just for tracked files) -
git restore <file_name> / .
: Revert changes to the last state (Just for tracked files) -
git clean -df
: Revert changes to the last state (Just for untracked files)
-
git reset <file_name> / . & git checkout / restore <file_name> / .
: Removing files from staging area -
git restore --staged <file_name> / . & git checkout / restore <file_name> / .
: Removing files from staging area
-
git reset --soft HEAD~1
: Undo the latest(~1) commit.
Is a command to save not staged changes into memory. By typing git stash push -m "MESSAGE"
you save the changes, and they will be direct disappear. To get the changes one more time you have to run git stash apply
. By running this command, you get the last changes.
You may do more than changes and save into stash, but to go through the stashes you have always to make stash. Every time you get a stash from memory, you have to stash one more time.
Eg: You have an unstaged file with text "First feature", and you want to save to stash. You have to run this command git stash push -m "First feature"
.
If you want to get back your first feature stash, you have to run git stash apply 0
. Stash 0 has always the latest stash. Now you do a second feature, and you stash it into git stash push -m "Second feature"
. Now you can get the latest stash by running git stash apply 0
.
If you want now to do new changes on the second feature, you have to get the first stash and do the changes.
First you go to the first feature stash git stash apply 1
, do changes and then make a stash
To add a stash to you project, just run get stash pop {index}
, this will add stash to project and delete it from stash list.
To just delete stash from list, run git stash drop {index}
To delete all stashes git stash clear
Let's say we have a master branch with two commits m1 and m2, from m2 we make a new feature branch and do also two commits f1 and f2. At this point, we want to merge the feature branch with the master branch. We can do fast-forward merge in this case cuz we do not have additional commits on the master branch.
The Fast-Forward move the HEAD to the last commit in the feature branch.
If we do a normal merge command, we will just move the Head to the last commit of the feature branch. We can capsule all commits in the feature branch and do just one commit, by running git merge --squash feature
. This will add all new changes from feature branch to the staging area of master branch, then we have just to commit these changes. In this case, we will have just one command.
The main idea of this type of merging, is to make a new commit that contains all changes of the feature branch and move the HEAD to this new commit.
We use not fast-forward merging if we make new commits to our master branch, that not contains in feature branch.
We can use the rebase if we have a master branch with two commits m1 and m2, from m2 we create a feature branch with two commits f1 and f2.
Now we make a new commit m3 to master. We can merge now ,aster with feature using not fast-forward. Or we can make Rebase.
Rebase make a new commits (f1, f2) after the commit m3. (the new f1 and f2 are copy of f1 and f2 of feature)
To rum this: go to feature branch and run git rebase master
This command allows us to pick one commit in feature branch (this commit should be done on files that in master branch) and then add it to master branch. Without merge the two branches with the new changes.