-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseful-git-commands
executable file
·241 lines (168 loc) · 6.99 KB
/
useful-git-commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//USEFUL GIT COMMANDS - UPDATED 2022 - JULY
//
//Useful Git commands with simple commentary for each command.
//Especially handy for beginners to Git.
//Use CTRL+F to search for a specific command.
/*
Git workflow for new features:
1. Fetch repository from remote location.
2. Merge changes from the remote.
3. Create a new branch.
4. Make changes.
6. Test the changes and make sure they work.
7. Commit to new branch.
8. Fetch and merge from the remote repo again (in case new commits were made while you were working) .
9. Push your branch to the remote repo for review.
10. Merge new branch with main branch.
11. Delete new branch.
Commit best practices:
* Confine your commits per feature or per bug fix.
Common .gitignore files:
node_modules and other dependencies
API files with secret keys
Log files
System files such as thumbs.db or .DS_Store
Binary files
Production builds
*/
//Create new local git repository in current directory
git init
//Show all current git settings
git config -l
//Add your git username to the project/rep
git config user.email "[email protected]"
//Add your git name to the project/repo
git config user.name "myname"
//Check the current username in use for the current git working directory
git config user.name
//New directory projectName in current directory and create git repository in that directory
git init projectName
//Sign all commits in all repositories with userName
git config --global user.name "userName"
//Check the current username in use for all git repositories.
git config --global user.name
//Sign all commits in all repositories with e-mail myMail
git config --global user.email "[email protected]"
//Discard changes in filename in the working directory
git checkout HEAD fileName
//Remove file from staging area
git reset HEAD -- fileName
//Remove and delete the last commit on the current branch
git reset --hard HEAD~1
//Remove all local changes on current branch
git reset --hard
//Copy a git repository and name the local repo as clone_name (clone_name will be the directory name)
git clone /path/to/repository clone_name
//Copy a remote git repository and name the local repo as clone_name (clone_name will be the directory name)
git clone username@host:/path/to/repository clone_name
//Add files to the staging area
git add fileName1 fileName2
//Add every file to the stating area
git add .
//Add every file in the sub-directory
git add folder/.
//Remove fileName from the staging area
git restore --staged fileName
//Remove every file from the staging area
git restore --staged .
//Commit changes to the working directory
git commit -m "Commit message"
//Commit changes without creating a new commit and keeping the original commit message
git commit --amend --no-edit
//Add file to the staging area and then immediately create a new commit
git add fileName.fileExtension && git commit -m "Commit message"
//Set git user info and configurations
git config
//Display the list of changed files
git status
//Display repository commit history
git log
//Display repository commit history, but only the ones that have keyword
git log -S "keyword"
//Display repository commit history with grpahical/visual overview
git log --oneline --graph - --graph
git log --graph --decorate --oneline --all
//Mark commitID with a tag for release points
git tag commitID
//List all current differences
git diff
//List differences between fileName and base file
git diff --base fileName
//List differences between sourceBranch and targetbranch
git diff sourceBranch targetBranch
//Create a new branch and switch to that branch
git checkout -b branchName
//Switch to a another branch that has branchName
git checkout branchName
//Force switch to another branch that has branchName
//Using force checkout may discard any unsaved/uncommitted changes you made in the branch
git checkout -f branchName
//Create a local branch called localBranchName that tracks a remote branch called remoteBranchName
git checkout -d localBranchName origin/remoteBranchName
//See all branches in the repository. The asterisk '*' shows what branch you are on.
git branch
//Show every available branch from both the local and the remote repo.
git branch -a
git branch --all
//create a new branch named branchName
git branch branchName
//Create a new branch called branchName and then immediately switch to that branch.
git branch branchName && git checkout branchName
//Delete a branch
git branch –d branchName
//Delete a branch that has never been merged or not been fully merged
git branch –D branchName
//View which branches are tracked
git branch -vv
//Merge branchName into the current branch
git merge branchName
//Switch to the main branch and then immediately merge branchName into the main branch
git checkout main && git merge branchName
//Send local commits to the master branch of the remote repository
git push origin main
//View all remote repositories
git remote –v
//Show info about remote branches and pull requests and tracked branches
git remote show origin
//Same as git remote origin, but can be used offline. Uses local cache to retrieve info.
git remote show -n origin
//Connect local repository to a remote server called hostName
git remote add origin hostName
//Add a new remote repo to the local repo called remoteName with source as url
git remote add remoteName url.git
//Check lines of code in a git repo
git ls-files | xargs wc -l
//Delete a connection to a specified remote repository:
git remote rm repositoryName
//Fetch new commits/changes from remote respository
//Git fetch does not merge any changes to the local repo.
//Use git merge origin/master to merge the commits from the fetch into the local main branch
git fetch
//Download a remote branch and create a local branch from it.
git fetch origin branchName
//Fetch a branch from the upstream remote repo to the local forked repo
git fetch upstream branchName
//This combines git fetch and git merge origin/master.
//This merges all changes from the main remote branch to the local repo
git pull
git pull origin main
//This merges all changes from the remote repo into branchName
git pull origin branchName
//Sync the remote upstream repo with the local forked repo.
git pull upstream main
//Merge branchName from the remote upstream repo in the current branch of the local forked repo
git pull upstream branchName
//Add the branchName to the remote repository. Note that this does not merge the branch with the main remote branch
git push origin branchName
//Move to branch with Branchname, then add files to staging area, then commit the files to the branch, then push the branch to the remote repository
git checkout branchName && git add fileName && git commit -m "Commit message" && git push origin branchName
//Login with the Github CLI tool
gh auth login
//Create a new issue on the Github remote repo
gh issue create --title "my_title" --body "my_description"
//Check the status of your Github ussues (you need be inside your local git repo directory)
gh issue status
//Create a Github pull request
gh pr create
//Merge the Github pull request
gh pr merge