1. Git CLIο
1.1. Git Helpο
Help formats:
git config --Help
git help config
1.2. 2 scenarios:ο
Existing project on local machine
Clone remote project to work on
2. Existing project on local machineο
2.1. Initialize the repository on local machineο
To initialize a repository from existing code, first change the command line directory to the project folder:
cd "C:\repos\PC-using-RTD"
Sometimes you may already be in a subfolder and just need to go up to the parent folder:
cd ..
(Also, handy for git use is:) To move the working directory to a subfolder:
cd mydir/
To Initialize the repository:
git init
2.2. Ignore filesο
Add the .gitignore
file from within the project folder.
type nul > .gitignore
Edit the .gitignore
file to list on each line any files of folders to be ignored by git.
2.3. Working tree -> Staging Area -> Historyο
The steps that a file takes to have an updated version of it added to the github repository are:
2.4. Add files to Staging Areaο
See: https://www.youtube.com/watch?v=tcd4txbTtAY&list=PL-osiE80TeTuRUfjRe54Eea17-YfnOOAx&index=6
Files added to the staging area are changed files: new, modified or deleted.
To add all changed files within the project folder to the staging area:
git add
To add a single file from within the project folder to the staging area:
git add filename
To add a single folder from within the project folder to the staging area:
git add directory/
To add all the changed files within the working directory (from which it is run in terminal) to the staging area:
git add .
2.5. Git Statusο
To check the status of the git:
git status
2.6. Remove files from Staging Areaο
To remove a single file from within the project folder from the staging area:
git rm --cached filename
or
git reset filename
To remove all files from within a subfolder within the project folder from the staging area:
git rm -r --cached foldername
To remove all files within the project folder from the staging area:
git reset
2.7. Commit files from Staging Areaο
To commit files from staging area:
git commit -m "First commit"
To add files to staging area and commit at once:
git commit -a -m "Second commit"
or
git commit -am "Second commit"
2.8. View Commit historyο
To check commits:
git log
For a more concise log to check commits:
git log --graph --oneline --decorate
2.9. Rename branchο
To rename current branch:
git branch -M newname
3. Clone remote project to work onο
3.1. Clone a git repositoryο
Clone a git repository to a local folder.
git clone <url> <destination>
Use a dot for the destination to use the working directory as the destination.
git clone https://github.com/gmc-code/PC-Using-RTD.git .
Use a foldername within the working directory as the destination.
git clone https://github.com/gmc-code/PC-Using-RTD.git "clonedrepo"
3.2. View information about remote repositoryο
To list info about the repository:
git remote -v
To list all the local and remote branches in the repo:
git branch -a
3.3. View changes madeο
To show changes to files:
git diff
3.4. Commit changes to filesο
To commit files from staging area:
git diff
git status
git add -A
git status
git commit -m "changes made"
3.5. Pull and Push commit to remote repoο
Pull from remote first to include other users changes then push:
git pull origin master
Pull will list changes in remote repo since last pull from it. Origin is the name of the remote repo and master is the branch. Push updates the remote branch:
git push origin master
3.6. Create branch for desired feature to work inο
Create new branch:
git branch mygitcmds
List local branches; the working branch will be listed with an asterisk:
git branch
To change to a local branch to work on it:
git checkout mygitcmds
Push branch to remote repo:
git push -u origin mygitcmds
-u
associates local with remote branch of same name so git pull
and git push
can be done in future without the other parameters.
To list all the local and remote branches in the repo:
git branch -a
3.7. Merge a branchο
Steps to merge mygitcmds
branch to master branch:
git checkout master
git pull origin master
git branch --merged
git merge mygitcmds
git push origin master
3.8. Delete a branch after merging itο
Steps to delete mygitcmds
branch:
git branch --merged
git branch -d mygitcmds
git branch -a
git push origin --delete mygitcmds
3.9. Remove file from commit not yet pushedο
Undo changes in a file filename
:
git checkout filename
3.10. Change commit message not yet pushedο
Undo commit message:
git commit --amend -m "New message"
3.11. Add a file created since last commit but not yet pushedο
Add the file first:
git add filename
git commit --amend
View details of changed files since last commit:
git log --stat
3.12. Move commit from master to feature branch not yet pushedο
Use this after accidentally make commit to master branch instead of feature branch. Move commit:
git log
Copy the hash of the master branchβs last commit that needs moving.
Switch to feature branch:
git checkout <featurebranch>
Copy the hash of last commit that needs moving.
Copy commit to feature branch:
git cherry-pick <hash>
Check it.
git log
Then return master branch.
git checkout master
git log
3.13. Soft resetο
Then copy hash of initial commit to use in soft reset.
git reset --soft <hash>
git log
git status
The soft reset kept work in modified files as shown by status.
3.14. Default resetο
A mixed (default) reset keeps changes but they are left in working area and are not in staging area.
git reset <hash>
git log
git status
3.15. Hard resetο
A hard reset returns track files back to state at time of the (hash) commit and leaves out changes from files.
git reset --hard <hash>
git log
git status
Untracked files left alone from hard rest can be removed:
git clean -df
-d
removed untracked directories; -f
is to force changes
The working directory will then be clean as shown by:
git status
3.16. Recover from Hard resetο
Get a log of commits in order from last commit:
git reflog
Copy a hash and check out that hash and check it using log:
git checkout <hash>
git log
Make a branch from this detached HEAD state to save the changes:
git branch <new branch>
View branches to check:
git branch
3.17. Make changes when changes have been pushedο
Create new commits to undo changes.
Firstly, copy the hash for the commit to remove:
git log
Use the copied hash below:
git revert <hash>
View the commits:
git log
Use the has for the original commit and the reverted commit to compare them:
git diff <hash_original> <hash_reverted>
3.18. Git Stashο
Use this when you want to store changes in a feature branch but not commit them yet.
git stash save "<message about context>"
All the changes will appear to be gone, but are stashed. To get a list of stashes:
git stash list
This lists the stash id, the branch and the message. To bring back the changes that were stashed:
git stash apply <stashID>
To return to were you were:
git checkout -- .
To apply the first stash and drop or delete the stash:
git stash pop
To drop a stash that is not needed:
git stash drop <stashID>
To drop all stashes:
git stash clear
3.19. Git Stash from master to feature branchο
Steps to take when accidentally working in master branch.
git stash save "feature in master for newfeaturebranch"
git status
git diff
git checkout <newfeaturebranch>
git stash pop
git diff
git add .
git commit -m "new feature from master"